• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

halostatue / diff-lcs / 14435601054

14 Apr 2025 01:36AM UTC coverage: 88.598%. Remained the same
14435601054

Pull #137

github

web-flow
Merge 3682b8a78 into 092281f97
Pull Request #137: Bump ruby/setup-ruby from 1.229.0 to 1.230.0

533 of 797 branches covered (66.88%)

676 of 763 relevant lines covered (88.6%)

288.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.59
/lib/diff/lcs.rb
1
# frozen_string_literal: true
2

3
module Diff; end unless defined? Diff
2!
4

5
# == How Diff Works (by Mark-Jason Dominus)
6
#
7
# I once read an article written by the authors of +diff+; they said that they
8
# hard worked very hard on the algorithm until they found the right one.
9
#
10
# I think what they ended up using (and I hope someone will correct me, because
11
# I am not very confident about this) was the `longest common subsequence'
12
# method. In the LCS problem, you have two sequences of items:
13
#
14
#    a b c d f g h j q z
15
#    a b c d e f g i j k r x y z
16
#
17
# and you want to find the longest sequence of items that is present in both
18
# original sequences in the same order. That is, you want to find a new
19
# sequence *S* which can be obtained from the first sequence by deleting some
20
# items, and from the second sequence by deleting other items. You also want
21
# *S* to be as long as possible. In this case *S* is:
22
#
23
#    a b c d f g j z
24
#
25
# From there it's only a small step to get diff-like output:
26
#
27
#    e   h i   k   q r x y
28
#    +   - +   +   - + + +
29
#
30
# This module solves the LCS problem. It also includes a canned function to
31
# generate +diff+-like output.
32
#
33
# It might seem from the example above that the LCS of two sequences is always
34
# pretty obvious, but that's not always the case, especially when the two
35
# sequences have many repeated elements. For example, consider
36
#
37
#    a x b y c z p d q
38
#    a b c a x b y c z
39
#
40
# A naive approach might start by matching up the +a+ and +b+ that appear at
41
# the beginning of each sequence, like this:
42
#
43
#    a x b y c         z p d q
44
#    a   b   c a b y c z
45
#
46
# This finds the common subsequence +a b c z+. But actually, the LCS is +a x b
47
# y c z+:
48
#
49
#          a x b y c z p d q
50
#    a b c a x b y c z
51
module Diff::LCS
2✔
52
end
53

54
require "diff/lcs/version"
2✔
55
require "diff/lcs/callbacks"
2✔
56
require "diff/lcs/internals"
2✔
57

58
module Diff::LCS
2✔
59
  # Returns an Array containing the longest common subsequence(s) between
60
  # +self+ and +other+. See Diff::LCS#lcs.
61
  #
62
  #   lcs = seq1.lcs(seq2)
63
  #
64
  # A note when using objects: Diff::LCS only works properly when each object
65
  # can be used as a key in a Hash. This means that those objects must implement
66
  # the methods +#hash+ and +#eql?+ such that two objects containing identical values
67
  # compare identically for key purposes. That is:
68
  #
69
  #   O.new('a').eql?(O.new('a')) == true &&
70
  #   O.new('a').hash == O.new('a').hash
71
  def lcs(other, &block) # :yields: self[i] if there are matched subsequences
2✔
72
    Diff::LCS.lcs(self, other, &block)
×
73
  end
74

75
  # Returns the difference set between +self+ and +other+. See Diff::LCS#diff.
76
  def diff(other, callbacks = nil, &block)
2✔
77
    Diff::LCS.diff(self, other, callbacks, &block)
×
78
  end
79

80
  # Returns the balanced ("side-by-side") difference set between +self+ and
81
  # +other+. See Diff::LCS#sdiff.
82
  def sdiff(other, callbacks = nil, &block)
2✔
83
    Diff::LCS.sdiff(self, other, callbacks, &block)
×
84
  end
85

86
  # Traverses the discovered longest common subsequences between +self+ and
87
  # +other+. See Diff::LCS#traverse_sequences.
88
  def traverse_sequences(other, callbacks = nil, &block)
