diff-lcs 1.3 → 1.5.1
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 +5 -5
- data/Contributing.md +86 -48
- data/History.md +370 -159
- data/License.md +6 -4
- data/Manifest.txt +23 -1
- data/README.rdoc +10 -10
- data/Rakefile +109 -36
- data/bin/htmldiff +9 -6
- data/bin/ldiff +4 -1
- data/lib/diff/lcs/array.rb +2 -2
- data/lib/diff/lcs/backports.rb +9 -0
- data/lib/diff/lcs/block.rb +5 -5
- data/lib/diff/lcs/callbacks.rb +22 -17
- data/lib/diff/lcs/change.rb +42 -49
- data/lib/diff/lcs/htmldiff.rb +21 -12
- data/lib/diff/lcs/hunk.rb +160 -73
- data/lib/diff/lcs/internals.rb +57 -56
- data/lib/diff/lcs/ldiff.rb +63 -57
- data/lib/diff/lcs/string.rb +1 -1
- data/lib/diff/lcs.rb +226 -210
- data/lib/diff-lcs.rb +2 -2
- data/spec/change_spec.rb +58 -34
- data/spec/diff_spec.rb +13 -9
- data/spec/fixtures/aX +1 -0
- data/spec/fixtures/bXaX +1 -0
- data/spec/fixtures/ldiff/output.diff +4 -0
- data/spec/fixtures/ldiff/output.diff-c +7 -0
- data/spec/fixtures/ldiff/output.diff-e +3 -0
- data/spec/fixtures/ldiff/output.diff-f +3 -0
- data/spec/fixtures/ldiff/output.diff-u +5 -0
- data/spec/fixtures/ldiff/output.diff.chef +4 -0
- data/spec/fixtures/ldiff/output.diff.chef-c +15 -0
- data/spec/fixtures/ldiff/output.diff.chef-e +3 -0
- data/spec/fixtures/ldiff/output.diff.chef-f +3 -0
- data/spec/fixtures/ldiff/output.diff.chef-u +9 -0
- data/spec/fixtures/ldiff/output.diff.chef2 +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-c +20 -0
- data/spec/fixtures/ldiff/output.diff.chef2-d +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-e +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-f +7 -0
- data/spec/fixtures/ldiff/output.diff.chef2-u +16 -0
- data/spec/fixtures/new-chef +4 -0
- data/spec/fixtures/new-chef2 +17 -0
- data/spec/fixtures/old-chef +4 -0
- data/spec/fixtures/old-chef2 +14 -0
- data/spec/hunk_spec.rb +48 -37
- data/spec/issues_spec.rb +132 -21
- data/spec/lcs_spec.rb +3 -3
- data/spec/ldiff_spec.rb +74 -32
- data/spec/patch_spec.rb +14 -20
- data/spec/sdiff_spec.rb +83 -81
- data/spec/spec_helper.rb +146 -91
- data/spec/traverse_balanced_spec.rb +138 -136
- data/spec/traverse_sequences_spec.rb +7 -9
- metadata +76 -48
- data/autotest/discover.rb +0 -1
data/Manifest.txt
CHANGED
|
@@ -6,7 +6,6 @@ License.md
|
|
|
6
6
|
Manifest.txt
|
|
7
7
|
README.rdoc
|
|
8
8
|
Rakefile
|
|
9
|
-
autotest/discover.rb
|
|
10
9
|
bin/htmldiff
|
|
11
10
|
bin/ldiff
|
|
12
11
|
docs/COPYING.txt
|
|
@@ -14,6 +13,7 @@ docs/artistic.txt
|
|
|
14
13
|
lib/diff-lcs.rb
|
|
15
14
|
lib/diff/lcs.rb
|
|
16
15
|
lib/diff/lcs/array.rb
|
|
16
|
+
lib/diff/lcs/backports.rb
|
|
17
17
|
lib/diff/lcs/block.rb
|
|
18
18
|
lib/diff/lcs/callbacks.rb
|
|
19
19
|
lib/diff/lcs/change.rb
|
|
@@ -24,8 +24,30 @@ lib/diff/lcs/ldiff.rb
|
|
|
24
24
|
lib/diff/lcs/string.rb
|
|
25
25
|
spec/change_spec.rb
|
|
26
26
|
spec/diff_spec.rb
|
|
27
|
+
spec/fixtures/aX
|
|
28
|
+
spec/fixtures/bXaX
|
|
27
29
|
spec/fixtures/ds1.csv
|
|
28
30
|
spec/fixtures/ds2.csv
|
|
31
|
+
spec/fixtures/ldiff/output.diff
|
|
32
|
+
spec/fixtures/ldiff/output.diff-c
|
|
33
|
+
spec/fixtures/ldiff/output.diff-e
|
|
34
|
+
spec/fixtures/ldiff/output.diff-f
|
|
35
|
+
spec/fixtures/ldiff/output.diff-u
|
|
36
|
+
spec/fixtures/ldiff/output.diff.chef
|
|
37
|
+
spec/fixtures/ldiff/output.diff.chef-c
|
|
38
|
+
spec/fixtures/ldiff/output.diff.chef-e
|
|
39
|
+
spec/fixtures/ldiff/output.diff.chef-f
|
|
40
|
+
spec/fixtures/ldiff/output.diff.chef-u
|
|
41
|
+
spec/fixtures/ldiff/output.diff.chef2
|
|
42
|
+
spec/fixtures/ldiff/output.diff.chef2-c
|
|
43
|
+
spec/fixtures/ldiff/output.diff.chef2-d
|
|
44
|
+
spec/fixtures/ldiff/output.diff.chef2-e
|
|
45
|
+
spec/fixtures/ldiff/output.diff.chef2-f
|
|
46
|
+
spec/fixtures/ldiff/output.diff.chef2-u
|
|
47
|
+
spec/fixtures/new-chef
|
|
48
|
+
spec/fixtures/new-chef2
|
|
49
|
+
spec/fixtures/old-chef
|
|
50
|
+
spec/fixtures/old-chef2
|
|
29
51
|
spec/hunk_spec.rb
|
|
30
52
|
spec/issues_spec.rb
|
|
31
53
|
spec/lcs_spec.rb
|
data/README.rdoc
CHANGED
|
@@ -4,8 +4,7 @@ home :: https://github.com/halostatue/diff-lcs
|
|
|
4
4
|
code :: https://github.com/halostatue/diff-lcs
|
|
5
5
|
bugs :: https://github.com/halostatue/diff-lcs/issues
|
|
6
6
|
rdoc :: http://rubydoc.info/github/halostatue/diff-lcs
|
|
7
|
-
continuous integration :: {<img src="https://
|
|
8
|
-
test coverage :: {<img src="https://coveralls.io/repos/halostatue/diff-lcs/badge.svg" alt="Coverage Status" />}[https://coveralls.io/r/halostatue/diff-lcs]
|
|
7
|
+
continuous integration :: {<img src="https://github.com/halostatue/diff-lcs/workflows/CI/badge.svg" />}[https://github.com/halostatue/diff-lcs/actions]
|
|
9
8
|
|
|
10
9
|
== Description
|
|
11
10
|
|
|
@@ -13,10 +12,15 @@ Diff::LCS computes the difference between two Enumerable sequences using the
|
|
|
13
12
|
McIlroy-Hunt longest common subsequence (LCS) algorithm. It includes utilities
|
|
14
13
|
to create a simple HTML diff output format and a standard diff-like tool.
|
|
15
14
|
|
|
16
|
-
This is release 1.3, providing a
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
This is release 1.4.3, providing a simple extension that allows for
|
|
16
|
+
Diff::LCS::Change objects to be treated implicitly as arrays and fixes a
|
|
17
|
+
number of formatting issues.
|
|
18
|
+
|
|
19
|
+
Ruby versions below 2.5 are soft-deprecated, which means that older versions
|
|
20
|
+
are no longer part of the CI test suite. If any changes have been introduced
|
|
21
|
+
that break those versions, bug reports and patches will be accepted, but it
|
|
22
|
+
will be up to the reporter to verify any fixes prior to release. The next
|
|
23
|
+
major release will completely break compatibility.
|
|
20
24
|
|
|
21
25
|
== Synopsis
|
|
22
26
|
|
|
@@ -78,7 +82,3 @@ The algorithm is described in <em>A Fast Algorithm for Computing Longest Common
|
|
|
78
82
|
Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May 1977, with a few minor
|
|
79
83
|
improvements to improve the speed. A simplified description of the algorithm,
|
|
80
84
|
originally written for the Perl version, was written by Mark-Jason Dominus.
|
|
81
|
-
|
|
82
|
-
:include: Contributing.rdoc
|
|
83
|
-
|
|
84
|
-
:include: License.rdoc
|
data/Rakefile
CHANGED
|
@@ -1,57 +1,130 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
require
|
|
3
|
+
require "rubygems"
|
|
4
|
+
require "rspec"
|
|
5
|
+
require "rspec/core/rake_task"
|
|
6
|
+
require "hoe"
|
|
7
|
+
|
|
8
|
+
# This is required until https://github.com/seattlerb/hoe/issues/112 is fixed
|
|
9
|
+
class Hoe
|
|
10
|
+
def with_config
|
|
11
|
+
config = Hoe::DEFAULT_CONFIG
|
|
12
|
+
|
|
13
|
+
rc = File.expand_path("~/.hoerc")
|
|
14
|
+
homeconfig = load_config(rc)
|
|
15
|
+
config = config.merge(homeconfig)
|
|
16
|
+
|
|
17
|
+
localconfig = load_config(File.expand_path(File.join(Dir.pwd, ".hoerc")))
|
|
18
|
+
config = config.merge(localconfig)
|
|
19
|
+
|
|
20
|
+
yield config, rc
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def load_config(name)
|
|
24
|
+
File.exist?(name) ? safe_load_yaml(name) : {}
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def safe_load_yaml(name)
|
|
28
|
+
return safe_load_yaml_file(name) if YAML.respond_to?(:safe_load_file)
|
|
29
|
+
|
|
30
|
+
data = IO.binread(name)
|
|
31
|
+
YAML.safe_load(data, permitted_classes: [Regexp])
|
|
32
|
+
rescue
|
|
33
|
+
YAML.safe_load(data, [Regexp])
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def safe_load_yaml_file(name)
|
|
37
|
+
YAML.safe_load_file(name, permitted_classes: [Regexp])
|
|
38
|
+
rescue
|
|
39
|
+
YAML.safe_load_file(name, [Regexp])
|
|
40
|
+
end
|
|
41
|
+
end
|
|
6
42
|
|
|
7
43
|
Hoe.plugin :bundler
|
|
8
44
|
Hoe.plugin :doofus
|
|
9
|
-
Hoe.plugin :email unless ENV['CI'] or ENV['TRAVIS']
|
|
10
45
|
Hoe.plugin :gemspec2
|
|
11
46
|
Hoe.plugin :git
|
|
12
|
-
Hoe.plugin :travis
|
|
13
47
|
|
|
14
|
-
|
|
15
|
-
|
|
48
|
+
if RUBY_VERSION < "1.9"
|
|
49
|
+
class Array # :nodoc:
|
|
50
|
+
def to_h
|
|
51
|
+
Hash[*flatten(1)]
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class Gem::Specification # :nodoc:
|
|
56
|
+
def metadata=(*)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def default_value(*)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
class Object # :nodoc:
|
|
64
|
+
def caller_locations(*)
|
|
65
|
+
[]
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
_spec = Hoe.spec "diff-lcs" do
|
|
71
|
+
developer("Austin Ziegler", "halostatue@gmail.com")
|
|
72
|
+
|
|
73
|
+
require_ruby_version ">= 1.8"
|
|
16
74
|
|
|
17
|
-
|
|
75
|
+
self.history_file = "History.md"
|
|
76
|
+
self.readme_file = "README.rdoc"
|
|
77
|
+
self.licenses = ["MIT", "Artistic-2.0", "GPL-2.0-or-later"]
|
|
18
78
|
|
|
19
|
-
|
|
20
|
-
self.readme_file = 'README.rdoc'
|
|
21
|
-
self.licenses = [ 'MIT', 'Artistic-2.0', 'GPL-2.0+' ]
|
|
79
|
+
spec_extras[:metadata] = ->(val) { val["rubygems_mfa_required"] = "true" }
|
|
22
80
|
|
|
23
|
-
extra_dev_deps << [
|
|
24
|
-
extra_dev_deps << [
|
|
25
|
-
extra_dev_deps << [
|
|
26
|
-
extra_dev_deps << [
|
|
27
|
-
extra_dev_deps << [
|
|
28
|
-
extra_dev_deps << [
|
|
29
|
-
extra_dev_deps << [
|
|
30
|
-
extra_dev_deps << [
|
|
81
|
+
extra_dev_deps << ["hoe", ">= 3.0", "< 5"]
|
|
82
|
+
extra_dev_deps << ["hoe-doofus", "~> 1.0"]
|
|
83
|
+
extra_dev_deps << ["hoe-gemspec2", "~> 1.1"]
|
|
84
|
+
extra_dev_deps << ["hoe-git2", "~> 1.7"]
|
|
85
|
+
extra_dev_deps << ["hoe-rubygems", "~> 1.0"]
|
|
86
|
+
extra_dev_deps << ["rspec", ">= 2.0", "< 4"]
|
|
87
|
+
extra_dev_deps << ["rake", ">= 10.0", "< 14"]
|
|
88
|
+
extra_dev_deps << ["rdoc", ">= 6.3.1", "< 7"]
|
|
31
89
|
end
|
|
32
90
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
91
|
+
desc "Run all specifications"
|
|
92
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
|
93
|
+
rspec_dirs = %w[spec lib].join(":")
|
|
94
|
+
t.rspec_opts = ["-I#{rspec_dirs}"]
|
|
36
95
|
end
|
|
37
96
|
|
|
38
|
-
|
|
39
|
-
namespace :spec do
|
|
40
|
-
task :coveralls do
|
|
41
|
-
if ENV['CI'] or ENV['TRAVIS']
|
|
42
|
-
ENV['COVERALLS'] = 'yes'
|
|
43
|
-
Rake::Task['spec'].execute
|
|
44
|
-
else
|
|
45
|
-
Rake::Task['spec:coverage'].execute
|
|
46
|
-
end
|
|
47
|
-
end
|
|
97
|
+
Rake::Task["spec"].actions.uniq! { |a| a.source_location }
|
|
48
98
|
|
|
99
|
+
# standard:disable Style/HashSyntax
|
|
100
|
+
task :default => :spec unless Rake::Task["default"].prereqs.include?("spec")
|
|
101
|
+
task :test => :spec unless Rake::Task["test"].prereqs.include?("spec")
|
|
102
|
+
# standard:enable Style/HashSyntax
|
|
103
|
+
|
|
104
|
+
if RUBY_VERSION >= "2.0" && RUBY_ENGINE == "ruby"
|
|
105
|
+
namespace :spec do
|
|
49
106
|
desc "Runs test coverage. Only works Ruby 2.0+ and assumes 'simplecov' is installed."
|
|
50
107
|
task :coverage do
|
|
51
|
-
ENV[
|
|
52
|
-
Rake::Task[
|
|
108
|
+
ENV["COVERAGE"] = "yes"
|
|
109
|
+
Rake::Task["spec"].execute
|
|
53
110
|
end
|
|
54
111
|
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
task :ruby18 do
|
|
115
|
+
# standard:disable Layout/HeredocIndentation
|
|
116
|
+
puts <<-MESSAGE
|
|
117
|
+
You are starting a barebones Ruby 1.8 docker environment. You will need to
|
|
118
|
+
do the following:
|
|
119
|
+
|
|
120
|
+
- mv Gemfile.lock{,.v2}
|
|
121
|
+
- gem install bundler --version 1.17.2 --no-ri --no-rdoc
|
|
122
|
+
- ruby -S bundle
|
|
123
|
+
- rake
|
|
124
|
+
|
|
125
|
+
Don't forget to restore your Gemfile.lock after testing.
|
|
55
126
|
|
|
56
|
-
|
|
127
|
+
MESSAGE
|
|
128
|
+
# standard:enable Layout/HeredocIndentation
|
|
129
|
+
sh "docker run -it --rm -v #{Dir.pwd}:/root/diff-lcs bellbind/docker-ruby18-rails2 bash -l"
|
|
57
130
|
end
|
data/bin/htmldiff
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
#!ruby -w
|
|
1
|
+
#! /usr/bin/env ruby -w
|
|
2
|
+
# frozen_string_literal: true
|
|
2
3
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
4
|
+
require "diff/lcs"
|
|
5
|
+
require "diff/lcs/htmldiff"
|
|
5
6
|
|
|
6
7
|
begin
|
|
7
|
-
require
|
|
8
|
+
require "text/format"
|
|
8
9
|
rescue LoadError
|
|
9
10
|
Diff::LCS::HTMLDiff.can_expand_tabs = false
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
if ARGV.size < 2 or ARGV.size > 3
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
warn "usage: #{File.basename($0)} old new [output.html]"
|
|
15
|
+
warn " #{File.basename($0)} old new > output.html"
|
|
15
16
|
exit 127
|
|
16
17
|
end
|
|
17
18
|
|
|
@@ -30,3 +31,5 @@ if ARGV[2]
|
|
|
30
31
|
else
|
|
31
32
|
htmldiff.run
|
|
32
33
|
end
|
|
34
|
+
|
|
35
|
+
# vim: ft=ruby
|
data/bin/ldiff
CHANGED
data/lib/diff/lcs/array.rb
CHANGED
data/lib/diff/lcs/block.rb
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
# A block is an operation removing, adding, or changing a group of items.
|
|
4
4
|
# Basically, this is just a list of changes, where each change adds or
|
|
@@ -25,13 +25,13 @@ class Diff::LCS::Block
|
|
|
25
25
|
def op
|
|
26
26
|
case [@remove.empty?, @insert.empty?]
|
|
27
27
|
when [false, false]
|
|
28
|
-
|
|
28
|
+
"!"
|
|
29
29
|
when [false, true]
|
|
30
|
-
|
|
30
|
+
"-"
|
|
31
31
|
when [true, false]
|
|
32
|
-
|
|
32
|
+
"+"
|
|
33
33
|
else # [true, true]
|
|
34
|
-
|
|
34
|
+
"^"
|
|
35
35
|
end
|
|
36
36
|
end
|
|
37
37
|
end
|
data/lib/diff/lcs/callbacks.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
3
|
+
require "diff/lcs/change"
|
|
4
4
|
|
|
5
5
|
module Diff::LCS
|
|
6
6
|
# This callback object implements the default set of callback events,
|
|
@@ -17,14 +17,17 @@ module Diff::LCS
|
|
|
17
17
|
def match(event)
|
|
18
18
|
event
|
|
19
19
|
end
|
|
20
|
+
|
|
20
21
|
# Called when the old value is discarded in favour of the new value.
|
|
21
22
|
def discard_a(event)
|
|
22
23
|
event
|
|
23
24
|
end
|
|
25
|
+
|
|
24
26
|
# Called when the new value is discarded in favour of the old value.
|
|
25
27
|
def discard_b(event)
|
|
26
28
|
event
|
|
27
29
|
end
|
|
30
|
+
|
|
28
31
|
# Called when both the old and new values have changed.
|
|
29
32
|
def change(event)
|
|
30
33
|
event
|
|
@@ -47,7 +50,9 @@ module Diff::LCS
|
|
|
47
50
|
BalancedCallbacks = DefaultCallbacks
|
|
48
51
|
|
|
49
52
|
def self.callbacks_for(callbacks)
|
|
50
|
-
callbacks.new
|
|
53
|
+
callbacks.new
|
|
54
|
+
rescue
|
|
55
|
+
callbacks
|
|
51
56
|
end
|
|
52
57
|
end
|
|
53
58
|
|
|
@@ -104,16 +109,16 @@ class Diff::LCS::DiffCallbacks
|
|
|
104
109
|
# Returns the difference set collected during the diff process.
|
|
105
110
|
attr_reader :diffs
|
|
106
111
|
|
|
107
|
-
def initialize # :yields self
|
|
112
|
+
def initialize # :yields: self
|
|
108
113
|
@hunk = []
|
|
109
114
|
@diffs = []
|
|
110
115
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
116
|
+
return unless block_given?
|
|
117
|
+
|
|
118
|
+
begin
|
|
119
|
+
yield self
|
|
120
|
+
ensure
|
|
121
|
+
finish
|
|
117
122
|
end
|
|
118
123
|
end
|
|
119
124
|
|
|
@@ -123,16 +128,16 @@ class Diff::LCS::DiffCallbacks
|
|
|
123
128
|
finish_hunk
|
|
124
129
|
end
|
|
125
130
|
|
|
126
|
-
def match(
|
|
131
|
+
def match(_event)
|
|
127
132
|
finish_hunk
|
|
128
133
|
end
|
|
129
134
|
|
|
130
135
|
def discard_a(event)
|
|
131
|
-
@hunk << Diff::LCS::Change.new(
|
|
136
|
+
@hunk << Diff::LCS::Change.new("-", event.old_position, event.old_element)
|
|
132
137
|
end
|
|
133
138
|
|
|
134
139
|
def discard_b(event)
|
|
135
|
-
@hunk << Diff::LCS::Change.new(
|
|
140
|
+
@hunk << Diff::LCS::Change.new("+", event.new_position, event.new_element)
|
|
136
141
|
end
|
|
137
142
|
|
|
138
143
|
def finish_hunk
|
|
@@ -190,7 +195,7 @@ end
|
|
|
190
195
|
# Diff::LCS::SDiffCallbacks. They may be compared as:
|
|
191
196
|
#
|
|
192
197
|
# s = Diff::LCS.sdiff(seq1, seq2).reject { |e| e.action == "=" }
|
|
193
|
-
# c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten
|
|
198
|
+
# c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten(1)
|
|
194
199
|
#
|
|
195
200
|
# s == c # -> true
|
|
196
201
|
#
|
|
@@ -241,7 +246,7 @@ end
|
|
|
241
246
|
# will compute and display the necessary components to show two sequences
|
|
242
247
|
# and their minimized differences side by side, just like the Unix utility
|
|
243
248
|
# +sdiff+.
|
|
244
|
-
#
|
|
249
|
+
#
|
|
245
250
|
# same same
|
|
246
251
|
# before | after
|
|
247
252
|
# old < -
|
|
@@ -270,7 +275,7 @@ end
|
|
|
270
275
|
# Diff::LCS::ContextDiffCallbacks. They may be compared as:
|
|
271
276
|
#
|
|
272
277
|
# s = Diff::LCS.sdiff(seq1, seq2).reject { |e| e.action == "=" }
|
|
273
|
-
# c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten
|
|
278
|
+
# c = Diff::LCS.sdiff(seq1, seq2, Diff::LCS::ContextDiffCallbacks).flatten(1)
|
|
274
279
|
#
|
|
275
280
|
# s == c # -> true
|
|
276
281
|
#
|
|
@@ -299,7 +304,7 @@ class Diff::LCS::SDiffCallbacks
|
|
|
299
304
|
# Returns the difference set collected during the diff process.
|
|
300
305
|
attr_reader :diffs
|
|
301
306
|
|
|
302
|
-
def initialize
|
|
307
|
+
def initialize # :yields: self
|
|
303
308
|
@diffs = []
|
|
304
309
|
yield self if block_given?
|
|
305
310
|
end
|
data/lib/diff/lcs/change.rb
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
# Represents a simplistic (non-contextual) change. Represents the removal or
|
|
4
4
|
# addition of an element from either the old or the new sequenced
|
|
5
5
|
# enumerable.
|
|
6
6
|
class Diff::LCS::Change
|
|
7
|
-
IntClass = 1.class # Fixnum is deprecated in Ruby 2.4
|
|
7
|
+
IntClass = 1.class # Fixnum is deprecated in Ruby 2.4 # rubocop:disable Naming/ConstantName
|
|
8
8
|
|
|
9
9
|
# The only actions valid for changes are '+' (add), '-' (delete), '='
|
|
10
10
|
# (no change), '!' (changed), '<' (tail changes from first sequence), or
|
|
11
11
|
# '>' (tail changes from second sequence). The last two ('<>') are only
|
|
12
12
|
# found with Diff::LCS::diff and Diff::LCS::sdiff.
|
|
13
|
-
VALID_ACTIONS = %
|
|
13
|
+
VALID_ACTIONS = %w[+ - = ! > <].freeze
|
|
14
14
|
|
|
15
15
|
def self.valid_action?(action)
|
|
16
16
|
VALID_ACTIONS.include? action
|
|
@@ -27,20 +27,20 @@ class Diff::LCS::Change
|
|
|
27
27
|
def initialize(*args)
|
|
28
28
|
@action, @position, @element = *args
|
|
29
29
|
|
|
30
|
-
unless Diff::LCS::Change.valid_action?(@action)
|
|
31
|
-
|
|
32
|
-
end
|
|
33
|
-
raise "Invalid Position Type" unless @position.kind_of? IntClass
|
|
30
|
+
fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
|
|
31
|
+
fail "Invalid Position Type" unless @position.is_a? IntClass
|
|
34
32
|
end
|
|
35
33
|
|
|
36
|
-
def inspect
|
|
37
|
-
to_a.inspect
|
|
34
|
+
def inspect(*_args)
|
|
35
|
+
"#<#{self.class}: #{to_a.inspect}>"
|
|
38
36
|
end
|
|
39
37
|
|
|
40
38
|
def to_a
|
|
41
|
-
[
|
|
39
|
+
[@action, @position, @element]
|
|
42
40
|
end
|
|
43
41
|
|
|
42
|
+
alias_method :to_ary, :to_a
|
|
43
|
+
|
|
44
44
|
def self.from_a(arr)
|
|
45
45
|
arr = arr.flatten(1)
|
|
46
46
|
case arr.size
|
|
@@ -49,7 +49,7 @@ class Diff::LCS::Change
|
|
|
49
49
|
when 3
|
|
50
50
|
Diff::LCS::Change.new(*(arr[0...3]))
|
|
51
51
|
else
|
|
52
|
-
|
|
52
|
+
fail "Invalid change array format provided."
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
|
|
@@ -57,40 +57,40 @@ class Diff::LCS::Change
|
|
|
57
57
|
|
|
58
58
|
def ==(other)
|
|
59
59
|
(self.class == other.class) and
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
(action == other.action) and
|
|
61
|
+
(position == other.position) and
|
|
62
|
+
(element == other.element)
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def <=>(other)
|
|
66
|
-
r =
|
|
67
|
-
r =
|
|
68
|
-
r =
|
|
66
|
+
r = action <=> other.action
|
|
67
|
+
r = position <=> other.position if r.zero?
|
|
68
|
+
r = element <=> other.element if r.zero?
|
|
69
69
|
r
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
def adding?
|
|
73
|
-
@action ==
|
|
73
|
+
@action == "+"
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
def deleting?
|
|
77
|
-
@action ==
|
|
77
|
+
@action == "-"
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
def unchanged?
|
|
81
|
-
@action ==
|
|
81
|
+
@action == "="
|
|
82
82
|
end
|
|
83
83
|
|
|
84
84
|
def changed?
|
|
85
|
-
@action ==
|
|
85
|
+
@action == "!"
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
def finished_a?
|
|
89
|
-
@action ==
|
|
89
|
+
@action == ">"
|
|
90
90
|
end
|
|
91
91
|
|
|
92
92
|
def finished_b?
|
|
93
|
-
@action ==
|
|
93
|
+
@action == "<"
|
|
94
94
|
end
|
|
95
95
|
end
|
|
96
96
|
|
|
@@ -114,27 +114,20 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
|
|
|
114
114
|
def initialize(*args)
|
|
115
115
|
@action, @old_position, @old_element, @new_position, @new_element = *args
|
|
116
116
|
|
|
117
|
-
unless Diff::LCS::Change.valid_action?(@action)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
unless @old_position.nil? or @old_position.kind_of? IntClass
|
|
121
|
-
raise "Invalid (Old) Position Type"
|
|
122
|
-
end
|
|
123
|
-
unless @new_position.nil? or @new_position.kind_of? IntClass
|
|
124
|
-
raise "Invalid (New) Position Type"
|
|
125
|
-
end
|
|
117
|
+
fail "Invalid Change Action '#{@action}'" unless Diff::LCS::Change.valid_action?(@action)
|
|
118
|
+
fail "Invalid (Old) Position Type" unless @old_position.nil? || @old_position.is_a?(IntClass)
|
|
119
|
+
fail "Invalid (New) Position Type" unless @new_position.nil? || @new_position.is_a?(IntClass)
|
|
126
120
|
end
|
|
127
121
|
|
|
128
122
|
def to_a
|
|
129
|
-
[
|
|
130
|
-
|
|
131
|
-
[
|
|
123
|
+
[
|
|
124
|
+
@action,
|
|
125
|
+
[@old_position, @old_element],
|
|
126
|
+
[@new_position, @new_element]
|
|
132
127
|
]
|
|
133
128
|
end
|
|
134
129
|
|
|
135
|
-
|
|
136
|
-
to_a.inspect
|
|
137
|
-
end
|
|
130
|
+
alias_method :to_ary, :to_a
|
|
138
131
|
|
|
139
132
|
def self.from_a(arr)
|
|
140
133
|
Diff::LCS::Change.from_a(arr)
|
|
@@ -146,15 +139,15 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
|
|
|
146
139
|
ea = event.to_a
|
|
147
140
|
|
|
148
141
|
case ea[0]
|
|
149
|
-
when
|
|
142
|
+
when "-"
|
|
150
143
|
ea[2][1] = nil
|
|
151
|
-
when
|
|
152
|
-
ea[0] =
|
|
144
|
+
when "<"
|
|
145
|
+
ea[0] = "-"
|
|
153
146
|
ea[2][1] = nil
|
|
154
|
-
when
|
|
147
|
+
when "+"
|
|
155
148
|
ea[1][1] = nil
|
|
156
|
-
when
|
|
157
|
-
ea[0] =
|
|
149
|
+
when ">"
|
|
150
|
+
ea[0] = "+"
|
|
158
151
|
ea[1][1] = nil
|
|
159
152
|
end
|
|
160
153
|
|
|
@@ -163,11 +156,11 @@ class Diff::LCS::ContextChange < Diff::LCS::Change
|
|
|
163
156
|
|
|
164
157
|
def ==(other)
|
|
165
158
|
(self.class == other.class) and
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
159
|
+
(@action == other.action) and
|
|
160
|
+
(@old_position == other.old_position) and
|
|
161
|
+
(@new_position == other.new_position) and
|
|
162
|
+
(@old_element == other.old_element) and
|
|
163
|
+
(@new_element == other.new_element)
|
|
171
164
|
end
|
|
172
165
|
|
|
173
166
|
def <=>(other)
|