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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +173 -86
  3. data/CODE_OF_CONDUCT.md +155 -116
  4. data/CONTRIBUTING.md +29 -28
  5. data/CONTRIBUTORS.md +15 -11
  6. data/LICENCE.md +19 -38
  7. data/Manifest.txt +16 -3
  8. data/README.md +37 -26
  9. data/Rakefile +3 -0
  10. data/SECURITY.md +7 -7
  11. data/lib/diff/lcs/array.rb +3 -1
  12. data/lib/diff/lcs/callbacks.rb +1 -1
  13. data/lib/diff/lcs/change.rb +74 -32
  14. data/lib/diff/lcs/frozen_value.rb +74 -0
  15. data/lib/diff/lcs/hunk.rb +146 -58
  16. data/lib/diff/lcs/internals.rb +183 -72
  17. data/lib/diff/lcs/ldiff.rb +23 -45
  18. data/lib/diff/lcs/string.rb +3 -1
  19. data/lib/diff/lcs/version.rb +1 -1
  20. data/lib/diff/lcs.rb +29 -26
  21. data/licences/MIT.txt +19 -0
  22. data/spec/ldiff_spec.rb +9 -7
  23. data/test/fixtures/ldiff/output.diff.bin1-q +0 -0
  24. data/test/fixtures/ldiff/output.diff.bin2-q +1 -0
  25. data/test/fixtures/ldiff/output.diff.hunks +9 -0
  26. data/test/fixtures/ldiff/output.diff.hunks-c +35 -0
  27. data/test/fixtures/ldiff/output.diff.hunks-u +20 -0
  28. data/test/fixtures/ldiff/output.diff.same +0 -0
  29. data/test/fixtures/ldiff/output.diff.same-c +0 -0
  30. data/test/fixtures/ldiff/output.diff.same-u +0 -0
  31. data/test/fixtures/new-hunks +31 -0
  32. data/test/fixtures/old-hunks +30 -0
  33. data/test/test_change.rb +23 -19
  34. data/test/test_frozen_value.rb +155 -0
  35. data/test/test_helper.rb +5 -0
  36. data/test/test_hunk.rb +238 -0
  37. data/test/test_lcs.rb +147 -0
  38. data/test/test_ldiff.rb +64 -7
  39. metadata +28 -18
  40. /data/{licenses → licences}/COPYING.txt +0 -0
  41. /data/{licenses → licences}/artistic.txt +0 -0
  42. /data/{licenses → licences}/dco.txt +0 -0
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # Diff::LCS
2
2
 
