diff-lcs 1.4.4 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Contributing.md +3 -0
- data/History.md +219 -107
- data/License.md +6 -4
- data/Manifest.txt +15 -1
- data/Rakefile +81 -25
- data/bin/htmldiff +4 -4
- data/lib/diff/lcs/array.rb +1 -1
- data/lib/diff/lcs/backports.rb +2 -2
- data/lib/diff/lcs/block.rb +4 -4
- data/lib/diff/lcs/callbacks.rb +9 -7
- data/lib/diff/lcs/change.rb +19 -19
- data/lib/diff/lcs/htmldiff.rb +24 -16
- data/lib/diff/lcs/hunk.rb +35 -30
- data/lib/diff/lcs/internals.rb +24 -20
- data/lib/diff/lcs/ldiff.rb +37 -35
- data/lib/diff/lcs.rb +77 -75
- data/lib/diff-lcs.rb +1 -1
- data/spec/change_spec.rb +50 -50
- data/spec/diff_spec.rb +14 -14
- data/spec/fixtures/ldiff/output.diff.chef +4 -0
- data/spec/fixtures/ldiff/output.diff.chef-c +15 -0
- data/spec/fixtures/ldiff/output.diff.chef-e +3 -0
- data/spec/fixtures/ldiff/output.diff.chef-f +3 -0
- data/spec/fixtures/ldiff/output.diff.chef-u +9 -0
- data/spec/fixtures/ldiff/output.diff.chef2 +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-c +20 -0
- data/spec/fixtures/ldiff/output.diff.chef2-d +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-e +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-f +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-u +16 -0
- data/spec/fixtures/new-chef +4 -0
- data/spec/fixtures/new-chef2 +17 -0
- data/spec/fixtures/old-chef +4 -0
- data/spec/fixtures/old-chef2 +14 -0
- data/spec/hunk_spec.rb +19 -19
- data/spec/issues_spec.rb +48 -42
- data/spec/lcs_spec.rb +11 -11
- data/spec/ldiff_spec.rb +13 -11
- data/spec/patch_spec.rb +84 -84
- data/spec/sdiff_spec.rb +111 -109
- data/spec/spec_helper.rb +77 -76
- data/spec/traverse_balanced_spec.rb +191 -189
- data/spec/traverse_sequences_spec.rb +31 -33
- metadata +50 -23
- data/autotest/discover.rb +0 -3
data/lib/diff/lcs.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module Diff; end unless defined? Diff
|
3
|
+
module Diff; end unless defined? Diff
|
4
4
|
|
5
5
|
# == How Diff Works (by Mark-Jason Dominus)
|
6
6
|
#
|
@@ -49,13 +49,13 @@ module Diff; end unless defined? Diff # rubocop:disable Style/Documentation
|
|
49
49
|
# a x b y c z p d q
|
50
50
|
# a b c a x b y c z
|
51
51
|
module Diff::LCS
|
52
|
-
VERSION =
|
52
|
+
VERSION = "1.5.1"
|
53
53
|
end
|
54
54
|
|
55
|
-
require
|
56
|
-
require
|
55
|
+
require "diff/lcs/callbacks"
|
56
|
+
require "diff/lcs/internals"
|
57
57
|
|
58
|
-
module Diff::LCS
|
58
|
+
module Diff::LCS
|
59
59
|
# Returns an Array containing the longest common subsequence(s) between
|
60
60
|
# +self+ and +other+. See Diff::LCS#lcs.
|
61
61
|
#
|
@@ -67,7 +67,7 @@ module Diff::LCS # rubocop:disable Style/Documentation
|
|
67
67
|
# identically for key purposes. That is:
|
68
68
|
#
|
69
69
|
# O.new('a').eql?(O.new('a')) == true
|
70
|
-
def lcs(other, &block)
|
70
|
+
def lcs(other, &block) # :yields: self[i] if there are matched subsequences
|
71
71
|
Diff::LCS.lcs(self, other, &block)
|
72
72
|
end
|
73
73
|
|
@@ -85,14 +85,14 @@ module Diff::LCS # rubocop:disable Style/Documentation
|
|
85
85
|
# Traverses the discovered longest common subsequences between +self+ and
|
86
86
|
# +other+. See Diff::LCS#traverse_sequences.
|
87
87
|
def traverse_sequences(other, callbacks = nil, &block)
|
88
|
-
traverse_sequences(self, other, callbacks || Diff::LCS::SequenceCallbacks, &block)
|
88
|
+
Diff::LCS.traverse_sequences(self, other, callbacks || Diff::LCS::SequenceCallbacks, &block)
|
89
89
|
end
|
90
90
|
|
91
91
|
# Traverses the discovered longest common subsequences between +self+ and
|
92
92
|
# +other+ using the alternate, balanced algorithm. See
|
93
93
|
# Diff::LCS#traverse_balanced.
|
94
94
|
def traverse_balanced(other, callbacks = nil, &block)
|
95
|
-
traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
|
95
|
+
Diff::LCS.traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
|
96
96
|
end
|
97
97
|
|
98
98
|
# Attempts to patch +self+ with the provided +patchset+. A new sequence based
|
@@ -101,7 +101,7 @@ module Diff::LCS # rubocop:disable Style/Documentation
|
|
101
101
|
def patch(patchset)
|
102
102
|
Diff::LCS.patch(self, patchset)
|
103
103
|
end
|
104
|
-
|
104
|
+
alias_method :unpatch, :patch
|
105
105
|
|
106
106
|
# Attempts to patch +self+ with the provided +patchset+. A new sequence based
|
107
107
|
# on +self+ and the +patchset+ will be created. See Diff::LCS#patch. Does no
|
@@ -141,11 +141,11 @@ module Diff::LCS # rubocop:disable Style/Documentation
|
|
141
141
|
end
|
142
142
|
|
143
143
|
class << Diff::LCS
|
144
|
-
def lcs(seq1, seq2, &block)
|
144
|
+
def lcs(seq1, seq2, &block) # :yields: seq1[i] for each matched
|
145
145
|
matches = Diff::LCS::Internals.lcs(seq1, seq2)
|
146
146
|
ret = []
|
147
|
-
string = seq1.
|
148
|
-
matches.
|
147
|
+
string = seq1.is_a? String
|
148
|
+
matches.each_index do |i|
|
149
149
|
next if matches[i].nil?
|
150
150
|
|
151
151
|
v = string ? seq1[i, 1] : seq1[i]
|
@@ -154,7 +154,7 @@ class << Diff::LCS
|
|
154
154
|
end
|
155
155
|
ret
|
156
156
|
end
|
157
|
-
|
157
|
+
alias_method :LCS, :lcs
|
158
158
|
|
159
159
|
# #diff computes the smallest set of additions and deletions necessary to
|
160
160
|
# turn the first sequence into the second, and returns a description of these
|
@@ -165,7 +165,7 @@ class << Diff::LCS
|
|
165
165
|
# Class argument is provided for +callbacks+, #diff will attempt to
|
166
166
|
# initialise it. If the +callbacks+ object (possibly initialised) responds to
|
167
167
|
# #finish, it will be called.
|
168
|
-
def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes
|
168
|
+
def diff(seq1, seq2, callbacks = nil, &block) # :yields: diff changes
|
169
169
|
diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks, &block)
|
170
170
|
end
|
171
171
|
|
@@ -197,7 +197,7 @@ class << Diff::LCS
|
|
197
197
|
# # insert
|
198
198
|
# end
|
199
199
|
# end
|
200
|
-
def sdiff(seq1, seq2, callbacks = nil, &block)
|
200
|
+
def sdiff(seq1, seq2, callbacks = nil, &block) # :yields: diff changes
|
201
201
|
diff_traversal(:sdiff, seq1, seq2, callbacks || Diff::LCS::SDiffCallbacks, &block)
|
202
202
|
end
|
203
203
|
|
@@ -250,8 +250,9 @@ class << Diff::LCS
|
|
250
250
|
# advance that arrow and will call <tt>callbacks#discard_a</tt> or
|
251
251
|
# <tt>callbacks#discard_b</tt>, depending on which arrow it advanced. If both
|
252
252
|
# arrows point to elements that are not part of the longest common
|
253
|
-
# subsequence, then #traverse_sequences will advance
|
254
|
-
# appropriate callback,
|
253
|
+
# subsequence, then #traverse_sequences will advance arrow +a+ and call the
|
254
|
+
# appropriate callback, then it will advance arrow +b+ and call the appropriate
|
255
|
+
# callback.
|
255
256
|
#
|
256
257
|
# The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>, and
|
257
258
|
# <tt>callbacks#discard_b</tt> are invoked with an event comprising the
|
@@ -281,58 +282,57 @@ class << Diff::LCS
|
|
281
282
|
# <tt>callbacks#discard_b</tt> will be called after the end of the sequence
|
282
283
|
# is reached, if +a+ has not yet reached the end of +A+ or +b+ has not yet
|
283
284
|
# reached the end of +B+.
|
284
|
-
def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks)
|
285
|
+
def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks) # :yields: change events
|
285
286
|
callbacks ||= Diff::LCS::SequenceCallbacks
|
286
287
|
matches = Diff::LCS::Internals.lcs(seq1, seq2)
|
287
288
|
|
288
289
|
run_finished_a = run_finished_b = false
|
289
|
-
string = seq1.
|
290
|
+
string = seq1.is_a?(String)
|
290
291
|
|
291
292
|
a_size = seq1.size
|
292
293
|
b_size = seq2.size
|
293
294
|
ai = bj = 0
|
294
295
|
|
295
|
-
|
296
|
-
b_line = matches[i]
|
297
|
-
|
298
|
-
ax = string ? seq1[i, 1] : seq1[i]
|
299
|
-
bx = string ? seq2[bj, 1] : seq2[bj]
|
300
|
-
|
296
|
+
matches.each do |b_line|
|
301
297
|
if b_line.nil?
|
302
|
-
unless
|
303
|
-
|
298
|
+
unless seq1[ai].nil?
|
299
|
+
ax = string ? seq1[ai, 1] : seq1[ai]
|
300
|
+
bx = string ? seq2[bj, 1] : seq2[bj]
|
301
|
+
|
302
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
304
303
|
event = yield event if block_given?
|
305
304
|
callbacks.discard_a(event)
|
306
305
|
end
|
307
306
|
else
|
307
|
+
ax = string ? seq1[ai, 1] : seq1[ai]
|
308
|
+
|
308
309
|
loop do
|
309
310
|
break unless bj < b_line
|
310
311
|
|
311
312
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
312
|
-
event = Diff::LCS::ContextChange.new(
|
313
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
313
314
|
event = yield event if block_given?
|
314
315
|
callbacks.discard_b(event)
|
315
316
|
bj += 1
|
316
317
|
end
|
317
318
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
318
|
-
event = Diff::LCS::ContextChange.new(
|
319
|
+
event = Diff::LCS::ContextChange.new("=", ai, ax, bj, bx)
|
319
320
|
event = yield event if block_given?
|
320
321
|
callbacks.match(event)
|
321
322
|
bj += 1
|
322
323
|
end
|
323
|
-
ai
|
324
|
+
ai += 1
|
324
325
|
end
|
325
|
-
ai += 1
|
326
326
|
|
327
327
|
# The last entry (if any) processed was a match. +ai+ and +bj+ point just
|
328
328
|
# past the last matching lines in their sequences.
|
329
|
-
while (ai < a_size)
|
329
|
+
while (ai < a_size) || (bj < b_size)
|
330
330
|
# last A?
|
331
|
-
if ai == a_size
|
332
|
-
if callbacks.respond_to?(:finished_a)
|
331
|
+
if ai == a_size && bj < b_size
|
332
|
+
if callbacks.respond_to?(:finished_a) && !run_finished_a
|
333
333
|
ax = string ? seq1[-1, 1] : seq1[-1]
|
334
334
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
335
|
-
event = Diff::LCS::ContextChange.new(
|
335
|
+
event = Diff::LCS::ContextChange.new(">", (a_size - 1), ax, bj, bx)
|
336
336
|
event = yield event if block_given?
|
337
337
|
callbacks.finished_a(event)
|
338
338
|
run_finished_a = true
|
@@ -340,7 +340,7 @@ class << Diff::LCS
|
|
340
340
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
341
341
|
loop do
|
342
342
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
343
|
-
event = Diff::LCS::ContextChange.new(
|
343
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
344
344
|
event = yield event if block_given?
|
345
345
|
callbacks.discard_b(event)
|
346
346
|
bj += 1
|
@@ -350,11 +350,11 @@ class << Diff::LCS
|
|
350
350
|
end
|
351
351
|
|
352
352
|
# last B?
|
353
|
-
if bj == b_size
|
354
|
-
if callbacks.respond_to?(:finished_b)
|
353
|
+
if bj == b_size && ai < a_size
|
354
|
+
if callbacks.respond_to?(:finished_b) && !run_finished_b
|
355
355
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
356
356
|
bx = string ? seq2[-1, 1] : seq2[-1]
|
357
|
-
event = Diff::LCS::ContextChange.new(
|
357
|
+
event = Diff::LCS::ContextChange.new("<", ai, ax, (b_size - 1), bx)
|
358
358
|
event = yield event if block_given?
|
359
359
|
callbacks.finished_b(event)
|
360
360
|
run_finished_b = true
|
@@ -362,7 +362,7 @@ class << Diff::LCS
|
|
362
362
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
363
363
|
loop do
|
364
364
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
365
|
-
event = Diff::LCS::ContextChange.new(
|
365
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
366
366
|
event = yield event if block_given?
|
367
367
|
callbacks.discard_a(event)
|
368
368
|
ai += 1
|
@@ -374,20 +374,20 @@ class << Diff::LCS
|
|
374
374
|
if ai < a_size
|
375
375
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
376
376
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
377
|
-
event = Diff::LCS::ContextChange.new(
|
377
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
378
378
|
event = yield event if block_given?
|
379
379
|
callbacks.discard_a(event)
|
380
380
|
ai += 1
|
381
381
|
end
|
382
382
|
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
383
|
+
if bj < b_size
|
384
|
+
ax = string ? seq1[ai, 1] : seq1[ai]
|
385
|
+
bx = string ? seq2[bj, 1] : seq2[bj]
|
386
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
387
|
+
event = yield event if block_given?
|
388
|
+
callbacks.discard_b(event)
|
389
|
+
bj += 1
|
390
|
+
end
|
391
391
|
end
|
392
392
|
end
|
393
393
|
|
@@ -478,14 +478,14 @@ class << Diff::LCS
|
|
478
478
|
b_size = seq2.size
|
479
479
|
ai = bj = mb = 0
|
480
480
|
ma = -1
|
481
|
-
string = seq1.
|
481
|
+
string = seq1.is_a?(String)
|
482
482
|
|
483
483
|
# Process all the lines in the match vector.
|
484
484
|
loop do
|
485
485
|
# Find next match indices +ma+ and +mb+
|
486
486
|
loop do
|
487
487
|
ma += 1
|
488
|
-
break unless ma < matches.size
|
488
|
+
break unless ma < matches.size && matches[ma].nil?
|
489
489
|
end
|
490
490
|
|
491
491
|
break if ma >= matches.size # end of matches?
|
@@ -493,36 +493,36 @@ class << Diff::LCS
|
|
493
493
|
mb = matches[ma]
|
494
494
|
|
495
495
|
# Change(seq2)
|
496
|
-
while (ai < ma)
|
496
|
+
while (ai < ma) || (bj < mb)
|
497
497
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
498
498
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
499
499
|
|
500
500
|
case [(ai < ma), (bj < mb)]
|
501
501
|
when [true, true]
|
502
502
|
if callbacks.respond_to?(:change)
|
503
|
-
event = Diff::LCS::ContextChange.new(
|
503
|
+
event = Diff::LCS::ContextChange.new("!", ai, ax, bj, bx)
|
504
504
|
event = yield event if block_given?
|
505
505
|
callbacks.change(event)
|
506
506
|
ai += 1
|
507
507
|
else
|
508
|
-
event = Diff::LCS::ContextChange.new(
|
508
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
509
509
|
event = yield event if block_given?
|
510
510
|
callbacks.discard_a(event)
|
511
511
|
ai += 1
|
512
512
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
513
|
-
event = Diff::LCS::ContextChange.new(
|
513
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
514
514
|
event = yield event if block_given?
|
515
515
|
callbacks.discard_b(event)
|
516
516
|
end
|
517
517
|
|
518
518
|
bj += 1
|
519
519
|
when [true, false]
|
520
|
-
event = Diff::LCS::ContextChange.new(
|
520
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
521
521
|
event = yield event if block_given?
|
522
522
|
callbacks.discard_a(event)
|
523
523
|
ai += 1
|
524
524
|
when [false, true]
|
525
|
-
event = Diff::LCS::ContextChange.new(
|
525
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
526
526
|
event = yield event if block_given?
|
527
527
|
callbacks.discard_b(event)
|
528
528
|
bj += 1
|
@@ -532,43 +532,43 @@ class << Diff::LCS
|
|
532
532
|
# Match
|
533
533
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
534
534
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
535
|
-
event = Diff::LCS::ContextChange.new(
|
535
|
+
event = Diff::LCS::ContextChange.new("=", ai, ax, bj, bx)
|
536
536
|
event = yield event if block_given?
|
537
537
|
callbacks.match(event)
|
538
538
|
ai += 1
|
539
539
|
bj += 1
|
540
540
|
end
|
541
541
|
|
542
|
-
while (ai < a_size)
|
542
|
+
while (ai < a_size) || (bj < b_size)
|
543
543
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
544
544
|
bx = string ? seq2[bj, 1] : seq2[bj]
|
545
545
|
|
546
546
|
case [(ai < a_size), (bj < b_size)]
|
547
547
|
when [true, true]
|
548
548
|
if callbacks.respond_to?(:change)
|
549
|
-
event = Diff::LCS::ContextChange.new(
|
549
|
+
event = Diff::LCS::ContextChange.new("!", ai, ax, bj, bx)
|
550
550
|
event = yield event if block_given?
|
551
551
|
callbacks.change(event)
|
552
552
|
ai += 1
|
553
553
|
else
|
554
|
-
event = Diff::LCS::ContextChange.new(
|
554
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
555
555
|
event = yield event if block_given?
|
556
556
|
callbacks.discard_a(event)
|
557
557
|
ai += 1
|
558
558
|
ax = string ? seq1[ai, 1] : seq1[ai]
|
559
|
-
event = Diff::LCS::ContextChange.new(
|
559
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
560
560
|
event = yield event if block_given?
|
561
561
|
callbacks.discard_b(event)
|
562
562
|
end
|
563
563
|
|
564
564
|
bj += 1
|
565
565
|
when [true, false]
|
566
|
-
event = Diff::LCS::ContextChange.new(
|
566
|
+
event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
|
567
567
|
event = yield event if block_given?
|
568
568
|
callbacks.discard_a(event)
|
569
569
|
ai += 1
|
570
570
|
when [false, true]
|
571
|
-
event = Diff::LCS::ContextChange.new(
|
571
|
+
event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
|
572
572
|
event = yield event if block_given?
|
573
573
|
callbacks.discard_b(event)
|
574
574
|
bj += 1
|
@@ -576,10 +576,12 @@ class << Diff::LCS
|
|
576
576
|
end
|
577
577
|
end
|
578
578
|
|
579
|
-
|
580
|
-
|
581
|
-
:
|
579
|
+
# standard:disable Style/HashSyntax
|
580
|
+
PATCH_MAP = { # :nodoc:
|
581
|
+
:patch => {"+" => "+", "-" => "-", "!" => "!", "=" => "="}.freeze,
|
582
|
+
:unpatch => {"+" => "-", "-" => "+", "!" => "!", "=" => "="}.freeze
|
582
583
|
}.freeze
|
584
|
+
# standard:enable Style/HashSyntax
|
583
585
|
|
584
586
|
# Applies a +patchset+ to the sequence +src+ according to the +direction+
|
585
587
|
# (<tt>:patch</tt> or <tt>:unpatch</tt>), producing a new sequence.
|
@@ -627,7 +629,7 @@ class << Diff::LCS
|
|
627
629
|
|
628
630
|
return src.respond_to?(:dup) ? src.dup : src unless has_changes
|
629
631
|
|
630
|
-
string = src.
|
632
|
+
string = src.is_a?(String)
|
631
633
|
# Start with a new empty type of the source's class
|
632
634
|
res = src.class.new
|
633
635
|
|
@@ -655,14 +657,14 @@ class << Diff::LCS
|
|
655
657
|
end
|
656
658
|
|
657
659
|
case action
|
658
|
-
when
|
660
|
+
when "-" # Remove details from the old string
|
659
661
|
while ai < op
|
660
662
|
res << (string ? src[ai, 1] : src[ai])
|
661
663
|
ai += 1
|
662
664
|
bj += 1
|
663
665
|
end
|
664
666
|
ai += 1
|
665
|
-
when
|
667
|
+
when "+"
|
666
668
|
while bj < np
|
667
669
|
res << (string ? src[ai, 1] : src[ai])
|
668
670
|
ai += 1
|
@@ -671,7 +673,7 @@ class << Diff::LCS
|
|
671
673
|
|
672
674
|
res << el
|
673
675
|
bj += 1
|
674
|
-
when
|
676
|
+
when "="
|
675
677
|
# This only appears in sdiff output with the SDiff callback.
|
676
678
|
# Therefore, we only need to worry about dealing with a single
|
677
679
|
# element.
|
@@ -679,7 +681,7 @@ class << Diff::LCS
|
|
679
681
|
|
680
682
|
ai += 1
|
681
683
|
bj += 1
|
682
|
-
when
|
684
|
+
when "!"
|
683
685
|
while ai < op
|
684
686
|
res << (string ? src[ai, 1] : src[ai])
|
685
687
|
ai += 1
|
@@ -693,14 +695,14 @@ class << Diff::LCS
|
|
693
695
|
end
|
694
696
|
when Diff::LCS::Change
|
695
697
|
case action
|
696
|
-
when
|
698
|
+
when "-"
|
697
699
|
while ai < change.position
|
698
700
|
res << (string ? src[ai, 1] : src[ai])
|
699
701
|
ai += 1
|
700
702
|
bj += 1
|
701
703
|
end
|
702
704
|
ai += 1
|
703
|
-
when
|
705
|
+
when "+"
|
704
706
|
while bj < change.position
|
705
707
|
res << (string ? src[ai, 1] : src[ai])
|
706
708
|
ai += 1
|
@@ -736,4 +738,4 @@ class << Diff::LCS
|
|
736
738
|
end
|
737
739
|
end
|
738
740
|
|
739
|
-
require
|
741
|
+
require "diff/lcs/backports"
|
data/lib/diff-lcs.rb
CHANGED
data/spec/change_spec.rb
CHANGED
@@ -1,89 +1,89 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
describe Diff::LCS::Change do
|
6
|
-
describe
|
7
|
-
subject { described_class.new(
|
8
|
-
it { should_not be_deleting
|
9
|
-
it { should
|
10
|
-
it { should_not be_unchanged
|
11
|
-
it { should_not be_changed
|
6
|
+
describe "an add" do
|
7
|
+
subject { described_class.new("+", 0, "element") }
|
8
|
+
it { should_not be_deleting }
|
9
|
+
it { should be_adding }
|
10
|
+
it { should_not be_unchanged }
|
11
|
+
it { should_not be_changed }
|
12
12
|
it { should_not be_finished_a }
|
13
13
|
it { should_not be_finished_b }
|
14
14
|
end
|
15
15
|
|
16
|
-
describe
|
17
|
-
subject { described_class.new(
|
18
|
-
it { should
|
19
|
-
it { should_not be_adding
|
20
|
-
it { should_not be_unchanged
|
21
|
-
it { should_not be_changed
|
16
|
+
describe "a delete" do
|
17
|
+
subject { described_class.new("-", 0, "element") }
|
18
|
+
it { should be_deleting }
|
19
|
+
it { should_not be_adding }
|
20
|
+
it { should_not be_unchanged }
|
21
|
+
it { should_not be_changed }
|
22
22
|
it { should_not be_finished_a }
|
23
23
|
it { should_not be_finished_b }
|
24
24
|
end
|
25
25
|
|
26
|
-
describe
|
27
|
-
subject { described_class.new(
|
28
|
-
it { should_not be_deleting
|
29
|
-
it { should_not be_adding
|
30
|
-
it { should
|
31
|
-
it { should_not be_changed
|
26
|
+
describe "an unchanged" do
|
27
|
+
subject { described_class.new("=", 0, "element") }
|
28
|
+
it { should_not be_deleting }
|
29
|
+
it { should_not be_adding }
|
30
|
+
it { should be_unchanged }
|
31
|
+
it { should_not be_changed }
|
32
32
|
it { should_not be_finished_a }
|
33
33
|
it { should_not be_finished_b }
|
34
34
|
end
|
35
35
|
|
36
|
-
describe
|
37
|
-
subject { described_class.new(
|
38
|
-
it { should_not be_deleting
|
39
|
-
it { should_not be_adding
|
40
|
-
it { should_not be_unchanged
|
41
|
-
it { should
|
36
|
+
describe "a changed" do
|
37
|
+
subject { described_class.new("!", 0, "element") }
|
38
|
+
it { should_not be_deleting }
|
39
|
+
it { should_not be_adding }
|
40
|
+
it { should_not be_unchanged }
|
41
|
+
it { should be_changed }
|
42
42
|
it { should_not be_finished_a }
|
43
43
|
it { should_not be_finished_b }
|
44
44
|
end
|
45
45
|
|
46
|
-
describe
|
47
|
-
subject { described_class.new(
|
48
|
-
it { should_not be_deleting
|
49
|
-
it { should_not be_adding
|
50
|
-
it { should_not be_unchanged
|
51
|
-
it { should_not be_changed
|
52
|
-
it { should
|
46
|
+
describe "a finished_a" do
|
47
|
+
subject { described_class.new(">", 0, "element") }
|
48
|
+
it { should_not be_deleting }
|
49
|
+
it { should_not be_adding }
|
50
|
+
it { should_not be_unchanged }
|
51
|
+
it { should_not be_changed }
|
52
|
+
it { should be_finished_a }
|
53
53
|
it { should_not be_finished_b }
|
54
54
|
end
|
55
55
|
|
56
|
-
describe
|
57
|
-
subject { described_class.new(
|
58
|
-
it { should_not be_deleting
|
59
|
-
it { should_not be_adding
|
60
|
-
it { should_not be_unchanged
|
61
|
-
it { should_not be_changed
|
56
|
+
describe "a finished_b" do
|
57
|
+
subject { described_class.new("<", 0, "element") }
|
58
|
+
it { should_not be_deleting }
|
59
|
+
it { should_not be_adding }
|
60
|
+
it { should_not be_unchanged }
|
61
|
+
it { should_not be_changed }
|
62
62
|
it { should_not be_finished_a }
|
63
|
-
it { should
|
63
|
+
it { should be_finished_b }
|
64
64
|
end
|
65
65
|
|
66
|
-
describe
|
67
|
-
it
|
68
|
-
action, position, element = described_class.new(
|
69
|
-
expect(action).to eq
|
66
|
+
describe "as array" do
|
67
|
+
it "should be converted" do
|
68
|
+
action, position, element = described_class.new("!", 0, "element")
|
69
|
+
expect(action).to eq "!"
|
70
70
|
expect(position).to eq 0
|
71
|
-
expect(element).to eq
|
71
|
+
expect(element).to eq "element"
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
describe Diff::LCS::ContextChange do
|
77
|
-
describe
|
78
|
-
it
|
77
|
+
describe "as array" do
|
78
|
+
it "should be converted" do
|
79
79
|
action, (old_position, old_element), (new_position, new_element) =
|
80
|
-
described_class.new(
|
80
|
+
described_class.new("!", 1, "old_element", 2, "new_element")
|
81
81
|
|
82
|
-
expect(action).to eq
|
82
|
+
expect(action).to eq "!"
|
83
83
|
expect(old_position).to eq 1
|
84
|
-
expect(old_element).to eq
|
84
|
+
expect(old_element).to eq "old_element"
|
85
85
|
expect(new_position).to eq 2
|
86
|
-
expect(new_element).to eq
|
86
|
+
expect(new_element).to eq "new_element"
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
data/spec/diff_spec.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
|
-
describe Diff::LCS,
|
5
|
+
describe Diff::LCS, ".diff" do
|
6
6
|
include Diff::LCS::SpecHelper::Matchers
|
7
7
|
|
8
|
-
it
|
8
|
+
it "correctly diffs seq1 to seq2" do
|
9
9
|
diff_s1_s2 = Diff::LCS.diff(seq1, seq2)
|
10
10
|
expect(change_diff(correct_forward_diff)).to eq(diff_s1_s2)
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it "correctly diffs seq2 to seq1" do
|
14
14
|
diff_s2_s1 = Diff::LCS.diff(seq2, seq1)
|
15
15
|
expect(change_diff(correct_backward_diff)).to eq(diff_s2_s1)
|
16
16
|
end
|
17
17
|
|
18
|
-
it
|
18
|
+
it "correctly diffs against an empty sequence" do
|
19
19
|
diff = Diff::LCS.diff(word_sequence, [])
|
20
20
|
correct_diff = [
|
21
21
|
[
|
22
|
-
[
|
23
|
-
[
|
24
|
-
[
|
25
|
-
[
|
22
|
+
["-", 0, "abcd"],
|
23
|
+
["-", 1, "efgh"],
|
24
|
+
["-", 2, "ijkl"],
|
25
|
+
["-", 3, "mnopqrstuvwxyz"]
|
26
26
|
]
|
27
27
|
]
|
28
28
|
|
@@ -30,22 +30,22 @@ describe Diff::LCS, '.diff' do
|
|
30
30
|
|
31
31
|
diff = Diff::LCS.diff([], word_sequence)
|
32
32
|
correct_diff.each do |hunk|
|
33
|
-
hunk.each
|
33
|
+
hunk.each { |change| change[0] = "+" }
|
34
34
|
end
|
35
35
|
expect(change_diff(correct_diff)).to eq(diff)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "correctly diffs 'xx' and 'xaxb'" do
|
39
|
-
left =
|
40
|
-
right =
|
39
|
+
left = "xx"
|
40
|
+
right = "xaxb"
|
41
41
|
expect(Diff::LCS.patch(left, Diff::LCS.diff(left, right))).to eq(right)
|
42
42
|
end
|
43
43
|
|
44
|
-
it
|
44
|
+
it "returns an empty diff with (hello, hello)" do
|
45
45
|
expect(Diff::LCS.diff(hello, hello)).to be_empty
|
46
46
|
end
|
47
47
|
|
48
|
-
it
|
48
|
+
it "returns an empty diff with (hello_ary, hello_ary)" do
|
49
49
|
expect(Diff::LCS.diff(hello_ary, hello_ary)).to be_empty
|
50
50
|
end
|
51
51
|
end
|