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

halostatue / diff-lcs / 20625065018

31 Dec 2025 06:41PM UTC coverage: 91.336% (+3.0%) from 88.366%
20625065018

push

github

halostatue
chore: Prepare for release of diff-lcs 2.0 beta 1

Simplified build tooling, updated GitHub Actions workflows, prepared
changelog for dev release, and updated support for Ruby 3.2 through 4.x.

- Streamlined Rakefile and Gemfile and removed unnecessary complexity
- Added Ruby 4.0 support and dropped unsupported Ruby versions
- Simplified CI configuration
- Updated release and support documents.

Signed-off-by: Austin Ziegler <austin@zieglers.ca>

400 of 597 branches covered (67.0%)

1 of 1 new or added line in 1 file covered. (100.0%)

52 existing lines in 6 files now uncovered.

622 of 681 relevant lines covered (91.34%)

291.44 hits per line

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

97.5
/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 hard worked
8
# 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 I am not
11
# very confident about this) was the `longest common subsequence' method. In the LCS
12
# problem, you have two sequences of items:
13
#
14
# ```
15
# a b c d f g h j q z
16
# a b c d e f g i j k r x y z
17
# ```
18
#
19
# and you want to find the longest sequence of items that is present in both original
20
# sequences in the same order. That is, you want to find a new sequence *S* which can be
21
# obtained from the first sequence by deleting some items, and from the second sequence by
22
# deleting other items. You also want *S* to be as long as possible. In this case *S* is:
23
#
24
# ```
25
# a b c d f g j z
26
# ```
27
#
28
# From there it's only a small step to get diff-like output:
29
#
30
# ```
31
# e   h i   k   q r x y
32
# +   - +   +   - + + +
33
# ```
34
#
35
# This module solves the LCS problem. It also includes a canned function to generate
36
# `diff`-like output.
37
#
38
# It might seem from the example above that the LCS of two sequences is always pretty
39
# obvious, but that's not always the case, especially when the two sequences have many
40
# repeated elements. For example, consider
41
#
42
# ```
43
# a x b y c z p d q
44
# a b c a x b y c z
45
# ```
46
#
47
# A naive approach might start by matching up the `a` and `b` that appear at
48
# the beginning of each sequence, like this:
49
#
50
# ```
51
# a x b y c         z p d q
52
# a   b   c a b y c z
53
# ```
54
#
55
# This finds the common subsequence `a b c z`. But actually, the LCS is `a x b y c z`:
56
#
57
# ```
58
#       a x b y c z p d q
59
# a b c a x b y c z
60
# ```
61
module Diff::LCS
2✔
62
end
63

64
require "diff/lcs/version"
2✔
65
require "diff/lcs/callbacks"
2✔
66
require "diff/lcs/internals"
2✔
67

68
module Diff::LCS
2✔
69
  # Returns an Array containing the longest common subsequence(s) between `self` and
70
  # `other`. See Diff::LCS.lcs.
71
  def lcs(other, &block) = # :yields: self[i] if there are matched subsequences
2✔
72
    Diff::LCS.lcs(self, other, &block)
73

74
  # Returns the difference set between `self` and `other`. See Diff::LCS.diff.
75
  def diff(other, callbacks = nil, &block) = Diff::LCS.diff(self, other, callbacks, &block)
2✔
76

77
  # Returns the balanced ("side-by-side") difference set between `self` and `other`. See
78
  # Diff::LCS.sdiff.
79
  def sdiff(other, callbacks = nil, &block) = Diff::LCS.sdiff(self, other, callbacks, &block)
2✔
80

81
  # Traverses the discovered longest common subsequences between `self` and `other`. See
82
  # Diff::LCS.traverse_sequences.
83
  def traverse_sequences(other, callbacks = nil, &block) =
2✔
84
    Diff::LCS.traverse_sequences(self, other, callbacks || Diff::LCS::SequenceCallbacks, &block)
85

86
  # Traverses the discovered longest common subsequences between `self` and `other` using
87
  # the alternate, balanced algorithm. See Diff::LCS.traverse_balanced.
88
  def traverse_balanced(other, callbacks = nil, &block) =
