diff-lcs 2.0.0.beta.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +173 -86
- data/CODE_OF_CONDUCT.md +155 -116
- data/CONTRIBUTING.md +29 -28
- data/CONTRIBUTORS.md +15 -11
- data/LICENCE.md +19 -38
- data/Manifest.txt +16 -3
- data/README.md +37 -26
- data/Rakefile +3 -0
- data/SECURITY.md +7 -7
- data/lib/diff/lcs/array.rb +3 -1
- data/lib/diff/lcs/callbacks.rb +1 -1
- data/lib/diff/lcs/change.rb +74 -32
- data/lib/diff/lcs/frozen_value.rb +74 -0
- data/lib/diff/lcs/hunk.rb +146 -58
- data/lib/diff/lcs/internals.rb +183 -72
- data/lib/diff/lcs/ldiff.rb +23 -45
- data/lib/diff/lcs/string.rb +3 -1
- data/lib/diff/lcs/version.rb +1 -1
- data/lib/diff/lcs.rb +29 -26
- data/licences/MIT.txt +19 -0
- data/spec/ldiff_spec.rb +9 -7
- data/test/fixtures/ldiff/output.diff.bin1-q +0 -0
- data/test/fixtures/ldiff/output.diff.bin2-q +1 -0
- data/test/fixtures/ldiff/output.diff.hunks +9 -0
- data/test/fixtures/ldiff/output.diff.hunks-c +35 -0
- data/test/fixtures/ldiff/output.diff.hunks-u +20 -0
- data/test/fixtures/ldiff/output.diff.same +0 -0
- data/test/fixtures/ldiff/output.diff.same-c +0 -0
- data/test/fixtures/ldiff/output.diff.same-u +0 -0
- data/test/fixtures/new-hunks +31 -0
- data/test/fixtures/old-hunks +30 -0
- data/test/test_change.rb +23 -19
- data/test/test_frozen_value.rb +155 -0
- data/test/test_helper.rb +5 -0
- data/test/test_hunk.rb +238 -0
- data/test/test_lcs.rb +147 -0
- data/test/test_ldiff.rb +64 -7
- metadata +28 -18
- /data/{licenses → licences}/COPYING.txt +0 -0
- /data/{licenses → licences}/artistic.txt +0 -0
- /data/{licenses → licences}/dco.txt +0 -0
data/lib/diff/lcs/hunk.rb
CHANGED
|
@@ -1,14 +1,103 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "diff/lcs"
|
|
3
4
|
require "diff/lcs/block"
|
|
4
5
|
|
|
5
6
|
# A Hunk is a group of Blocks which overlap because of the context surrounding each block.
|
|
6
7
|
# (So if we're not using context, every hunk will contain one block.) Used in the diff
|
|
7
8
|
# program (bin/ldiff).
|
|
9
|
+
#
|
|
10
|
+
# Hunks can also be used without bin/ldiff to produce the same output for two sequences of
|
|
11
|
+
# lines.
|
|
12
|
+
#
|
|
13
|
+
# ```ruby
|
|
14
|
+
# require "diff/lcs"
|
|
15
|
+
# require "diff/lcs/hunk"
|
|
16
|
+
#
|
|
17
|
+
# data_old = "a\nb\nc\n".lines
|
|
18
|
+
# data_new = "a\nx\nc\n".lines
|
|
19
|
+
#
|
|
20
|
+
# puts Diff::LCS::Hunk.diff(data_old, data_new, :unified)
|
|
21
|
+
# # @@ -1,3 +1,3 @@
|
|
22
|
+
# # a
|
|
23
|
+
# # -b
|
|
24
|
+
# # +x
|
|
25
|
+
# # c
|
|
26
|
+
# ```
|
|
27
|
+
#
|
|
28
|
+
# The lines must keep their line endings, as String#lines returns them: a last line which
|
|
29
|
+
# does not end with "\n" is reported with "". Chomped lines are
|
|
30
|
+
# not supported, and may add or omit that marker.
|
|
8
31
|
class Diff::LCS::Hunk
|
|
9
32
|
OLD_DIFF_OP_ACTION = {"+" => "a", "-" => "d", "!" => "c"}.freeze # :nodoc:
|
|
10
33
|
private_constant :OLD_DIFF_OP_ACTION
|
|
11
34
|
|
|
35
|
+
FORMATS = %i[old unified context].freeze # :nodoc:
|
|
36
|
+
private_constant :FORMATS
|
|
37
|
+
|
|
38
|
+
# Returns the hunks for the differences between the lines of `data_old` and `data_new`,
|
|
39
|
+
# each with `context` lines of context (3 by default, as `diff -u` and `diff -c`). Hunks
|
|
40
|
+
# which overlap because of their context are merged. Returns an empty array if there are
|
|
41
|
+
# no differences.
|
|
42
|
+
#
|
|
43
|
+
# Each hunk's #diff output may not end with a newline; Hunk.diff adds one between hunks
|
|
44
|
+
# and marks the last hunk, as bin/ldiff does.
|
|
45
|
+
def self.from_diff(data_old, data_new, context = 3)
|
|
46
|
+
unless context.is_a?(Integer) && !context.negative?
|
|
47
|
+
raise ArgumentError, "context must be a non-negative Integer, not #{context.inspect}"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
data_old, data_new = file_lines(data_old), file_lines(data_new)
|
|
51
|
+
|
|
52
|
+
# After we've read up to a certain point in each file, the number of items we've read
|
|
53
|
+
# from each file will differ by FLD (could be 0).
|
|
54
|
+
file_length_difference = 0
|
|
55
|
+
|
|
56
|
+
Diff::LCS.diff(data_old, data_new).each_with_object([]) do |piece, hunks|
|
|
57
|
+
hunk = new(data_old, data_new, piece, context, file_length_difference)
|
|
58
|
+
file_length_difference = hunk.file_length_difference
|
|
59
|
+
|
|
60
|
+
# If a hunk overlaps with the last hunk, join them.
|
|
61
|
+
if context.positive? && hunk.merge(hunks.last)
|
|
62
|
+
hunks[-1] = hunk
|
|
63
|
+
else
|
|
64
|
+
hunks << hunk
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Returns a diff string for the differences between the lines of `data_old` and
|
|
70
|
+
# `data_new` in the provided `format` (`:old`, `:unified`, or `:context`) with `context`
|
|
71
|
+
# lines of context (3 by default, as `diff -u` and `diff -c`). Returns an empty string if
|
|
72
|
+
# there are no differences. The `:old` format has no context lines, so `context` does not
|
|
73
|
+
# change its output.
|
|
74
|
+
#
|
|
75
|
+
# The diff string does not include the file header lines (`---` and `+++` for a unified
|
|
76
|
+
# diff) that are written by bin/ldiff. It has the encoding of the lines.
|
|
77
|
+
def self.diff(data_old, data_new, format = :old, context = 3)
|
|
78
|
+
fail "Unknown diff format #{format}." unless FORMATS.include?(format)
|
|
79
|
+
|
|
80
|
+
old_lines, new_lines = file_lines(data_old), file_lines(data_new)
|
|
81
|
+
hunks = from_diff(old_lines, new_lines, context)
|
|
82
|
+
last = hunks.last
|
|
83
|
+
# The encoding the hunks use, or the one of the data if there are no hunks.
|
|
84
|
+
output = String.new(encoding: (old_lines[0] || new_lines[0] || data_old[0] || data_new[0] || "").encoding)
|
|
85
|
+
newline = "\n".encode(output.encoding)
|
|
86
|
+
|
|
87
|
+
hunks.each do |hunk|
|
|
88
|
+
output << hunk.diff(format, hunk.equal?(last))
|
|
89
|
+
output << newline unless output.end_with?(newline)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
output
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# A single empty line without a newline is an empty file.
|
|
96
|
+
def self.file_lines(data) # :nodoc:
|
|
97
|
+
(data.size == 1 && data[0].empty?) ? [] : data
|
|
98
|
+
end
|
|
99
|
+
private_class_method :file_lines
|
|
100
|
+
|
|
12
101
|
# Create a hunk using references to both the old and new data, as well as the piece of
|
|
13
102
|
# data.
|
|
14
103
|
def initialize(data_old, data_new, piece, flag_context, file_length_difference)
|
|
@@ -33,27 +122,7 @@ class Diff::LCS::Hunk
|
|
|
33
122
|
@file_length_difference = after # The caller must get this manually
|
|
34
123
|
@max_diff_size = @blocks.map { |e| e.diff_size.abs }.max
|
|
35
124
|
|
|
36
|
-
|
|
37
|
-
# adding items in this block), then figure out the line number based on the line
|
|
38
|
-
# number of the other file and the current difference in file lengths.
|
|
39
|
-
if @blocks[0].remove.empty?
|
|
40
|
-
a1 = a2 = nil
|
|
41
|
-
else
|
|
42
|
-
a1 = @blocks[0].remove[0].position
|
|
43
|
-
a2 = @blocks[0].remove[-1].position
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
if @blocks[0].insert.empty?
|
|
47
|
-
b1 = b2 = nil
|
|
48
|
-
else
|
|
49
|
-
b1 = @blocks[0].insert[0].position
|
|
50
|
-
b2 = @blocks[0].insert[-1].position
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
@start_old = a1 || (b1 - before)
|
|
54
|
-
@start_new = b1 || (a1 + before)
|
|
55
|
-
@end_old = a2 || (b2 - after)
|
|
56
|
-
@end_new = b2 || (a2 + after)
|
|
125
|
+
@start_old, @end_old, @start_new, @end_new = block_positions(@blocks[0], before, after)
|
|
57
126
|
|
|
58
127
|
self.flag_context = flag_context
|
|
59
128
|
end
|
|
@@ -124,41 +193,67 @@ class Diff::LCS::Hunk
|
|
|
124
193
|
|
|
125
194
|
private
|
|
126
195
|
|
|
127
|
-
#
|
|
128
|
-
#
|
|
196
|
+
# An old diff has no context lines, so each block of the hunk is written on its own,
|
|
197
|
+
# with the line numbers of its own changes rather than those of the hunk.
|
|
129
198
|
def old_diff(last = false)
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
del, ins, sep, _ = ["< ", "> ", "---\n", "\\n"]
|
|
133
|
-
.map { _1.encode(@preferred_data_encoding) }
|
|
134
|
-
|
|
135
|
-
block = @blocks[0]
|
|
199
|
+
del, ins, sep = ["< ", "> ", "---\n"].map { _1.encode(@preferred_data_encoding) }
|
|
136
200
|
|
|
137
201
|
if last
|
|
138
202
|
old_missing_newline = !@old_empty && missing_last_newline?(@data_old)
|
|
139
203
|
new_missing_newline = !@new_empty && missing_last_newline?(@data_new)
|
|
140
204
|
end
|
|
141
205
|
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
206
|
+
# Context lines are the same in both files, so the difference between the start
|
|
207
|
+
# positions is the file length difference before the first block.
|
|
208
|
+
before = @start_new - @start_old
|
|
209
|
+
|
|
210
|
+
@blocks.each_with_object(String.new(encoding: @preferred_data_encoding)) do |block, s|
|
|
211
|
+
after = before + block.diff_size
|
|
212
|
+
start_old, end_old, start_new, end_new = block_positions(block, before, after)
|
|
213
|
+
last_block = last && block.equal?(@blocks[-1])
|
|
214
|
+
|
|
215
|
+
# Old diff range is just like a context diff range, except the ranges are on one line
|
|
216
|
+
# with the action between them.
|
|
217
|
+
s << "#{line_range(start_old, end_old)}#{OLD_DIFF_OP_ACTION[block.op]}#{line_range(start_new, end_new)}\n"
|
|
218
|
+
.encode(@preferred_data_encoding)
|
|
151
219
|
|
|
152
|
-
|
|
153
|
-
|
|
220
|
+
unless block.remove.empty?
|
|
221
|
+
@data_old[start_old..end_old].each { |e| s << del + e.chomp + @newline }
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
s << @missing_newline << @newline if last_block && old_missing_newline && !new_missing_newline
|
|
225
|
+
s << sep if block.op == "!"
|
|
226
|
+
|
|
227
|
+
unless block.insert.empty?
|
|
228
|
+
@data_new[start_new..end_new].each { |e| s << ins + e.chomp + @newline }
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
s << @missing_newline << @newline if last_block && new_missing_newline && !old_missing_newline
|
|
154
232
|
|
|
155
|
-
|
|
156
|
-
@data_new[@start_new..@end_new].each { |e| s << ins + e.chomp + @newline }
|
|
233
|
+
before = after
|
|
157
234
|
end
|
|
235
|
+
end
|
|
158
236
|
|
|
159
|
-
|
|
237
|
+
# Returns the start and end of each array for the block, as
|
|
238
|
+
# `[start_old, end_old, start_new, end_new]`. If the array doesn't exist (e.g., we're only
|
|
239
|
+
# adding items in this block), then figure out the line number based on the line number
|
|
240
|
+
# of the other file and the difference in file lengths `before` and `after` the block.
|
|
241
|
+
def block_positions(block, before, after)
|
|
242
|
+
if block.remove.empty?
|
|
243
|
+
a1 = a2 = nil
|
|
244
|
+
else
|
|
245
|
+
a1 = block.remove[0].position
|
|
246
|
+
a2 = block.remove[-1].position
|
|
247
|
+
end
|
|
160
248
|
|
|
161
|
-
|
|
249
|
+
if block.insert.empty?
|
|
250
|
+
b1 = b2 = nil
|
|
251
|
+
else
|
|
252
|
+
b1 = block.insert[0].position
|
|
253
|
+
b2 = block.insert[-1].position
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
[a1 || (b1 - before), a2 || (b2 - after), b1 || (a1 + before), b2 || (a2 + after)]
|
|
162
257
|
end
|
|
163
258
|
|
|
164
259
|
def unified_diff(last = false)
|
|
@@ -169,7 +264,7 @@ class Diff::LCS::Hunk
|
|
|
169
264
|
# `outlist` starts containing the hunk of the old file. Removing an item just means
|
|
170
265
|
# putting a '-' in front of it. Inserting an item requires getting it from the new
|
|
171
266
|
# file and splicing it in. We splice in `num_added` items. Remove blocks use
|
|
172
|
-
# `num_added` because splicing changed the length of
|
|
267
|
+
# `num_added` because splicing changed the length of output list.
|
|
173
268
|
#
|
|
174
269
|
# We remove `num_removed` items. Insert blocks use `num_removed` because their item
|
|
175
270
|
# numbers -- corresponding to positions in the NEW file -- don't take removed items
|
|
@@ -215,8 +310,8 @@ class Diff::LCS::Hunk
|
|
|
215
310
|
|
|
216
311
|
def context_diff(last = false)
|
|
217
312
|
s = "***************\n".encode(@preferred_data_encoding)
|
|
218
|
-
s << "*** #{
|
|
219
|
-
r =
|
|
313
|
+
s << "*** #{line_range(@start_old, @end_old)} ****\n".encode(@preferred_data_encoding)
|
|
314
|
+
r = line_range(@start_new, @end_new)
|
|
220
315
|
|
|
221
316
|
spaces = " ".encode(@preferred_data_encoding)
|
|
222
317
|
|
|
@@ -272,17 +367,10 @@ class Diff::LCS::Hunk
|
|
|
272
367
|
s
|
|
273
368
|
end
|
|
274
369
|
|
|
275
|
-
# Generate a range of item numbers to print
|
|
276
|
-
# one item in it. Otherwise, it's 'start,end'
|
|
277
|
-
def
|
|
278
|
-
|
|
279
|
-
when :old
|
|
280
|
-
s, e = (@start_old + 1), (@end_old + 1)
|
|
281
|
-
when :new
|
|
282
|
-
s, e = (@start_new + 1), (@end_new + 1)
|
|
283
|
-
end
|
|
284
|
-
|
|
285
|
-
(s < e) ? "#{s}#{op}#{e}" : e.to_s
|
|
370
|
+
# Generate a range of item numbers to print for the positions `first` to `last`. Only
|
|
371
|
+
# print 1 number if the range has only one item in it. Otherwise, it's 'start,end'
|
|
372
|
+
def line_range(first, last)
|
|
373
|
+
(first < last) ? "#{first + 1},#{last + 1}" : (last + 1).to_s
|
|
286
374
|
end
|
|
287
375
|
|
|
288
376
|
# Generate a range of item numbers to print for unified diff. Print number where block
|
data/lib/diff/lcs/internals.rb
CHANGED
|
@@ -9,19 +9,27 @@ class << Diff::LCS
|
|
|
9
9
|
private :callbacks_for
|
|
10
10
|
|
|
11
11
|
def diff_traversal(method, seq1, seq2, callbacks, &block)
|
|
12
|
-
|
|
12
|
+
diffs =
|
|
13
|
+
if method == :sdiff && Diff::LCS::SDiffCallbacks.equal?(callbacks)
|
|
14
|
+
Diff::LCS::Internals.sdiff_changes(seq1, seq2)
|
|
15
|
+
elsif method == :diff && Diff::LCS::DiffCallbacks.equal?(callbacks)
|
|
16
|
+
Diff::LCS::Internals.diff_hunks(seq1, seq2)
|
|
17
|
+
else
|
|
18
|
+
callbacks = callbacks_for(callbacks)
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
case method
|
|
21
|
+
when :diff
|
|
22
|
+
traverse_sequences(seq1, seq2, callbacks)
|
|
23
|
+
when :sdiff
|
|
24
|
+
traverse_balanced(seq1, seq2, callbacks)
|
|
25
|
+
end
|
|
20
26
|
|
|
21
|
-
|
|
27
|
+
callbacks.finish if callbacks.respond_to? :finish
|
|
28
|
+
callbacks.diffs
|
|
29
|
+
end
|
|
22
30
|
|
|
23
31
|
if block
|
|
24
|
-
|
|
32
|
+
diffs.map do |hunk|
|
|
25
33
|
if hunk.is_a? Array
|
|
26
34
|
hunk.map { block.call(_1) }
|
|
27
35
|
else
|
|
@@ -29,7 +37,7 @@ class << Diff::LCS
|
|
|
29
37
|
end
|
|
30
38
|
end
|
|
31
39
|
else
|
|
32
|
-
|
|
40
|
+
diffs
|
|
33
41
|
end
|
|
34
42
|
end
|
|
35
43
|
private :diff_traversal
|
|
@@ -46,6 +54,8 @@ module Diff::LCS::Internals # :nodoc:
|
|
|
46
54
|
# end
|
|
47
55
|
# ```
|
|
48
56
|
def self.lcs(a, b)
|
|
57
|
+
a = indexable(a)
|
|
58
|
+
b = indexable(b)
|
|
49
59
|
a_start = b_start = 0
|
|
50
60
|
a_finish = a.size - 1
|
|
51
61
|
b_finish = b.size - 1
|
|
@@ -72,22 +82,69 @@ module Diff::LCS::Internals # :nodoc:
|
|
|
72
82
|
thresh = []
|
|
73
83
|
links = []
|
|
74
84
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
bm = b_matches[
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
85
|
+
i = a_start
|
|
86
|
+
while i <= a_finish
|
|
87
|
+
bm = b_matches[a[i]]
|
|
88
|
+
|
|
89
|
+
# Positions are visited in descending order, so each lands in a threshold slot no
|
|
90
|
+
# higher than the previous one: `limit` bounds the search.
|
|
91
|
+
limit = thresh.size - 1
|
|
92
|
+
m = bm.size - 1
|
|
93
|
+
|
|
94
|
+
while m >= 0
|
|
95
|
+
j = bm[m]
|
|
96
|
+
|
|
97
|
+
if thresh.empty? || (j > thresh[-1])
|
|
98
|
+
k = thresh.size
|
|
86
99
|
else
|
|
87
|
-
|
|
100
|
+
# Find the first threshold not below `j`, galloping down from `limit`: positions
|
|
101
|
+
# sharing an element tend to land in neighbouring slots. `j` replaces a larger
|
|
102
|
+
# threshold; an equal one means `j` extends no subsequence.
|
|
103
|
+
high = limit
|
|
104
|
+
step = 1
|
|
105
|
+
low = high - step
|
|
106
|
+
while (low >= 0) && (thresh[low] >= j)
|
|
107
|
+
high = low
|
|
108
|
+
step *= 2
|
|
109
|
+
low = high - step
|
|
110
|
+
end
|
|
111
|
+
low = low.negative? ? 0 : low + 1
|
|
112
|
+
|
|
113
|
+
while low < high
|
|
114
|
+
mid = (low + high) / 2
|
|
115
|
+
if thresh[mid] < j
|
|
116
|
+
low = mid + 1
|
|
117
|
+
else
|
|
118
|
+
high = mid
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
if thresh[low] == j
|
|
123
|
+
k = nil
|
|
124
|
+
limit = low
|
|
125
|
+
else
|
|
126
|
+
k = low
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
if k
|
|
131
|
+
# The next positions down to the smallest one above the previous threshold all
|
|
132
|
+
# land in this slot, each replacing the last: only that smallest one counts.
|
|
133
|
+
lower = k.positive? ? thresh[k - 1] : -1
|
|
134
|
+
if m.positive? && (bm[m - 1] > lower)
|
|
135
|
+
m = bm.bsearch_index { _1 > lower }
|
|
136
|
+
j = bm[m]
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
thresh[k] = j
|
|
140
|
+
links[k] = [k.positive? ? links[k - 1] : nil, i, j]
|
|
141
|
+
limit = k
|
|
88
142
|
end
|
|
89
|
-
|
|
143
|
+
|
|
144
|
+
m -= 1
|
|
90
145
|
end
|
|
146
|
+
|
|
147
|
+
i += 1
|
|
91
148
|
end
|
|
92
149
|
|
|
93
150
|
unless thresh.empty?
|
|
@@ -101,23 +158,114 @@ module Diff::LCS::Internals # :nodoc:
|
|
|
101
158
|
vector
|
|
102
159
|
end
|
|
103
160
|
|
|
104
|
-
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
161
|
+
# Indexing a String allocates a new String on each access; its characters are read once
|
|
162
|
+
# instead. Not for subclasses, whose `[]` may return more than a plain String
|
|
163
|
+
# (`ActiveSupport::SafeBuffer` keeps slices HTML-safe).
|
|
164
|
+
def self.indexable(seq) = seq.instance_of?(String) ? seq.chars : seq
|
|
165
|
+
|
|
166
|
+
# Returns what `traverse_balanced` reports to `Diff::LCS::SDiffCallbacks`, built straight
|
|
167
|
+
# from the LCS without an event to simplify per element. `TestDefaultCallbacksShortcut`
|
|
168
|
+
# holds the two paths equal.
|
|
169
|
+
def self.sdiff_changes(seq1, seq2)
|
|
170
|
+
seq1 = indexable(seq1)
|
|
171
|
+
seq2 = indexable(seq2)
|
|
172
|
+
matches = lcs(seq1, seq2)
|
|
173
|
+
changes = []
|
|
174
|
+
a_i = b_j = m_a = 0
|
|
175
|
+
|
|
176
|
+
loop do
|
|
177
|
+
m_a += 1 while (m_a < matches.size) && matches[m_a].nil?
|
|
178
|
+
|
|
179
|
+
# Past the last match, the ends of both sequences act as one.
|
|
180
|
+
last = m_a >= matches.size
|
|
181
|
+
m_b = last ? seq2.size : matches[m_a]
|
|
182
|
+
m_a = seq1.size if last
|
|
183
|
+
|
|
184
|
+
while (a_i < m_a) || (b_j < m_b)
|
|
185
|
+
if (a_i < m_a) && (b_j < m_b)
|
|
186
|
+
changes << Diff::LCS::ContextChange.new("!", a_i, seq1[a_i], b_j, seq2[b_j])
|
|
187
|
+
a_i += 1
|
|
188
|
+
b_j += 1
|
|
189
|
+
elsif a_i < m_a
|
|
190
|
+
changes << Diff::LCS::ContextChange.new("-", a_i, seq1[a_i], b_j, nil)
|
|
191
|
+
a_i += 1
|
|
192
|
+
else
|
|
193
|
+
changes << Diff::LCS::ContextChange.new("+", a_i, nil, b_j, seq2[b_j])
|
|
194
|
+
b_j += 1
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
break if last
|
|
199
|
+
|
|
200
|
+
changes << Diff::LCS::ContextChange.new("=", a_i, seq1[a_i], b_j, seq2[b_j])
|
|
201
|
+
a_i += 1
|
|
202
|
+
b_j += 1
|
|
203
|
+
m_a += 1
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
changes
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
# Returns what `traverse_sequences` reports to `Diff::LCS::DiffCallbacks`, built straight
|
|
210
|
+
# from the LCS without an event per element. `TestDefaultCallbacksShortcut` holds the two
|
|
211
|
+
# paths equal.
|
|
212
|
+
def self.diff_hunks(seq1, seq2)
|
|
213
|
+
seq1 = indexable(seq1)
|
|
214
|
+
seq2 = indexable(seq2)
|
|
215
|
+
matches = lcs(seq1, seq2)
|
|
216
|
+
a_size = seq1.size
|
|
217
|
+
b_size = seq2.size
|
|
218
|
+
hunks = []
|
|
219
|
+
hunk = []
|
|
220
|
+
a_i = b_j = 0
|
|
221
|
+
|
|
222
|
+
matches.each do |b_line|
|
|
223
|
+
if b_line.nil?
|
|
224
|
+
hunk << Diff::LCS::Change.new("-", a_i, seq1[a_i]) unless seq1[a_i].nil?
|
|
225
|
+
else
|
|
226
|
+
while b_j < b_line
|
|
227
|
+
hunk << Diff::LCS::Change.new("+", b_j, seq2[b_j])
|
|
228
|
+
b_j += 1
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
unless hunk.empty?
|
|
232
|
+
hunks << hunk
|
|
233
|
+
hunk = []
|
|
234
|
+
end
|
|
235
|
+
b_j += 1
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
a_i += 1
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Not a copy of the tail loop of `traverse_sequences` but what it comes down to for
|
|
242
|
+
# `DiffCallbacks`, which has neither `finished_a` nor `finished_b`: one discard of `a`
|
|
243
|
+
# then one of `b` until both run out. A change to that loop must be carried over here.
|
|
244
|
+
while (a_i < a_size) || (b_j < b_size)
|
|
245
|
+
if a_i < a_size
|
|
246
|
+
hunk << Diff::LCS::Change.new("-", a_i, seq1[a_i])
|
|
247
|
+
a_i += 1
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
if b_j < b_size
|
|
251
|
+
hunk << Diff::LCS::Change.new("+", b_j, seq2[b_j])
|
|
252
|
+
b_j += 1
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
hunks << hunk unless hunk.empty?
|
|
257
|
+
hunks
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# This method will analyze the provided `patchset` to provide a single-pass
|
|
261
|
+
# normalization (conversion of the array form of Diff::LCS::Change objects to the object
|
|
262
|
+
# form of same) and detection of whether the `patchset` represents changes to be made.
|
|
107
263
|
def self.analyze_patchset(patchset, depth = 0)
|
|
108
264
|
fail "Patchset too complex" if depth > 1
|
|
109
265
|
|
|
110
266
|
has_changes = false
|
|
111
267
|
new_patchset = []
|
|
112
268
|
|
|
113
|
-
# Format:
|
|
114
|
-
# [ # patchset
|
|
115
|
-
# # hunk (change)
|
|
116
|
-
# [ # hunk
|
|
117
|
-
# # change
|
|
118
|
-
# ]
|
|
119
|
-
# ]
|
|
120
|
-
|
|
121
269
|
patchset.each do |hunk|
|
|
122
270
|
case hunk
|
|
123
271
|
when Diff::LCS::Change, Diff::LCS::ContextChange
|
|
@@ -142,7 +290,7 @@ module Diff::LCS::Internals # :nodoc:
|
|
|
142
290
|
[has_changes, new_patchset]
|
|
143
291
|
end
|
|
144
292
|
|
|
145
|
-
# Examine the patchset and the source to see in which direction the patch should be
|
|
293
|
+
# Examine the `patchset` and the source to see in which direction the patch should be
|
|
146
294
|
# applied.
|
|
147
295
|
#
|
|
148
296
|
# WARNING: By default, this examines the whole patch, so this could take some time. This
|
|
@@ -244,43 +392,6 @@ module Diff::LCS::Internals # :nodoc:
|
|
|
244
392
|
end
|
|
245
393
|
|
|
246
394
|
class << self
|
|
247
|
-
# Find the place at which `value` would normally be inserted into the Enumerable. If
|
|
248
|
-
# that place is already occupied by `value`, do nothing and return `nil`. If the place
|
|
249
|
-
# does not exist (i.e., it is off the end of the Enumerable), add it to the end.
|
|
250
|
-
# Otherwise, replace the element at that point with `value`. It is assumed that the
|
|
251
|
-
# Enumerable's values are numeric.
|
|
252
|
-
#
|
|
253
|
-
# This operation preserves the sort order.
|
|
254
|
-
def replace_next_larger(enum, value, last_index = nil)
|
|
255
|
-
# Off the end?
|
|
256
|
-
if enum.empty? || (value > enum[-1])
|
|
257
|
-
enum << value
|
|
258
|
-
return enum.size - 1
|
|
259
|
-
end
|
|
260
|
-
|
|
261
|
-
# Binary search for the insertion point
|
|
262
|
-
last_index ||= enum.size - 1
|
|
263
|
-
first_index = 0
|
|
264
|
-
while first_index <= last_index
|
|
265
|
-
i = (first_index + last_index) >> 1
|
|
266
|
-
|
|
267
|
-
found = enum[i]
|
|
268
|
-
|
|
269
|
-
return nil if value == found
|
|
270
|
-
|
|
271
|
-
if value > found
|
|
272
|
-
first_index = i + 1
|
|
273
|
-
else
|
|
274
|
-
last_index = i - 1
|
|
275
|
-
end
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
# The insertion point is in first_index; overwrite the next larger value.
|
|
279
|
-
enum[first_index] = value
|
|
280
|
-
first_index
|
|
281
|
-
end
|
|
282
|
-
private :replace_next_larger
|
|
283
|
-
|
|
284
395
|
# If `vector` maps the matching elements of another collection onto this Enumerable,
|
|
285
396
|
# compute the inverse of `vector` that maps this Enumerable onto the collection.
|
|
286
397
|
# (Currently unused.)
|