2✔
89
    Diff::LCS.traverse_sequences(self, other, callbacks || Diff::LCS::SequenceCallbacks, &block)
×
90
  end
91

92
  # Traverses the discovered longest common subsequences between +self+ and
93
  # +other+ using the alternate, balanced algorithm. See
94
  # Diff::LCS#traverse_balanced.
95
  def traverse_balanced(other, callbacks = nil, &block)
2✔
96
    Diff::LCS.traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
×
97
  end
98

99
  # Attempts to patch +self+ with the provided +patchset+. A new sequence based
100
  # on +self+ and the +patchset+ will be created. See Diff::LCS#patch. Attempts
101
  # to autodiscover the direction of the patch.
102
  def patch(patchset)
2✔
103
    Diff::LCS.patch(self, patchset)
×
104
  end
105
  alias_method :unpatch, :patch
2✔
106

107
  # Attempts to patch +self+ with the provided +patchset+. A new sequence based
108
  # on +self+ and the +patchset+ will be created. See Diff::LCS#patch. Does no
109
  # patch direction autodiscovery.
110
  def patch!(patchset)
2✔
111
    Diff::LCS.patch!(self, patchset)
×
112
  end
113

114
  # Attempts to unpatch +self+ with the provided +patchset+. A new sequence
115
  # based on +self+ and the +patchset+ will be created. See Diff::LCS#unpatch.
116
  # Does no patch direction autodiscovery.
117
  def unpatch!(patchset)
2✔
118
    Diff::LCS.unpatch!(self, patchset)
×
119
  end
120

121
  # Attempts to patch +self+ with the provided +patchset+, using #patch!. If
122
  # the sequence this is used on supports #replace, the value of +self+ will be
123
  # replaced. See Diff::LCS#patch. Does no patch direction autodiscovery.
124
  def patch_me(patchset)
2✔
125
    if respond_to? :replace
×
126
      replace(patch!(patchset))
×
127
    else
×
128
      patch!(patchset)
×
129
    end
130
  end
131

132
  # Attempts to unpatch +self+ with the provided +patchset+, using #unpatch!.
133
  # If the sequence this is used on supports #replace, the value of +self+ will
134
  # be replaced. See Diff::LCS#unpatch. Does no patch direction autodiscovery.
135
  def unpatch_me(patchset)
2✔
136
    if respond_to? :replace
×
137
      replace(unpatch!(patchset))
×
138
    else
×
139
      unpatch!(patchset)
×
140
    end
141
  end
142
end
143

144
class << Diff::LCS
2✔
145
  def lcs(seq1, seq2, &block) # :yields: seq1[i] for each matched
2✔
146
    matches = Diff::LCS::Internals.lcs(seq1, seq2)
8✔
147
    ret = []
8✔
148
    string = seq1.is_a? String
8✔
149
    matches.each_index do |i|
8✔
150
      next if matches[i].nil?
54✔
151

152
      v = string ? seq1[i, 1] : seq1[i]
44✔
153
      v = block[v] if block
44!
154
      ret << v
44✔
155
    end
156
    ret
8✔
157
  end
158
  alias_method :LCS, :lcs
2✔
159

160
  # #diff computes the smallest set of additions and deletions necessary to
161
  # turn the first sequence into the second, and returns a description of these
162
  # changes.
163
  #
164
  # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate
165
  # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If a
166
  # Class argument is provided for +callbacks+, #diff will attempt to
167
  # initialise it. If the +callbacks+ object (possibly initialised) responds to
168
  # #finish, it will be called.
169
  def diff(seq1, seq2, callbacks = nil, &block) # :yields: diff changes
2✔
170
    diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks, &block)
168✔
171
  end
172

173
  # #sdiff computes all necessary components to show two sequences and their
174
  # minimized differences side by side, just like the Unix utility
175
  # <em>sdiff</em> does:
176
  #
177
  #     old        <     -
178
  #     same             same
179
  #     before     |     after
180
  #     -          >     new
181
  #
182
  # See Diff::LCS::SDiffCallbacks for the default behaviour. An alternate