2✔
89
    Diff::LCS.traverse_balanced(self, other, callbacks || Diff::LCS::BalancedCallbacks, &block)
90

91
  # Attempts to patch `self` with the provided `patchset`. A new sequence based on `self`
92
  # and the `patchset` will be created. See Diff::LCS.patch. Attempts to autodiscover the
93
  # direction of the patch.
94
  def patch(patchset) = Diff::LCS.patch(self, patchset)
2✔
95
  alias_method :unpatch, :patch
2✔
96

97
  # Attempts to patch `self` with the provided `patchset`. A new sequence based on `self`
98
  # and the `patchset` will be created. See Diff::LCS.patch!. Does no patch direction
99
  # autodiscovery.
100
  def patch!(patchset) = Diff::LCS.patch!(self, patchset)
2✔
101

102
  # Attempts to unpatch `self` with the provided `patchset`. A new sequence based on
103
  # `self` and the `patchset` will be created. See Diff::LCS.unpatch!. Does no patch
104
  # direction autodiscovery.
105
  def unpatch!(patchset) = Diff::LCS.unpatch!(self, patchset)
2✔
106

107
  # Attempts to patch `self` with the provided `patchset`, using #patch!. If the sequence
108
  # this is used on supports #replace, the value of `self` will be replaced. See
109
  # Diff::LCS.patch!. Does no patch direction autodiscovery.
110
  def patch_me(patchset)
2✔
111
    if respond_to? :replace
×
UNCOV
112
      replace(patch!(patchset))
×
113
    else
×
UNCOV
114
      patch!(patchset)
×
115
    end
116
  end
117

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

129
  # Returns an Array containing the longest common subsequence(s) between `seq` and
130
  # `seq2`.
131
  #
132
  # > NOTE on comparing objects: Diff::LCS only works properly when each object can be
133
  # > used as a key in a Hash. This means that those objects must implement the methods
134
  # > `#hash` and `#eql?` such that two objects containing identical values compare
135
  # > identically for key purposes. That is:
136
  # >
137
  # > ```
138
  # > O.new('a').eql?(O.new('a')) == true && O.new('a').hash == O.new('a').hash
139
  # > ```
140
  def self.lcs(seq1, seq2, &block) # :yields: seq1[i] for each matched
2✔
141
    matches = Diff::LCS::Internals.lcs(seq1, seq2)
8✔
142
    [].tap { |result|
8✔
143
      matches.each_index do
8✔
144
        next if matches[_1].nil?
54✔
145

146
        v = seq1[_1]
44✔
147
        v = block.call(v) if block
44!
148

149
        result << v
44✔
150
      end
151
    }
152
  end
153

154
  # `diff` computes the smallest set of additions and deletions necessary to turn the
155
  # first sequence into the second, and returns a description of these changes.
156
  #
157
  # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate behaviour may be
158
  # implemented with Diff::LCS::ContextDiffCallbacks. If the `callbacks` object responds
159
  # to #finish, it will be called.
160
  def self.diff(seq1, seq2, callbacks = nil, &block) = # :yields: diff changes
2✔
161
    diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks, &block)
162

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

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

273
    run_finished_a = run_finished_b = false
226✔
274

275
    a_size = seq1.size
226✔
276
    b_size = seq2.size
226✔
277
    a_i = b_j = 0
226✔
278

279
    matches.each do |b_line|
226✔
280
      if b_line.nil?
1,724✔
281
        unless seq1[a_i].nil?
288!
282
          a_x = seq1[a_i]
288✔
283
          b_x = seq2[b_j]
288✔
284

285
          event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
288✔
286
          event = yield event if block_given?
288!
287
          callbacks.discard_a(event)
288✔
288
        end
289
      else
718✔
290
        a_x = seq1[a_i]
1,436✔
291

292
        loop do
1,436✔
293
          break unless b_j < b_line
1,724✔
294

295
          b_x = seq2[b_j]
288✔
296
          event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
288✔
297
          event = yield event if block_given?
288!
298
          callbacks.discard_b(event)
288✔
299
          b_j += 1
288✔
300
        end
