Sha256: 22c08d79d05b725221543a236351be7833b5075c0f47a104ae67f200e8adf820

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'irwi_mod/comparators/base'

class IrwiMod::Comparators::DiffLcs < IrwiMod::Comparators::Base

  def initialize
    super

    require 'diff/lcs'
  end

  def build_changes( old_text, new_text )
    diffs = Diff::LCS.sdiff( (old_text || '').mb_chars, (new_text || '').mb_chars ) # Building symmetric diff sequence
    changes = [] # Array for our result changes

    diffs.each do |change|
      case change.action
        when '=' then
          if !changes.empty? && changes.last.action == '=' # Append to last not changed span, if exists
            changes.last.value << change.old_element
          else
            changes << new_not_changed( change.old_element )
          end

        when '+' then
          if !changes.empty? && changes.last.action == '+' # Append to last addition, if exists
            changes.last.new_value << change.new_element
          elsif !changes.empty? && changes.last.action == '!' # Append to last replace, if exists (it's necessary when replacing short string with a new long)
            changes.last.new_value << change.new_element
          else
            changes << new_changed( '+', nil, change.new_element )
          end

        when '-' then
          if !changes.empty? && changes.last.action == '-' # Append to last deletion, if exists
            changes.last.old_value << change.old_element
          else
            changes << new_changed( '-', change.old_element, nil )
          end

        when '!' then
          if !changes.empty? && changes.last.action == '!' # Append to last replace, if exists
            changes.last.old_value << change.old_element
            changes.last.new_value << change.new_element
          else
            changes << new_changed( '!', change.old_element, change.new_element )
          end

      end
    end

    changes
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
irwi_mod-0.0.2 lib/irwi_mod/comparators/diff_lcs.rb
irwi_mod-0.0.1 lib/irwi_mod/comparators/diff_lcs.rb