183
  # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If a
184
  # Class argument is provided for +callbacks+, #diff will attempt to
185
  # initialise it. If the +callbacks+ object (possibly initialised) responds to
186
  # #finish, it will be called.
187
  #
188
  # Each element of a returned array is a Diff::LCS::ContextChange object,
189
  # which can be implicitly converted to an array.
190
  #
191
  #   Diff::LCS.sdiff(a, b).each do |action, (old_pos, old_element), (new_pos, new_element)|
192
  #     case action
193
  #     when '!'
194
  #       # replace
195
  #     when '-'
196
  #       # delete
197
  #     when '+'
198
  #       # insert
199
  #     end
200
  #   end
201
  def sdiff(seq1, seq2, callbacks = nil, &block) # :yields: diff changes
2✔
202
    diff_traversal(:sdiff, seq1, seq2, callbacks || Diff::LCS::SDiffCallbacks, &block)
164✔
203
  end
204

205
  # #traverse_sequences is the most general facility provided by this module;
206
  # #diff and #lcs are implemented as calls to it.
207
  #
208
  # The arguments to #traverse_sequences are the two sequences to traverse, and
209
  # a callback object, like this:
210
  #
211
  #   traverse_sequences(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
212
  #
213
  # == Callback Methods
214
  #
215
  # Optional callback methods are <em>emphasized</em>.
216
  #
217
  # callbacks#match::               Called when +a+ and +b+ are pointing to
218
  #                                 common elements in +A+ and +B+.
219
  # callbacks#discard_a::           Called when +a+ is pointing to an
220
  #                                 element not in +B+.
221
  # callbacks#discard_b::           Called when +b+ is pointing to an
222
  #                                 element not in +A+.
223
  # <em>callbacks#finished_a</em>:: Called when +a+ has reached the end of
224
  #                                 sequence +A+.
225
  # <em>callbacks#finished_b</em>:: Called when +b+ has reached the end of
226
  #                                 sequence +B+.
227
  #
228
  # == Algorithm
229
  #
230
  #       a---+
231
  #           v
232
  #       A = a b c e h j l m n p
233
  #       B = b c d e f j k l m r s t
234
  #           ^
235
  #       b---+
236
  #
237
  # If there are two arrows (+a+ and +b+) pointing to elements of sequences +A+
238
  # and +B+, the arrows will initially point to the first elements of their
239
  # respective sequences. #traverse_sequences will advance the arrows through
240
  # the sequences one element at a time, calling a method on the user-specified
241
  # callback object before each advance. It will advance the arrows in such a
242
  # way that if there are elements <tt>A[i]</tt> and <tt>B[j]</tt> which are
243
  # both equal and part of the longest common subsequence, there will be some
244
  # moment during the execution of #traverse_sequences when arrow +a+ is
245
  # pointing to <tt>A[i]</tt> and arrow +b+ is pointing to <tt>B[j]</tt>. When
246
  # this happens, #traverse_sequences will call <tt>callbacks#match</tt> and
247
  # then it will advance both arrows.
248
  #
249
  # Otherwise, one of the arrows is pointing to an element of its sequence that
250
  # is not part of the longest common subsequence. #traverse_sequences will
251
  # advance that arrow and will call <tt>callbacks#discard_a</tt> or
252
  # <tt>callbacks#discard_b</tt>, depending on which arrow it advanced. If both
253
  # arrows point to elements that are not part of the longest common
254
  # subsequence, then #traverse_sequences will advance arrow +a+ and call the
255
  # appropriate callback, then it will advance arrow +b+ and call the appropriate
256
  # callback.
257
  #
258
  # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>, and
259
  # <tt>callbacks#discard_b</tt> are invoked with an event comprising the
260
  # action ("=", "+", or "-", respectively), the indexes +i+ and +j+, and the
261
  # elements <tt>A[i]</tt> and <tt>B[j]</tt>. Return values are discarded by
262
  # #traverse_sequences.
263
  #
264
  # === End of Sequences
265
  #
266
  # If arrow +a+ reaches the end of its sequence before arrow +b+ does,
