diff options
-rw-r--r-- | lib/diff/lcs.rb | 44 | ||||
-rw-r--r-- | spec/spec_helper.rb | 8 |
2 files changed, 26 insertions, 26 deletions
diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb index 43d3ce1..38dff6e 100644 --- a/lib/diff/lcs.rb +++ b/lib/diff/lcs.rb @@ -250,8 +250,9 @@ class << Diff::LCS # advance that arrow and will call <tt>callbacks#discard_a</tt> or # <tt>callbacks#discard_b</tt>, depending on which arrow it advanced. If both # arrows point to elements that are not part of the longest common - # subsequence, then #traverse_sequences will advance one of them and call the - # appropriate callback, but it is not specified which it will call. + # subsequence, then #traverse_sequences will advance arrow +a+ and call the + # appropriate callback, then it will advance arrow +b+ and call the appropriate + # callback. # # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>, and # <tt>callbacks#discard_b</tt> are invoked with an event comprising the @@ -292,37 +293,36 @@ class << Diff::LCS b_size = seq2.size ai = bj = 0 - (0...matches.size).each do |i| - b_line = matches[i] - - ax = string ? seq1[i, 1] : seq1[i] - bx = string ? seq2[bj, 1] : seq2[bj] - + matches.each do |b_line| if b_line.nil? - unless ax.nil? or (string and ax.empty?) - event = Diff::LCS::ContextChange.new('-', i, ax, bj, bx) + unless seq1[ai].nil? + ax = string ? seq1[ai, 1] : seq1[ai] + bx = string ? seq2[bj, 1] : seq2[bj] + + event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx) event = yield event if block_given? callbacks.discard_a(event) end else + ax = string ? seq1[ai, 1] : seq1[ai] + loop do break unless bj < b_line bx = string ? seq2[bj, 1] : seq2[bj] - event = Diff::LCS::ContextChange.new('+', i, ax, bj, bx) + event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx) event = yield event if block_given? callbacks.discard_b(event) bj += 1 end bx = string ? seq2[bj, 1] : seq2[bj] - event = Diff::LCS::ContextChange.new('=', i, ax, bj, bx) + event = Diff::LCS::ContextChange.new('=', ai, ax, bj, bx) event = yield event if block_given? callbacks.match(event) bj += 1 end - ai = i + ai += 1 end - ai += 1 # The last entry (if any) processed was a match. +ai+ and +bj+ point just # past the last matching lines in their sequences. @@ -380,14 +380,14 @@ class << Diff::LCS ai += 1 end - next unless bj < b_size - - ax = string ? seq1[ai, 1] : seq1[ai] - bx = string ? seq2[bj, 1] : seq2[bj] - event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx) - event = yield event if block_given? - callbacks.discard_b(event) - bj += 1 + if bj < b_size + ax = string ? seq1[ai, 1] : seq1[ai] + bx = string ? seq2[bj, 1] : seq2[bj] + event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx) + event = yield event if block_given? + callbacks.discard_b(event) + bj += 1 + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 17af248..326c507 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -123,8 +123,8 @@ module Diff::LCS::SpecHelper ], [ ['-', 8, 'n'], - ['-', 9, 'p'], ['+', 9, 'r'], + ['-', 9, 'p'], ['+', 10, 's'], ['+', 11, 't'] ] @@ -148,10 +148,10 @@ module Diff::LCS::SpecHelper ], [ ['-', 9, 'r'], - ['-', 10, 's'], ['+', 8, 'n'], - ['-', 11, 't'], - ['+', 9, 'p'] + ['-', 10, 's'], + ['+', 9, 'p'], + ['-', 11, 't'] ] ] end |