summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortiendo1011 <tiendo1011@gmail.com>2021-12-20 13:42:48 +0700
committertiendo1011 <tiendo1011@gmail.com>2021-12-22 13:39:13 +0700
commit6d15558f727d8ea47db895e32ae9447685122cd2 (patch)
treed47256cd67e6da387e8d57d3b84f8914e436929a
parent2b0a45d7b9c132eb7c9888c28a2a866b112fc2bb (diff)
downloaddiff-lcs-6d15558f727d8ea47db895e32ae9447685122cd2.tar.gz
Use element directly instead of accessing it from the index
We have ai, bj to keep track of which index we're in for both a and b, so keeping i is not needed Without it, the logic will be simpler
-rw-r--r--lib/diff/lcs.rb15
1 files changed, 6 insertions, 9 deletions
diff --git a/lib/diff/lcs.rb b/lib/diff/lcs.rb
index 43d3ce1..f102bb1 100644
--- a/lib/diff/lcs.rb
+++ b/lib/diff/lcs.rb
@@ -292,15 +292,13 @@ 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]
+ matches.each do |b_line|
+ ax = string ? seq1[ai, 1] : seq1[ai]
bx = string ? seq2[bj, 1] : seq2[bj]
if b_line.nil?
unless ax.nil? or (string and ax.empty?)
- 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_a(event)
end
@@ -309,20 +307,19 @@ class << Diff::LCS
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.