267
  # #traverse_sequence will try to call <tt>callbacks#finished_a</tt> with the
268
  # last index and element of +A+ (<tt>A[-1]</tt>) and the current index and
269
  # element of +B+ (<tt>B[j]</tt>). If <tt>callbacks#finished_a</tt> does not
270
  # exist, then <tt>callbacks#discard_b</tt> will be called on each element of
271
  # +B+ until the end of the sequence is reached (the call will be done with
272
  # <tt>A[-1]</tt> and <tt>B[j]</tt> for each element).
273
  #
274
  # If +b+ reaches the end of +B+ before +a+ reaches the end of +A+,
275
  # <tt>callbacks#finished_b</tt> will be called with the current index and
276
  # element of +A+ (<tt>A[i]</tt>) and the last index and element of +B+
277
  # (<tt>A[-1]</tt>). Again, if <tt>callbacks#finished_b</tt> does not exist on
278
  # the callback object, then <tt>callbacks#discard_a</tt> will be called on
279
  # each element of +A+ until the end of the sequence is reached (<tt>A[i]</tt>
280
  # and <tt>B[-1]</tt>).
281
  #
282
  # There is a chance that one additional <tt>callbacks#discard_a</tt> or
283
  # <tt>callbacks#discard_b</tt> will be called after the end of the sequence
284
  # is reached, if +a+ has not yet reached the end of +A+ or +b+ has not yet
285
  # reached the end of +B+.
286
  def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks) # :yields: change events
2✔
287
    callbacks ||= Diff::LCS::SequenceCallbacks
228✔
288
    matches = Diff::LCS::Internals.lcs(seq1, seq2)
228✔
289

290
    run_finished_a = run_finished_b = false
228✔
291
    string = seq1.is_a?(String)
228✔
292

293
    a_size = seq1.size
228✔
294
    b_size = seq2.size
228✔
295
    ai = bj = 0
228✔
296

297
    matches.each do |b_line|
228✔
298
      if b_line.nil?
1,724✔
299
        unless seq1[ai].nil?
288!
300
          ax = string ? seq1[ai, 1] : seq1[ai]
288✔
301
          bx = string ? seq2[bj, 1] : seq2[bj]
288✔
302

303
          event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
288✔
304
          event = yield event if block_given?
288!
305
          callbacks.discard_a(event)
288✔
306
        end
307
      else
718✔
308
        ax = string ? seq1[ai, 1] : seq1[ai]
1,436✔
309

310
        loop do
1,436✔
311
          break unless bj < b_line
1,724✔
312

313
          bx = string ? seq2[bj, 1] : seq2[bj]
288✔
314
          event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
288✔
315
          event = yield event if block_given?
288!
316
          callbacks.discard_b(event)
288✔
317
          bj += 1
288✔
318
        end
319
        bx = string ? seq2[bj, 1] : seq2[bj]
1,436✔
320
        event = Diff::LCS::ContextChange.new("=", ai, ax, bj, bx)
1,436✔
321
        event = yield event if block_given?
1,436!
322
        callbacks.match(event)
1,436✔
323
        bj += 1
1,436✔
324
      end
325
      ai += 1
1,724✔
326
    end
327

328
    # The last entry (if any) processed was a match. +ai+ and +bj+ point just
329
    # past the last matching lines in their sequences.
330
    while (ai < a_size) || (bj < b_size)
758✔
331
      # last A?
151✔
332
      if ai == a_size && bj < b_size
302✔
333
        if callbacks.respond_to?(:finished_a) && !run_finished_a
54✔
334
          ax = string ? seq1[-1, 1] : seq1[-1]
10!
335
          bx = string ? seq2[bj, 1] : seq2[bj]
10!
336
          event = Diff::LCS::ContextChange.new(">", (a_size - 1), ax, bj, bx)
10✔
337
          event = yield event if block_given?
10!
338
          callbacks.finished_a(event)
10✔
339
          run_finished_a = true
10✔
340
        else
22✔
341
          ax = string ? seq1[ai, 1] : seq1[ai]
44✔
342
          loop do