301
        b_x = seq2[b_j]
1,436✔
302
        event = Diff::LCS::ContextChange.new("=", a_i, a_x, b_j, b_x)
1,436✔
303
        event = yield event if block_given?
1,436!
304
        callbacks.match(event)
1,436✔
305
        b_j += 1
1,436✔
306
      end
307

308
      a_i += 1
1,724✔
309
    end
310

311
    # The last entry (if any) processed was a match. `a_i` and `b_j` point just past the
312
    # last matching lines in their sequences.
313
    while (a_i < a_size) || (b_j < b_size)
752✔
314
      # last A?
150✔
315
      if a_i == a_size && b_j < b_size
300✔
316
        if callbacks.respond_to?(:finished_a) && !run_finished_a
54✔
317
          a_x = seq1[-1]
10✔
318
          b_x = seq2[b_j]
10✔
319
          event = Diff::LCS::ContextChange.new(">", a_size - 1, a_x, b_j, b_x)
10✔
320
          event = yield event if block_given?
10!
321
          callbacks.finished_a(event)
10✔
322
          run_finished_a = true
10✔
323
        else
22✔
324
          a_x = seq1[a_i]
44✔
325
          loop do
44✔
326
            b_x = seq2[b_j]
56✔
327
            event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
56✔
328
            event = yield event if block_given?
56!
329
            callbacks.discard_b(event)
56✔
330
            b_j += 1
56✔
331
            break unless b_j < b_size
56✔
332
          end
333
        end
334
      end
335

336
      # last B?
337
      if b_j == b_size && a_i < a_size
300✔
338
        if callbacks.respond_to?(:finished_b) && !run_finished_b
52✔
339
          a_x = seq1[a_i]
10✔
340
          b_x = seq2[-1]
10✔
341
          event = Diff::LCS::ContextChange.new("<", a_i, a_x, b_size - 1, b_x)
10✔
342
          event = yield event if block_given?
10!
343
          callbacks.finished_b(event)
10✔
344
          run_finished_b = true
10✔
345
        else
21✔
346
          b_x = seq2[b_j]
42✔
347
          loop do
42✔
348
            a_x = seq1[a_i]
42✔
349
            event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
42✔
350
            event = yield event if block_given?
42!
351
            callbacks.discard_a(event)
42✔
352
            a_i += 1
42✔
353
            break unless b_j < b_size
42!
354
          end
355
        end
356
      end
357

358
      if a_i < a_size
300✔
359
        a_x = seq1[a_i]
208✔
360
        b_x = seq2[b_j]
208✔
361
        event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
208✔
362
        event = yield event if block_given?
208!
363
        callbacks.discard_a(event)
208✔
364
        a_i += 1
208✔
365
      end
366

367
      if b_j < b_size
300✔
368
        a_x = seq1[a_i]
204✔
369
        b_x = seq2[b_j]
204✔
370
        event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
204✔
371
        event = yield event if block_given?
204!
372
        callbacks.discard_b(event)
204✔
373
        b_j += 1
204✔
374
      end
375
    end
376
  end
377

378
  # #traverse_balanced is an alternative to #traverse_sequences. It uses a different
379
  # algorithm to iterate through the entries in the computed longest common subsequence.
380
  # Instead of viewing the changes as insertions or deletions from one of the sequences,
381
  # #traverse_balanced will report _changes_ between the sequences.
382
  #
383
  # The arguments to #traverse_balanced are the two sequences to traverse and a callback
384
  # object, like this:
385
  #
386
  # ```ruby
387
  # traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks)
388
  # ```
389
  #
390
  # #sdiff is implemented using #traverse_balanced.
391
  #
392
  # ### Callback Methods
393
  #
394
  # - `callbacks#match`: Called when `a` and `b` are pointing to common elements in `A`
395
  #   and `B`.
396
  # - `callbacks#discard_a`: Called when `a` is pointing to an element not in `B`.
397
  # - `callbacks#discard_b`: Called when `b` is pointing to an element not in `A`.
398
  # - `callbacks#change`: Called when `a` and `b` are pointing to the same relative