3
- [![RubyGems Version][shield-gems]][rubygems] ![Coveralls][shield-coveralls]
4
- [![Build Status][shield-ci]][ci-workflow]
3
+ [![RubyGems Version](https://img.shields.io/gem/v/diff-lcs?style=for-the-badge "Version")](https://rubygems.org/gems/diff-lcs)
4
+ [![Coveralls](https://img.shields.io/coverallsCoverage/github/halostatue/diff-lcs?style=for-the-badge)](https://coveralls.io/github/halostatue/diff-lcs?branch=main)
5
+ [![Build Status](https://img.shields.io/github/actions/workflow/status/halostatue/diff-lcs/ci.yml?style=for-the-badge "Build Status")](https://github.com/halostatue/diff-lcs/actions/workflows/ci.yml)
6
+ [![MIT or Artistic-1.0-Perls or GPL-2.0-or-later](https://img.shields.io/badge/licence-MIT_or_Artistic--1.0--Perl_or_GPL--2.0--or--later-blue?style=for-the-badge "MIT or Artistic-1.0-Perls or GPL-2.0-or-later")](https://github.com/halostatue/diff-lcs/blob/main/LICENCE.md)
5
7
 
6
8
  - code :: <https://github.com/halostatue/diff-lcs>
7
9
  - issues :: <https://github.com/halostatue/diff-lcs/issues>
@@ -14,20 +16,29 @@ Diff::LCS computes the difference between two Enumerable sequences using the
14
16
  McIlroy-Hunt longest common subsequence (LCS) algorithm. It includes utilities
15
17
  to create a simple HTML diff output format and a standard diff-like tool.
16
18
 
17
- This is release 1.6.1, providing a simple extension that allows for
18
- Diff::LCS::Change objects to be treated implicitly as arrays and fixes a number
19
- of formatting issues.
19
+ This is release 2.0, which has significant breaking changes (removal of
20
+ deprecations and workarounds) and requires at least Ruby 3.2 or higher. See
21
+ full details in the `CHANGELOG`, but users of diff-lcs should not notice any
22
+ significant changes to the APIs.
20
23
 
21
- Ruby versions below 2.5 are soft-deprecated, which means that older versions are
22
- no longer part of the CI test suite. If any changes have been introduced that
23
- break those versions, bug reports and patches will be accepted, but it will be
24
- up to the reporter to verify any fixes prior to release. The next major release
25
- will completely break compatibility.
24
+ ### Performance and Compatibility
25
+
26
+ I have not run any benchmarks, but the use of immutable Data classes and the
27
+ removal of a number of inner loop conditionals related to encoding and String
28
+ handling (which have been unnecessary since at least Ruby 2.1 but kept for
29
+ strict compatibility) should allow better optimization by modern Ruby
30
+ implementations.
31
+
32
+ If you are using RSpec for your test suite, you are unlikely to be able to use
33
+ Diff::LCS 2.0 because of the minimum Ruby version unless the developers of
34
+ RSpec loosen their version constraints. I cannot control this and have raised
35
+ [rspec/rspec#290][rspec-issue-290].
26
36
 
27
37
  ## Synopsis
28
38
 
29
- Using this module is quite simple. By default, Diff::LCS does not extend objects
30
- with the Diff::LCS interface, but will be called as if it were a function:
39
+ Using this module is quite simple. By default, Diff::LCS does not extend
40
+ objects with the Diff::LCS interface, but will be called as if it were a
41
+ function:
31
42
 
32
43
  ```ruby
33
44
  require 'diff/lcs'
@@ -64,33 +75,33 @@ seq1 == seq2.unpatch!(sdiff)
64
75
  By requiring 'diff/lcs/array' or 'diff/lcs/string', Array or String will be
65
76
  extended for use this way.
66
77
 
67
- Note that Diff::LCS requires a sequenced enumerable container, which means that
68
- the order of enumeration is both predictable and consistent for the same set of
69
- data. While it is theoretically possible to generate a diff for an unordered
70
- hash, it will only be meaningful if the enumeration of the hashes is consistent.
71
- In general, this will mean that containers that behave like String or Array will
72
- perform best.
78
+ Note that Diff::LCS requires a sequenced enumerable container, which means
79
+ that the order of enumeration is both predictable and consistent for the same
80
+ set of data. While it is theoretically possible to generate a diff for an
81
+ unordered hash, it will only be meaningful if the enumeration of the hashes is
82
+ consistent. In general, this will mean that containers that behave like String
83
+ or Array will perform best.
73
84
 
74
85
  ## History
75
86
 
76
- Diff::LCS is a port of Perl's Algorithm::Diff that uses the McIlroy-Hunt longest
77
- common subsequence (LCS) algorithm to compute intelligent differences between
78
- two sequenced enumerable containers. The implementation is based on Mario I.
79
- Wolczko's [Smalltalk version 1.2][smalltalk] (1993) and Ned Konz's Perl version
80
- [Algorithm::Diff 1.15][perl]. `Diff::LCS#sdiff` and
87
+ Diff::LCS is a port of Perl's Algorithm::Diff that uses the McIlroy-Hunt
88
+ longest common subsequence (LCS) algorithm to compute intelligent differences
89
+ between two sequenced enumerable containers. The implementation is based on
90
+ Mario I. Wolczko's [Smalltalk version 1.2][smalltalk] (1993) and Ned Konz's
91
+ Perl version [Algorithm::Diff 1.15][perl]. `Diff::LCS#sdiff` and
81
92
  `Diff::LCS#traverse_balanced` were originally written for the Perl version by
82
93
  Mike Schilli.
83
94
 
84
- The algorithm is described in <em>A Fast Algorithm for Computing Longest Common
85
- Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May 1977, with a few minor
95
+ The algorithm is described in _A Fast Algorithm for Computing Longest Common
96
+ Subsequences_, CACM, vol.20, no.5, pp.350-353, May 1977, with a few minor
86
97
  improvements to improve the speed. A simplified description of the algorithm,
87
98
  originally written for the Perl version, was written by Mark-Jason Dominus.
88
99
 
89
100
  [ci-workflow]: https://github.com/halostatue/diff-lcs/actions/workflows/ci.yml
90
101
  [coveralls]: https://coveralls.io/github/halostatue/diff-lcs?branch=main
91
102
  [perl]: https://search.cpan.org/~nedkonz/Algorithm-Diff-1.15/
103
+ [rspec-issue-290]: https://github.com/rspec/rspec/issues/290
92
104
  [rubygems]: https://rubygems.org/gems/diff-lcs
93
105
  [shield-ci]: https://img.shields.io/github/actions/workflow/status/halostatue/diff-lcs/ci.yml?style=for-the-badge "Build Status"
94
106
  [shield-coveralls]: https://img.shields.io/coverallsCoverage/github/halostatue/diff-lcs?style=for-the-badge
95
- [shield-gems]: https://img.shields.io/gem/v/diff-lcs?style=for-the-badge "Version"
96
107
  [smalltalk]: ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
data/Rakefile CHANGED
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "rubygems"
2
4
  require "hoe"
3
5
  require "rake/clean"
4
6
  require "rdoc/task"
7
+ require "minitest"
5
8
  require "minitest/test_task"
6
9
 
7
10
  Hoe.plugin :halostatue
data/SECURITY.md CHANGED
@@ -1,14 +1,14 @@
1
- # diff-lcs Security
1
+ # `diff-lcs` Security
2
2
 
3
3
  ## LLM-Generated Security Report Policy
4
4
 
5
- Absolutely no security reports will be accepted that have been generated by LLM
6
- agents.
5
+ Absolutely no security reports will be accepted that have been generated
6
+ solely by LLM agents. There must be a human that confirms the issue.
7
7
 
8
8
  ## Supported Versions
9
9
 
10
- Security reports are accepted for the most recent major release, with a limited
11
- window of support after the initial major release.
10
+ Security reports are accepted for the most recent major release, with a
11
+ limited window of support after the initial major release.
12
12
 
13
13
  - Bug reports will be accepted up to three months after release.
14
14
  - Security reports will be accepted up to six months after release.
@@ -22,8 +22,8 @@ All issues raised must be demonstrated on the minimum supported Ruby version.
22
22
  >
23
23
  > | Version | Release Date | Support Ends | Security Support Ends |
24
24
  > | ------- | ------------ | ------------ | --------------------- |
25
- > | 1.x | 2010 | 2026-04-30 | 2027-01-31 |
26
- > | 2.x | 2026-01-31 | - | - |
25
+ > | 1.x | 2010 | 2026-05-01 | 2027-02-01 |
26
+ > | 2.x | 2026-02-01 | - | - |
27
27
 
28
28
  ## Reporting a Vulnerability
29
29
 
@@ -2,6 +2,8 @@
2
2
 
3
3
  require "diff/lcs"
4
4
 
5
- class Array
5
+ warn 'diff/lcs/array: Automatically extending Array with Diff::LCS is deprecated'
6
+
7
+ class ::Array #:nodoc:
6
8
  include Diff::LCS
7
9
  end
@@ -234,7 +234,7 @@ end
234
234
  # old and new objects will be presented for both changed <strong>and unchanged</strong>
235
235
  # objects. `nil` will be substituted for a discarded object.
236
236
  #
237
- # The diffset produced by this callback, when provided to Diff::LCS#sdiff, will compute
237
+ # The diff set produced by this callback, when provided to Diff::LCS#sdiff, will compute
238
238
  # and display the necessary components to show two sequences and their minimized
239
239
  # differences side by side, just like the Unix utility `sdiff`.
240
240
  #
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- Diff::LCS::Change = Data.define(:action, :position, :element) # :nodoc:
4
- Diff::LCS::ContextChange = Data.define(:action, :old_position, :old_element, :new_position, :new_element) # :nodoc:
3
+ require "diff/lcs/frozen_value"
5
4
 
6
5
  # Represents a simplistic (non-contextual) change. Represents the removal or addition of
7
6
  # an element from either the old or the new sequenced enumerable.
8
7
  class Diff::LCS::Change
8
+ include Diff::LCS::FrozenValue
9
+
9
10
  # The only actions valid for changes are '+' (add), '-' (delete), '=' (no change), '!'
10
11
  # (changed), '<' (tail changes from first sequence), or '>' (tail changes from second
11
12
  # sequence). The last two ('<>') are only found with Diff::LCS::diff and
@@ -14,23 +15,37 @@ class Diff::LCS::Change
14
15
 
15
16
  def self.valid_action?(action) = VALID_ACTIONS.include?(action)
16
17
 
17
- ##
18
+ MEMBERS = %i[action position element].freeze
19
+ private_constant :MEMBERS
20
+
21
+ # Returns the member names of a Change, in the order of its positional arguments.
22
+ def self.members = MEMBERS.dup
23
+
18
24
  # Returns the action this Change represents.
19
- # :attr_reader: action
25
+ attr_reader :action
20
26
 
21
- ##
22
27
  # Returns the position of the Change.
23
- # :attr_reader: position
28
+ attr_reader :position
24
29
 
25
- ##
26
30
  # Returns the sequence element of the Change.
27
- # :attr_reader: element
31
+ attr_reader :element
32
+
33
+ # :call-seq:
34
+ # new(action, position, element) -> change
35
+ # new(action:, position:, element:) -> change
36
+ #
37
+ # Returns a new frozen Change. `action` must be one of VALID_ACTIONS and `position`
38
+ # must be an Integer.
39
+ def initialize(action = NO_ARGUMENT, position = NO_ARGUMENT, element = NO_ARGUMENT)
40
+ action, position, element = arguments_to_values(action, position, element) if NO_ARGUMENT.equal?(element)
28
41
 
29
- def initialize(action:, position:, element:)
30
42
  fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action)
31
43
  fail "Invalid Position Type" unless position.is_a?(Integer)
32
44
 
33
- super
45
+ @action = action
46
+ @position = position
47
+ @element = element
48
+ freeze
34
49
  end
35
50
 
36
51
  def inspect(*_args) = "#<#{self.class}: #{to_a.inspect}>"
@@ -41,6 +56,12 @@ class Diff::LCS::Change
41
56
 
42
57
  def deconstruct_keys(_) = {action:, position:, element:}
43
58
 
59
+ protected def values # :nodoc:
60
+ [@action, @position, @element]
61
+ end
62
+
63
+ private def member_names = MEMBERS
64
+
44
65
  def self.from_a(arr)
45
66
  case arr
46
67
  in [action, [old_position, old_element], [new_position, new_element]]
@@ -84,32 +105,51 @@ end
84
105
  # Represents a contextual change. Contains the position and values of the elements in the
85
106
  # old and the new sequenced enumerable values as well as the action taken.
86
107
  class Diff::LCS::ContextChange
87
- ##
108
+ include Diff::LCS::FrozenValue
109
+
110
+ MEMBERS = %i[action old_position old_element new_position new_element].freeze
111
+ private_constant :MEMBERS
112
+
113
+ # Returns the member names of a ContextChange, in the order of its positional arguments.
114
+ def self.members = MEMBERS.dup
115
+
88
116
  # Returns the action this Change represents.
89
- # :attr_reader: action
117
+ attr_reader :action
90
118
 
91
- ##
92
119
  # Returns the old position being changed.
93
- # :attr_reader: old_position
94
-
95
- ##
96
- # Returns the new position being changed.
97
- # :attr_reader: new_position
120
+ attr_reader :old_position
98
121
 
99
- ##
100
122
  # Returns the old element being changed.
101
- # :attr_reader: old_element
123
+ attr_reader :old_element
124
+
125
+ # Returns the new position being changed.
126
+ attr_reader :new_position
102
127
 
103
- ##
104
128
  # Returns the new element being changed.
105
- # :attr_reader: new_element
129
+ attr_reader :new_element
130
+
131
+ # Returns a new frozen ContextChange. `action` must be one of Change::VALID_ACTIONS,
132
+ # and each position must be an Integer or `nil`.
133
+ #
134
+ # :call-seq:
135
+ # new(action, old_position, old_element, new_position, new_element) -> context_change
136
+ # new(action:, old_position:, old_element:, new_position:, new_element:) -> context_change
137
+ def initialize(action = NO_ARGUMENT, old_position = NO_ARGUMENT, old_element = NO_ARGUMENT, new_position = NO_ARGUMENT, new_element = NO_ARGUMENT)
138
+ if NO_ARGUMENT.equal?(new_element)
139
+ action, old_position, old_element, new_position, new_element =
140
+ arguments_to_values(action, old_position, old_element, new_position, new_element)
141
+ end
106
142
 
107
- def initialize(action:, old_position:, old_element:, new_position:, new_element:)
108
143
  fail "Invalid Change Action '#{action}'" unless Diff::LCS::Change.valid_action?(action)
109
144
  fail "Invalid (Old) Position Type" unless old_position.nil? || old_position.is_a?(Integer)
110
145
  fail "Invalid (New) Position Type" unless new_position.nil? || new_position.is_a?(Integer)
111
146
 
112
- super
147
+ @action = action
148
+ @old_position = old_position
149
+ @old_element = old_element
150
+ @new_position = new_position
151
+ @new_element = new_element
152
+ freeze
113
153
  end
114
154
 
115
155
  def to_a = [action, [old_position, old_element], [new_position, new_element]]
@@ -118,20 +158,22 @@ class Diff::LCS::ContextChange
118
158
 
119
159
  def deconstruct_keys(_) = {action:, old_position:, old_element:, new_position:, new_element:}
120
160
 
161
+ protected def values # :nodoc:
162
+ [@action, @old_position, @old_element, @new_position, @new_element]
163
+ end
164
+
165
+ private def member_names = MEMBERS
166
+
121
167
  def self.from_a(arr) = Diff::LCS::Change.from_a(arr)
122
168
 
123
169
  # Simplifies a context change for use in some diff callbacks. '<' actions are converted
124
170
  # to '-' and '>' actions are converted to '+'.
125
171
  def self.simplify(event)
126
172
  case event.action
127
- when "-"
128
- event.with(new_element: nil)
129
- when "<"
130
- event.with(action: "-", new_element: nil)
131
- when "+"
132
- event.with(old_element: nil)
133
- when ">"
134
- event.with(action: "+", old_element: nil)
173
+ when "-", "<"
174
+ event.class.new("-", event.old_position, event.old_element, event.new_position, nil)
175
+ when "+", ">"
176
+ event.class.new("+", event.old_position, nil, event.new_position, event.new_element)
135
177
  else
136
178
  event
137
179
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diff; end unless defined? Diff
4
+ module Diff::LCS; end unless defined? Diff::LCS
5
+
6
+ # The Data API (keyword or positional construction, `members`, `to_h`, `with`, `eql?`,
7
+ # `hash`, frozen instances) for Diff::LCS::Change and Diff::LCS::ContextChange. They are
8
+ # built for each element of a traversal, and a Data subclass validating in `initialize`
9
+ # allocates three more objects per instance than a plain class (ruby-core#22437).
10
+ #
11
+ # An including class defines a class-level `members` returning a new Array, like Data, a
12
+ # private `member_names` returning the same names without a copy, `values`,
13
+ # `deconstruct_keys`, readers, and an `initialize` taking one positional argument per
14
+ # member, each defaulting to NO_ARGUMENT, which calls `arguments_to_values` when its last
15
+ # argument is NO_ARGUMENT, then freezes.
16
+ module Diff::LCS::FrozenValue # :nodoc:
17
+ NO_ARGUMENT = Object.new.freeze
18
+ private_constant :NO_ARGUMENT
19
+
20
+ def self.included(base)
21
+ base.singleton_class.alias_method :[], :new
22
+ end
23
+
24
+ def members = self.class.members
25
+
26
+ def to_h(&) = deconstruct_keys(nil).to_h(&)
27
+
28
+ def with(**updates)
29
+ return self if updates.empty?
30
+
31
+ self.class.new(**to_h.merge!(updates))
32
+ end
33
+
34
+ def eql?(other) = self.class == other.class && values.eql?(other.values)
35
+
36
+ def hash = [self.class, values].hash
37
+
38
+ def inspect = "#<#{self.class} #{to_h.map { |member, value| "#{member}=#{value.inspect}" }.join(", ")}>"
39
+
40
+ alias_method :to_s, :inspect
41
+
42
+ # Psych only calls `encode_with` and `init_with` when they are public.
43
+ def encode_with(coder) = to_h.each { |member, value| coder[member.to_s] = value }
44
+
45
+ # Like `marshal_load`, goes through `initialize`, so loaded values are validated and
46
+ # frozen.
47
+ def init_with(coder) = initialize(**coder.map.transform_keys { _1.is_a?(String) ? _1.to_sym : _1 })
48
+
49
+ private
50
+
51
+ # `allocate` must stay defined: Psych calls it before `init_with`, and TruffleRuby's
52
+ # Marshal before `marshal_load`.
53
+ def marshal_load(values) = initialize(*values)
54
+
55
+ def marshal_dump = values
56
+
57
+ def initialize_copy(*) = super.freeze
58
+
59
+ def arguments_to_values(*arguments)
60
+ arguments.pop while NO_ARGUMENT.equal?(arguments.last)
61
+ keywords = arguments.first if arguments.size == 1 && arguments.first.is_a?(Hash)
62
+
63
+ # Fewer positional arguments than members: the ones left out are always missing.
64
+ missing = keywords ? member_names.reject { keywords.key?(_1) } : member_names.drop(arguments.size)
65
+ raise_keywords_error("missing", missing) if missing.any?
66
+ raise_keywords_error("unknown", keywords.keys - member_names) if keywords.size > member_names.size
67
+
68
+ keywords.values_at(*member_names)
69
+ end
70
+
71
+ def raise_keywords_error(kind, keywords)
72
+ raise ArgumentError, "#{kind} keyword#{"s" if keywords.size > 1}: #{keywords.map(&:inspect).join(", ")}"
73
+ end
74
+ end