44✔
343
            bx = string ? seq2[bj, 1] : seq2[bj]
56✔
344
            event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
56✔
345
            event = yield event if block_given?
56!
346
            callbacks.discard_b(event)
56✔
347
            bj += 1
56✔
348
            break unless bj < b_size
56✔
349
          end
350
        end
351
      end
352

353
      # last B?
354
      if bj == b_size && ai < a_size
302✔
355
        if callbacks.respond_to?(:finished_b) && !run_finished_b
52✔
356
          ax = string ? seq1[ai, 1] : seq1[ai]
10!
357
          bx = string ? seq2[-1, 1] : seq2[-1]
10!
358
          event = Diff::LCS::ContextChange.new("<", ai, ax, (b_size - 1), bx)
10✔
359
          event = yield event if block_given?
10!
360
          callbacks.finished_b(event)
10✔
361
          run_finished_b = true
10✔
362
        else
21✔
363
          bx = string ? seq2[bj, 1] : seq2[bj]
42!
364
          loop do
42✔
365
            ax = string ? seq1[ai, 1] : seq1[ai]
42!
366
            event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
42✔
367
            event = yield event if block_given?
42!
368
            callbacks.discard_a(event)
42✔
369
            ai += 1
42✔
370
            break unless bj < b_size
42!
371
          end
372
        end
373
      end
374

375
      if ai < a_size
302✔
376
        ax = string ? seq1[ai, 1] : seq1[ai]
210!
377
        bx = string ? seq2[bj, 1] : seq2[bj]
210!
378
        event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
210✔
379
        event = yield event if block_given?
210!
380
        callbacks.discard_a(event)
210✔
381
        ai += 1
210✔
382
      end
383

384
      if bj < b_size
302✔
385
        ax = string ? seq1[ai, 1] : seq1[ai]
206!
386
        bx = string ? seq2[bj, 1] : seq2[bj]
206!
387
        event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
206✔
388
        event = yield event if block_given?
206!
389
        callbacks.discard_b(event)
206✔
390
        bj += 1
206✔
391
      end
392
    end
393
  end
394

395
  # #traverse_balanced is an alternative to #traverse_sequences. It uses a
396
  # different algorithm to iterate through the entries in the computed longest
397
  # common subsequence. Instead of viewing the changes as insertions or
398
  # deletions from one of the sequences, #traverse_balanced will report
399
  # <em>changes</em> between the sequences.
400
  #
401
  # The arguments to #traverse_balanced are the two sequences to traverse and a
402
  # callback object, like this:
403
  #
404
  #   traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
405
  #
406
  # #sdiff is implemented with #traverse_balanced.
407
  #
408
  # == Callback Methods
409
  #
410
  # Optional callback methods are <em>emphasized</em>.
411
  #
412
  # callbacks#match::               Called when +a+ and +b+ are pointing to
413
  #                                 common elements in +A+ and +B+.
414
  # callbacks#discard_a::           Called when +a+ is pointing to an
415
  #                                 element not in +B+.
416
  # callbacks#discard_b::           Called when +b+ is pointing to an
417
  #                                 element not in +A+.
418
  # <em>callbacks#change</em>::     Called when +a+ and +b+ are pointing to
419
  #                                 the same relative position, but
420
  #                                 <tt>A[a]</tt> and <tt>B[b]</tt> are not
421
  #                                 the same; a <em>change</em> has
422
  #                                 occurred.
423
  #
424
  # #traverse_balanced might be a bit slower than #traverse_sequences,
425
  # noticeable only while processing huge amounts of data.
426
  #
427
  # == Algorithm
428
  #
429
  #       a---+
430
  #           v
431
  #       A = a b c e h j l m n p
432
  #       B = b c d e f j k l m r s t
433
  #           ^
434
  #       b---+
435
  #
436
  # === Matches
437
  #
438
  # If there are two arrows (+a+ and +b+) pointing to elements of sequences +A+
439
  # and +B+, the arrows will initially point to the first elements of their
440
  # respective sequences. #traverse_sequences will advance the arrows through