399
  #   position, but `A[a]` and `B[b]` are not the same; a _change_ has occurred. Optional.
400
  #
401
  # #traverse_balanced might be a bit slower than #traverse_sequences, noticeable only
402
  # while processing large amounts of data.
403
  #
404
  # ### Algorithm
405
  #
406
  # ```
407
  # a---+
408
  #     v
409
  # A = a b c e h j l m n p
410
  # B = b c d e f j k l m r s t
411
  #     ^
412
  # b---+
413
  # ```
414
  #
415
  # #### Matches
416
  #
417
  # If there are two arrows (`a` and `b`) pointing to elements of sequences `A` and `B`,
418
  # the arrows will initially point to the first elements of their respective sequences.
419
  # #traverse_sequences will advance the arrows through the sequences one element at
420
  # a time, calling a method on the user-specified callback object before each advance. It
421
  # will advance the arrows in such a way that if there are elements `A[i]` and
422
  # `B[j]` which are both equal and part of the longest common subsequence, there will be
423
  # some moment during the execution of #traverse_sequences when arrow `a` is pointing to
424
  # `A[i]` and arrow `b` is pointing to `B[j]`. When this happens, #traverse_sequences
425
  # will call `callbacks#match` and then it will advance both arrows.
426
  #
427
  # #### Discards
428
  #
429
  # Otherwise, one of the arrows is pointing to an element of its sequence that is not
430
  # part of the longest common subsequence. #traverse_sequences will advance that arrow
431
  # and will call `callbacks#discard_a` or `callbacks#discard_b`, depending on which arrow
432
  # it advanced.
433
  #
434
  # #### Changes
435
  #
436
  # If both `a` and `b` point to elements that are not part of the longest common
437
  # subsequence, then #traverse_sequences will try to call `callbacks#change` and advance
438
  # both arrows. If `callbacks#change` is not implemented, then `callbacks#discard_a` and
439
  # `callbacks#discard_b` will be called in turn.
440
  #
441
  # The methods for `callbacks#match`, `callbacks#discard_a`, `callbacks#discard_b`, and
442
  # `callbacks#change` are invoked with an event comprising the action ("=", "+", "-", or
443
  # "!", respectively), the indexes `i` and `j`, and the elements `A[i]` and `B[j]`.
444
  # Return values are discarded by #traverse_balanced.
445
  #
446
  # === Context
447
  #
448
  # Note that `i` and `j` may not be the same index position, even if `a` and `b` are
449
  # considered to be pointing to matching or changed elements.
450
  def self.traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
2✔
451
    matches = Diff::LCS::Internals.lcs(seq1, seq2)
308✔
452
    a_size = seq1.size
308✔
453
    b_size = seq2.size
308✔
454
    a_i = b_j = m_b = 0
308✔
455
    m_a = -1
308✔
456

457
    # Process all the lines in the match vector.
458
    loop do
308✔
459
      # Find next match indexes `m_a` and `m_b`
460
      loop do
1,616✔
461
        m_a += 1
1,960✔
462
        break unless m_a < matches.size && matches[m_a].nil?
1,960✔
463
      end
464

465
      break if m_a >= matches.size # end of matches?
1,616✔
466

467
      m_b = matches[m_a]
1,308✔
468

469
      # Change(seq2)
470
      while (a_i < m_a) || (b_j < m_b)
3,128✔
471
        a_x = seq1[a_i]
512✔
472
        b_x = seq2[b_j]
512✔
473

474
        case [(a_i < m_a), (b_j < m_b)]
512!
475
        when [true, true]
88✔
476
          if callbacks.respond_to?(:change)
176✔
477
            event = Diff::LCS::ContextChange.new("!", a_i, a_x, b_j, b_x)
104✔
478
            event = yield event if block_given?
104!
479
            callbacks.change(event)
104✔
480
            a_i += 1
104✔
481
          else
36✔
482
            event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
72✔
483
            event = yield event if block_given?
72!
484
            callbacks.discard_a(event)
72✔
485
            a_i += 1
72✔
486
            a_x = seq1[a_i]
72✔
487
            event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
72✔
488
            event = yield event if block_given?
72!
489
            callbacks.discard_b(event)
72✔
490
          end
491

492
          b_j += 1
176✔
493
        when [true, false]
84✔
494
          event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
168✔
495
          event = yield event if block_given?
168!
496
          callbacks.discard_a(event)
168✔
497
          a_i += 1
168✔
498
        when [false, true]
84✔
499
          event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
168✔
500
          event = yield event if block_given?
168!
501
          callbacks.discard_b(event)
168✔
502
          b_j += 1
168✔
503
        end
504
      end
505

506
      # Match
507
      a_x = seq1[a_i]
1,308✔
508
      b_x = seq2[b_j]
1,308✔
509
      event = Diff::LCS::ContextChange.new("=", a_i, a_x, b_j, b_x)
1,308✔
510
      event = yield event if block_given?
1,308!
511
      callbacks.match(event)
1,308✔
512
      a_i += 1
1,308✔
513
      b_j += 1
1,308✔
514
    end
515

516
    while (a_i < a_size) || (b_j < b_size)
1,200✔
517
      a_x = seq1[a_i]
584✔
518
      b_x = seq2[b_j]
584✔
519

520
      case [(a_i < a_size), (b_j < b_size)]
584!
521
      when [true, true]
88✔
522
        if callbacks.respond_to?(:change)
176✔
523
          event = Diff::LCS::ContextChange.new("!", a_i, a_x, b_j, b_x)
112✔
524
          event = yield event if block_given?
112!
525
          callbacks.change(event)
112✔
526
          a_i += 1
112✔
527
        else
32✔
528
          event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
64✔
529
          event = yield event if block_given?
64!
530
          callbacks.discard_a(event)
64✔
531
          a_i += 1
64✔
532
          a_x = seq1[a_i]
64✔
533
          event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
64✔
534
          event = yield event if block_given?
64!
535
          callbacks.discard_b(event)
64✔
536
        end
537

538
        b_j += 1
176✔
539
      when [true, false]
102✔
540
        event = Diff::LCS::ContextChange.new("-", a_i, a_x, b_j, b_x)
204✔
541
        event = yield event if block_given?
204!
542
        callbacks.discard_a(event)
204✔
543
        a_i += 1
204✔
544
      when [false, true]
102✔
545
        event = Diff::LCS::ContextChange.new("+", a_i, a_x, b_j, b_x)
204✔
546
        event = yield event if block_given?
204!
547
        callbacks.discard_b(event)
204✔
548
        b_j += 1
204✔
549
      end
550
    end
551
  end
552

553
  # standard:disable Style/HashSyntax
554
  PATCH_MAP = { # :nodoc:
555
    :patch => {"+" => "+", "-" => "-", "!" => "!", "=" => "="}.freeze,
2✔
556
    :unpatch => {"+" => "-", "-" => "+", "!" => "!", "=" => "="}.freeze
557
  }.freeze
558
  private_constant :PATCH_MAP
2✔
559
  # standard:enable Style/HashSyntax
560

561
  # Applies a `patchset` to the sequence `src` according to the `direction` (`:patch` or
562
  # `:unpatch`), producing a new sequence.
563
  #
564
  # If the `direction` is not specified, Diff::LCS::patch will attempt to discover the
565
  # direction of the `patchset`.
566
  #
567
  # A `patchset` can be considered to apply forward (`:patch`) if the following expression
568
  # is true:
569
  #
570
  # ```ruby
571
  # patch(s1, diff(s1, s2)) # => s2
572
  # ```
573
  #
574
  # A `patchset` can be considered to apply backward (`:unpatch`) if the following
575
  # expression is true:
576
  #
577
  # ```ruby
578
  # patch(s2, diff(s1, s2)) # => s1
579
  # ```
580
  #
581
  # If the `patchset` contains no changes, the `src` value will be returned as either
582
  # `src.dup` or `src`. A `patchset` can be deemed as having no changes if the following
583
  # predicate returns true:
584
  #
585
  # ```ruby
586
  # patchset.empty? or patchset.flatten(1).all? { |change| change.unchanged? }
587
  # ```
588
  #
589
  # ### Patchsets
590
  #
591
  # A `patchset` is always an enumerable sequence of changes, hunks of changes, or a mix
592
  # of the two. A hunk of changes is an enumerable sequence of changes:
593
  #
594
  # ```
595
  # [ # patchset
596
  #   # change
597
  #   [ # hunk
598
  #     # change
599
  #   ]
600
  # ]
601
  # ```
602
  #
603
  # The `patch` method accepts `patchset`s that are enumerable sequences containing either
604
  # Diff::LCS::Change objects (or a subclass) or the array representations of those
605
  # objects. Prior to application, array representations of Diff::LCS::Change objects will
606
  # be reified.
607
  def self.patch(src, patchset, direction = nil)
2✔
608
    # Normalize the patchset.
609
    has_changes, patchset = Diff::LCS::Internals.analyze_patchset(patchset)
314✔
610

611
    return src.respond_to?(:dup) ? src.dup : src unless has_changes
314!
612

613
    # Start with a new empty type of the source's class
614
    res = src.class.new
306✔
615

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

618
    a_i = b_j = 0
306✔
619

620
    patch_map = PATCH_MAP[direction]
306✔
621

622
    patchset.each do |change|
306✔
623
      # Both Change and ContextChange support #action
624
      action = patch_map[change.action]
2,292✔
625

626
      case change
2,292!
627
      when Diff::LCS::ContextChange
864✔
628
        case direction
1,728!
629
        when :patch
432✔
630
          el = change.new_element
864✔
631
          op = change.old_position
864✔
632
          np = change.new_position
864✔
633
        when :unpatch
432✔
634
          el = change.old_element
864✔
635
          op = change.new_position
864✔
636
          np = change.old_position
864✔
637
        end
638

639
        case action
1,728!
640
        when "-" # Remove details from the old string
192✔
641
          while a_i < op
384✔
642
            res << src[a_i]
204✔
643
            a_i += 1
204✔
644
            b_j += 1
204✔
645
          end
646
          a_i += 1
384✔
647
        when "+"
192✔
648
          while b_j < np
384✔
649
            res << src[a_i]
204✔
650
            a_i += 1
204✔
651
            b_j += 1
204✔
652
          end
653

654
          res << el
384✔
655
          b_j += 1
384✔
656
        when "="
657
          # This only appears in sdiff output with the SDiff callback.
658
          # Therefore, we only need to worry about dealing with a single
659
          # element.
408✔
660
          res << el
816✔
661

662
          a_i += 1
816✔
663
          b_j += 1
816✔
664
        when "!"
72✔
665
          while a_i < op
144✔
666
            res << src[a_i]
72✔
667
            a_i += 1
72✔
668
            b_j += 1
72✔
669
          end
670

671
          b_j += 1
144✔
672
          a_i += 1
144✔
673

674
          res << el
144✔
675
        end
676
      when Diff::LCS::Change
282✔
677
        case action
564!
678
        when "-"
140✔
679
          while a_i < change.position
280✔
680
            res << src[a_i]
240✔
681
            a_i += 1
240✔
682
            b_j += 1
240✔
683
          end
684
          a_i += 1
280✔
685
        when "+"
142✔
686
          while b_j < change.position
284✔
687
            res << src[a_i]
244✔
688
            a_i += 1
244✔
689
            b_j += 1
244✔
690
          end
691

692
          b_j += 1
284✔
693

694
          res << change.element
284✔
695
        end
696
      end
697
    end
698

699
    while a_i < src.size
306✔
700
      res << src[a_i]
704✔
701
      a_i += 1
704✔
702
      b_j += 1
704✔
703
    end
704

705
    res
306✔
706
  end
707

708
  # Given a patchset, convert the current version to the prior version. Does no
709
  # auto-discovery.
710
  def self.unpatch!(src, patchset) = patch(src, patchset, :unpatch)
2✔
711

712
  # Given a patchset, convert the current version to the next version. Does no
713
  # auto-discovery.
714
  def self.patch!(src, patchset) = patch(src, patchset, :patch)
2✔
715
end
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

© 2026 Coveralls, Inc