441
  # the sequences one element at a time, calling a method on the user-specified
442
  # callback object before each advance. It will advance the arrows in such a
443
  # way that if there are elements <tt>A[i]</tt> and <tt>B[j]</tt> which are
444
  # both equal and part of the longest common subsequence, there will be some
445
  # moment during the execution of #traverse_sequences when arrow +a+ is
446
  # pointing to <tt>A[i]</tt> and arrow +b+ is pointing to <tt>B[j]</tt>. When
447
  # this happens, #traverse_sequences will call <tt>callbacks#match</tt> and
448
  # then it will advance both arrows.
449
  #
450
  # === Discards
451
  #
452
  # Otherwise, one of the arrows is pointing to an element of its sequence that
453
  # is not part of the longest common subsequence. #traverse_sequences will
454
  # advance that arrow and will call <tt>callbacks#discard_a</tt> or
455
  # <tt>callbacks#discard_b</tt>, depending on which arrow it advanced.
456
  #
457
  # === Changes
458
  #
459
  # If both +a+ and +b+ point to elements that are not part of the longest
460
  # common subsequence, then #traverse_sequences will try to call
461
  # <tt>callbacks#change</tt> and advance both arrows. If
462
  # <tt>callbacks#change</tt> is not implemented, then
463
  # <tt>callbacks#discard_a</tt> and <tt>callbacks#discard_b</tt> will be
464
  # called in turn.
465
  #
466
  # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
467
  # <tt>callbacks#discard_b</tt>, and <tt>callbacks#change</tt> are invoked
468
  # with an event comprising the action ("=", "+", "-", or "!", respectively),
469
  # the indexes +i+ and +j+, and the elements <tt>A[i]</tt> and <tt>B[j]</tt>.
470
  # Return values are discarded by #traverse_balanced.
471
  #
472
  # === Context
473
  #
474
  # Note that +i+ and +j+ may not be the same index position, even if +a+ and
475
  # +b+ are considered to be pointing to matching or changed elements.
476
  def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
2✔
477
    matches = Diff::LCS::Internals.lcs(seq1, seq2)
308✔
478
    a_size = seq1.size
308✔
479
    b_size = seq2.size
308✔
480
    ai = bj = mb = 0
308✔
481
    ma = -1
308✔
482
    string = seq1.is_a?(String)
308✔
483

484
    # Process all the lines in the match vector.
485
    loop do
308✔
486
      # Find next match indexes +ma+ and +mb+
487
      loop do
1,616✔
488
        ma += 1
1,960✔
489
        break unless ma < matches.size && matches[ma].nil?
1,960✔
490
      end
491

492
      break if ma >= matches.size # end of matches?
1,616✔
493

494
      mb = matches[ma]
1,308✔
495

496
      # Change(seq2)
497
      while (ai < ma) || (bj < mb)
3,128✔
498
        ax = string ? seq1[ai, 1] : seq1[ai]
512✔
499
        bx = string ? seq2[bj, 1] : seq2[bj]
512✔
500

501
        case [(ai < ma), (bj < mb)]
512!
502
        when [true, true]
88✔
503
          if callbacks.respond_to?(:change)
176✔
504
            event = Diff::LCS::ContextChange.new("!", ai, ax, bj, bx)
104✔
505
            event = yield event if block_given?
104!
506
            callbacks.change(event)
104✔
507
            ai += 1
104✔
508
          else
36✔
509
            event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
72✔
510
            event = yield event if block_given?
72!
511
            callbacks.discard_a(event)
72✔
512
            ai += 1
72✔
513
            ax = string ? seq1[ai, 1] : seq1[ai]
72✔
514
            event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
72✔
515
            event = yield event if block_given?
72!
516
            callbacks.discard_b(event)
72✔
517
          end
518

519
          bj += 1
176✔
520
        when [true, false]
84✔
521
          event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
168✔
522
          event = yield event if block_given?
168!
523
          callbacks.discard_a(event)
168✔
524
          ai += 1
168✔
525
        when [false, true]
84✔
526
          event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
168✔
527
          event = yield event if block_given?
168!
528
          callbacks.discard_b(event)
168✔
529
          bj += 1
168✔
530
        end
531
      end
532

533
      # Match
534
      ax = string ? seq1[ai, 1] : seq1[ai]
1,308✔
535
      bx = string ? seq2[bj, 1] : seq2[bj]
1,308✔
536
      event = Diff::LCS::ContextChange.new("=", ai, ax, bj, bx)
1,308✔
537
      event = yield event if block_given?
1,308!
538
      callbacks.match(event)
1,308✔
539
      ai += 1
1,308✔
540
      bj += 1
1,308✔
541
    end
542

543
    while (ai < a_size) || (bj < b_size)
1,200✔
544
      ax = string ? seq1[ai, 1] : seq1[ai]
584✔
545
      bx = string ? seq2[bj, 1] : seq2[bj]
584✔
546

547
      case [(ai < a_size), (bj < b_size)]
584!
548
      when [true, true]
88✔
549
        if callbacks.respond_to?(:change)
176✔
550
          event = Diff::LCS::ContextChange.new("!", ai, ax, bj, bx)
112✔
551
          event = yield event if block_given?
112!
552
          callbacks.change(event)
112✔
553
          ai += 1
112✔
554
        else
32✔
555
          event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
64✔
556
          event = yield event if block_given?
64!
557
          callbacks.discard_a(event)
64✔
558
          ai += 1
64✔
559
          ax = string ? seq1[ai, 1] : seq1[ai]
64✔
560
          event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
64✔
561
          event = yield event if block_given?
64!
562
          callbacks.discard_b(event)
64✔
563
        end
564

565
        bj += 1
176✔
566
      when [true, false]
102✔
567
        event = Diff::LCS::ContextChange.new("-", ai, ax, bj, bx)
204✔
568
        event = yield event if block_given?
204!
569
        callbacks.discard_a(event)
204✔
570
        ai += 1
204✔
571
      when [false, true]
102✔
572
        event = Diff::LCS::ContextChange.new("+", ai, ax, bj, bx)
204✔
573
        event = yield event if block_given?
204!
574
        callbacks.discard_b(event)
204✔
575
        bj += 1
204✔
576
      end
577
    end
578
  end
579

580
  # standard:disable Style/HashSyntax
581
  PATCH_MAP = { # :nodoc:
582
    :patch => {"+" => "+", "-" => "-", "!" => "!", "=" => "="}.freeze,
2✔
583
    :unpatch => {"+" => "-", "-" => "+", "!" => "!", "=" => "="}.freeze
584
  }.freeze
585
  # standard:enable Style/HashSyntax
586

587
  # Applies a +patchset+ to the sequence +src+ according to the +direction+
588
  # (<tt>:patch</tt> or <tt>:unpatch</tt>), producing a new sequence.
589
  #
590
  # If the +direction+ is not specified, Diff::LCS::patch will attempt to
591
  # discover the direction of the +patchset+.
592
  #
593
  # A +patchset+ can be considered to apply forward (<tt>:patch</tt>) if the
594
  # following expression is true:
595
  #
596
  #     patch(s1, diff(s1, s2)) -> s2
597
  #
598
  # A +patchset+ can be considered to apply backward (<tt>:unpatch</tt>) if the
599
  # following expression is true:
600
  #
601
  #     patch(s2, diff(s1, s2)) -> s1
602
  #
603
  # If the +patchset+ contains no changes, the +src+ value will be returned as
604
  # either <tt>src.dup</tt> or +src+. A +patchset+ can be deemed as having no
605
  # changes if the following predicate returns true:
606
  #
607
  #     patchset.empty? or
608
  #       patchset.flatten(1).all? { |change| change.unchanged? }
609
  #
610
  # === Patchsets
611
  #
612
  # A +patchset+ is always an enumerable sequence of changes, hunks of changes,
613
  # or a mix of the two. A hunk of changes is an enumerable sequence of
614
  # changes:
615
  #
616
  #     [ # patchset
617
  #       # change
618
  #       [ # hunk
619
  #         # change
620
  #       ]
621
  #     ]
622
  #
623
  # The +patch+ method accepts <tt>patchset</tt>s that are enumerable sequences
624
  # containing either Diff::LCS::Change objects (or a subclass) or the array
625
  # representations of those objects. Prior to application, array
626
  # representations of Diff::LCS::Change objects will be reified.
627
  def patch(src, patchset, direction = nil)
2✔
628
    # Normalize the patchset.
629
    has_changes, patchset = Diff::LCS::Internals.analyze_patchset(patchset)
314✔
630

631
    return src.respond_to?(:dup) ? src.dup : src unless has_changes
314!
632

633
    string = src.is_a?(String)
306✔
634
    # Start with a new empty type of the source's class
635
    res = src.class.new
306✔
636

637
    direction ||= Diff::LCS::Internals.intuit_diff_direction(src, patchset)
306✔
638

639
    ai = bj = 0
306✔
640

641
    patch_map = PATCH_MAP[direction]
306✔
642

643
    patchset.each do |change|
306✔
644
      # Both Change and ContextChange support #action
645
      action = patch_map[change.action]
2,292✔
646

647
      case change
2,292!
648
      when Diff::LCS::ContextChange
864✔
649
        case direction
1,728!
650
        when :patch
432✔
651
          el = change.new_element
864✔
652
          op = change.old_position
864✔
653
          np = change.new_position
864✔
654
        when :unpatch
432✔
655
          el = change.old_element
864✔
656
          op = change.new_position
864✔
657
          np = change.old_position
864✔
658
        end
659

660
        case action
1,728!
661
        when "-" # Remove details from the old string
192✔
662
          while ai < op
384✔
663
            res << (string ? src[ai, 1] : src[ai])
204!
664
            ai += 1
204✔
665
            bj += 1
204✔
666
          end
667
          ai += 1
384✔
668
        when "+"
192✔
669
          while bj < np
384✔
670
            res << (string ? src[ai, 1] : src[ai])
204!
671
            ai += 1
204✔
672
            bj += 1
204✔
673
          end
674

675
          res << el
384✔
676
          bj += 1
384✔
677
        when "="
678
          # This only appears in sdiff output with the SDiff callback.
679
          # Therefore, we only need to worry about dealing with a single
680
          # element.
408✔
681
          res << el
816✔
682

683
          ai += 1
816✔
684
          bj += 1
816✔
685
        when "!"
72✔
686
          while ai < op
144✔
687
            res << (string ? src[ai, 1] : src[ai])
72!
688
            ai += 1
72✔
689
            bj += 1
72✔
690
          end
691

692
          bj += 1
144✔
693
          ai += 1
144✔
694

695
          res << el
144✔
696
        end
697
      when Diff::LCS::Change
282✔
698
        case action
564!
699
        when "-"
140✔
700
          while ai < change.position
280✔
701
            res << (string ? src[ai, 1] : src[ai])
240!
702
            ai += 1
240✔
703
            bj += 1
240✔
704
          end
705
          ai += 1
280✔
706
        when "+"
142✔
707
          while bj < change.position
284✔
708
            res << (string ? src[ai, 1] : src[ai])
244✔
709
            ai += 1
244✔
710
            bj += 1
244✔
711
          end
712

713
          bj += 1
284✔
714

715
          res << change.element
284✔
716
        end
717
      end
718
    end
719

720
    while ai < src.size
306✔
721
      res << (string ? src[ai, 1] : src[ai])
704✔
722
      ai += 1
704✔
723
      bj += 1
704✔
724
    end
725

726
    res
306✔
727
  end
728

729
  # Given a set of patchset, convert the current version to the prior version.
730
  # Does no auto-discovery.
731
  def unpatch!(src, patchset)
2✔
732
    patch(src, patchset, :unpatch)
48✔
733
  end
734

735
  # Given a set of patchset, convert the current version to the next version.
736
  # Does no auto-discovery.
737
  def patch!(src, patchset)
2✔
738
    patch(src, patchset, :patch)
48✔
739
  end
740
end
741

742
require "diff/lcs/backports"
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc