diff-lcs 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ The "Artistic License"
2
+
3
+ Preamble
4
+
5
+ The intent of this document is to state the conditions under which a
6
+ Package may be copied, such that the Copyright Holder maintains some
7
+ semblance of artistic control over the development of the package,
8
+ while giving the users of the package the right to use and distribute
9
+ the Package in a more-or-less customary fashion, plus the right to make
10
+ reasonable modifications.
11
+
12
+ Definitions:
13
+
14
+ "Package" refers to the collection of files distributed by the
15
+ Copyright Holder, and derivatives of that collection of files
16
+ created through textual modification.
17
+
18
+ "Standard Version" refers to such a Package if it has not been
19
+ modified, or has been modified in accordance with the wishes
20
+ of the Copyright Holder as specified below.
21
+
22
+ "Copyright Holder" is whoever is named in the copyright or
23
+ copyrights for the package.
24
+
25
+ "You" is you, if you're thinking about copying or distributing
26
+ this Package.
27
+
28
+ "Reasonable copying fee" is whatever you can justify on the
29
+ basis of media cost, duplication charges, time of people involved,
30
+ and so on. (You will not be required to justify it to the
31
+ Copyright Holder, but only to the computing community at large
32
+ as a market that must bear the fee.)
33
+
34
+ "Freely Available" means that no fee is charged for the item
35
+ itself, though there may be fees involved in handling the item.
36
+ It also means that recipients of the item may redistribute it
37
+ under the same conditions they received it.
38
+
39
+ 1. You may make and give away verbatim copies of the source form of the
40
+ Standard Version of this Package without restriction, provided that you
41
+ duplicate all of the original copyright notices and associated disclaimers.
42
+
43
+ 2. You may apply bug fixes, portability fixes and other modifications
44
+ derived from the Public Domain or from the Copyright Holder. A Package
45
+ modified in such a way shall still be considered the Standard Version.
46
+
47
+ 3. You may otherwise modify your copy of this Package in any way, provided
48
+ that you insert a prominent notice in each changed file stating how and
49
+ when you changed that file, and provided that you do at least ONE of the
50
+ following:
51
+
52
+ a) place your modifications in the Public Domain or otherwise make them
53
+ Freely Available, such as by posting said modifications to Usenet or
54
+ an equivalent medium, or placing the modifications on a major archive
55
+ site such as uunet.uu.net, or by allowing the Copyright Holder to include
56
+ your modifications in the Standard Version of the Package.
57
+
58
+ b) use the modified Package only within your corporation or organization.
59
+
60
+ c) rename any non-standard executables so the names do not conflict
61
+ with standard executables, which must also be provided, and provide
62
+ a separate manual page for each non-standard executable that clearly
63
+ documents how it differs from the Standard Version.
64
+
65
+ d) make other distribution arrangements with the Copyright Holder.
66
+
67
+ 4. You may distribute the programs of this Package in object code or
68
+ executable form, provided that you do at least ONE of the following:
69
+
70
+ a) distribute a Standard Version of the executables and library files,
71
+ together with instructions (in the manual page or equivalent) on where
72
+ to get the Standard Version.
73
+
74
+ b) accompany the distribution with the machine-readable source of
75
+ the Package with your modifications.
76
+
77
+ c) give non-standard executables non-standard names, and clearly
78
+ document the differences in manual pages (or equivalent), together
79
+ with instructions on where to get the Standard Version.
80
+
81
+ d) make other distribution arrangements with the Copyright Holder.
82
+
83
+ 5. You may charge a reasonable copying fee for any distribution of this
84
+ Package. You may charge any fee you choose for support of this
85
+ Package. You may not charge a fee for this Package itself. However,
86
+ you may distribute this Package in aggregate with other (possibly
87
+ commercial) programs as part of a larger (possibly commercial) software
88
+ distribution provided that you do not advertise this Package as a
89
+ product of your own. You may embed this Package's interpreter within
90
+ an executable of yours (by linking); this shall be construed as a mere
91
+ form of aggregation, provided that the complete Standard Version of the
92
+ interpreter is so embedded.
93
+
94
+ 6. The scripts and library files supplied as input to or produced as
95
+ output from the programs of this Package do not automatically fall
96
+ under the copyright of this Package, but belong to whoever generated
97
+ them, and may be sold commercially, and may be aggregated with this
98
+ Package. If such scripts or library files are aggregated with this
99
+ Package via the so-called "undump" or "unexec" methods of producing a
100
+ binary executable image, then distribution of such an image shall
101
+ neither be construed as a distribution of this Package nor shall it
102
+ fall under the restrictions of Paragraphs 3 and 4, provided that you do
103
+ not represent such an executable image as a Standard Version of this
104
+ Package.
105
+
106
+ 7. C subroutines (or comparably compiled subroutines in other
107
+ languages) supplied by you and linked into this Package in order to
108
+ emulate subroutines and variables of the language defined by this
109
+ Package shall not be considered part of this Package, but are the
110
+ equivalent of input as in Paragraph 6, provided these subroutines do
111
+ not change the language in any way that would cause it to fail the
112
+ regression tests for the language.
113
+
114
+ 8. Aggregation of this Package with a commercial distribution is always
115
+ permitted provided that the use of this Package is embedded; that is,
116
+ when no overt attempt is made to make this Package's interfaces visible
117
+ to the end user of the commercial distribution. Such use shall not be
118
+ construed as a distribution of this Package.
119
+
120
+ 9. The name of the Copyright Holder may not be used to endorse or promote
121
+ products derived from this software without specific prior written permission.
122
+
123
+ 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
124
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
125
+ WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
126
+
127
+ The End
@@ -1,5 +1,3 @@
1
1
  # -*- ruby encoding: utf-8 -*-
2
2
 
3
3
  require 'diff/lcs'
4
-
5
- # vim: ft=ruby
@@ -1,166 +1,166 @@
1
1
  # -*- ruby encoding: utf-8 -*-
2
2
 
3
- module Diff
4
- # = Diff::LCS 1.1.3
5
- # Computes "intelligent" differences between two sequenced Enumerables.
6
- # This is an implementation of the McIlroy-Hunt "diff" algorithm for
7
- # Enumerable objects that include Diffable.
8
- #
9
- # Based on Mario I. Wolczko's Smalltalk version (1.2, 1993) and Ned Konz's
10
- # Perl version (Algorithm::Diff 1.15).
11
- #
12
- # == Synopsis
13
- # require 'diff/lcs'
14
- #
15
- # seq1 = %w(a b c e h j l m n p)
16
- # seq2 = %w(b c d e f j k l m r s t)
17
- #
18
- # lcs = Diff::LCS.LCS(seq1, seq2)
19
- # diffs = Diff::LCS.diff(seq1, seq2)
20
- # sdiff = Diff::LCS.sdiff(seq1, seq2)
21
- # seq = Diff::LCS.traverse_sequences(seq1, seq2, callback_obj)
22
- # bal = Diff::LCS.traverse_balanced(seq1, seq2, callback_obj)
23
- # seq2 == Diff::LCS.patch(seq1, diffs)
24
- # seq2 == Diff::LCS.patch!(seq1, diffs)
25
- # seq1 == Diff::LCS.unpatch(seq2, diffs)
26
- # seq1 == Diff::LCS.unpatch!(seq2, diffs)
27
- # seq2 == Diff::LCS.patch(seq1, sdiff)
28
- # seq2 == Diff::LCS.patch!(seq1, sdiff)
29
- # seq1 == Diff::LCS.unpatch(seq2, sdiff)
30
- # seq1 == Diff::LCS.unpatch!(seq2, sdiff)
31
- #
32
- # Alternatively, objects can be extended with Diff::LCS:
33
- #
34
- # seq1.extend(Diff::LCS)
35
- # lcs = seq1.lcs(seq2)
36
- # diffs = seq1.diff(seq2)
37
- # sdiff = seq1.sdiff(seq2)
38
- # seq = seq1.traverse_sequences(seq2, callback_obj)
39
- # bal = seq1.traverse_balanced(seq2, callback_obj)
40
- # seq2 == seq1.patch(diffs)
41
- # seq2 == seq1.patch!(diffs)
42
- # seq1 == seq2.unpatch(diffs)
43
- # seq1 == seq2.unpatch!(diffs)
44
- # seq2 == seq1.patch(sdiff)
45
- # seq2 == seq1.patch!(sdiff)
46
- # seq1 == seq2.unpatch(sdiff)
47
- # seq1 == seq2.unpatch!(sdiff)
48
- #
49
- # Default extensions are provided for Array and String objects through the
50
- # use of 'diff/lcs/array' and 'diff/lcs/string'.
51
- #
52
- # == Introduction (by Mark-Jason Dominus)
53
- #
54
- # <em>The following text is from the Perl documentation. The only changes
55
- # have been to make the text appear better in Rdoc</em>.
56
- #
57
- # I once read an article written by the authors of +diff+; they said that
58
- # they hard worked very hard on the algorithm until they found the right
59
- # one.
60
- #
61
- # I think what they ended up using (and I hope someone will correct me,
62
- # because I am not very confident about this) was the `longest common
63
- # subsequence' method. In the LCS problem, you have two sequences of
64
- # items:
65
- #
66
- # a b c d f g h j q z
67
- # a b c d e f g i j k r x y z
68
- #
69
- # and you want to find the longest sequence of items that is present in
70
- # both original sequences in the same order. That is, you want to find a
71
- # new sequence *S* which can be obtained from the first sequence by
72
- # deleting some items, and from the second sequence by deleting other
73
- # items. You also want *S* to be as long as possible. In this case *S* is:
74
- #
75
- # a b c d f g j z
76
- #
77
- # From there it's only a small step to get diff-like output:
78
- #
79
- # e h i k q r x y
80
- # + - + + - + + +
81
- #
82
- # This module solves the LCS problem. It also includes a canned function
83
- # to generate +diff+-like output.
84
- #
85
- # It might seem from the example above that the LCS of two sequences is
86
- # always pretty obvious, but that's not always the case, especially when
87
- # the two sequences have many repeated elements. For example, consider
88
- #
89
- # a x b y c z p d q
90
- # a b c a x b y c z
91
- #
92
- # A naive approach might start by matching up the +a+ and +b+ that appear
93
- # at the beginning of each sequence, like this:
94
- #
95
- # a x b y c z p d q
96
- # a b c a b y c z
97
- #
98
- # This finds the common subsequence +a b c z+. But actually, the LCS is
99
- # +a x b y c z+:
100
- #
101
- # a x b y c z p d q
102
- # a b c a x b y c z
103
- #
104
- # == Author
105
- # This version is by Austin Ziegler <austin@rubyforge.org>.
106
- #
107
- # It is based on the Perl Algorithm::Diff (1.15) by Ned Konz , copyright
108
- # &copy; 2000&ndash;2002 and the Smalltalk diff version by Mario I.
109
- # Wolczko, copyright &copy; 1993. Documentation includes work by
110
- # Mark-Jason Dominus.
111
- #
112
- # == Licence
113
- # Copyright &copy; 2004 Austin Ziegler
114
- # This program is free software; you can redistribute it and/or modify it
115
- # under the same terms as Ruby, or alternatively under the Perl Artistic
116
- # licence.
117
- #
118
- # == Credits
119
- # Much of the documentation is taken directly from the Perl
120
- # Algorithm::Diff implementation and was written originally by Mark-Jason
121
- # Dominus and later by Ned Konz. The basic Ruby implementation was
122
- # re-ported from the Smalltalk implementation, available at
123
- # ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
124
- #
125
- # #sdiff and #traverse_balanced were written for the Perl version by Mike
126
- # Schilli <m@perlmeister.com>.
127
- #
128
- # "The algorithm is described in <em>A Fast Algorithm for Computing
129
- # Longest Common Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May
130
- # 1977, with a few minor improvements to improve the speed."
131
- module LCS
132
- VERSION = '1.1.3'
133
- end
3
+ module Diff; end unless defined? Diff
4
+ # = Diff::LCS 1.2.0
5
+ #
6
+ # Computes "intelligent" differences between two sequenced Enumerables. This
7
+ # is an implementation of the McIlroy-Hunt "diff" algorithm for Enumerable
8
+ # objects that include Diffable.
9
+ #
10
+ # Based on Mario I. Wolczko's Smalltalk version (1.2, 1993) and Ned Konz's
11
+ # Perl version (Algorithm::Diff 1.15).
12
+ #
13
+ # == Synopsis
14
+ # require 'diff/lcs'
15
+ #
16
+ # seq1 = %w(a b c e h j l m n p)
17
+ # seq2 = %w(b c d e f j k l m r s t)
18
+ #
19
+ # lcs = Diff::LCS.lcs(seq1, seq2)
20
+ # diffs = Diff::LCS.diff(seq1, seq2)
21
+ # sdiff = Diff::LCS.sdiff(seq1, seq2)
22
+ # seq = Diff::LCS.traverse_sequences(seq1, seq2, callback_obj)
23
+ # bal = Diff::LCS.traverse_balanced(seq1, seq2, callback_obj)
24
+ # seq2 == Diff::LCS.patch(seq1, diffs)
25
+ # seq2 == Diff::LCS.patch!(seq1, diffs)
26
+ # seq1 == Diff::LCS.unpatch(seq2, diffs)
27
+ # seq1 == Diff::LCS.unpatch!(seq2, diffs)
28
+ # seq2 == Diff::LCS.patch(seq1, sdiff)
29
+ # seq2 == Diff::LCS.patch!(seq1, sdiff)
30
+ # seq1 == Diff::LCS.unpatch(seq2, sdiff)
31
+ # seq1 == Diff::LCS.unpatch!(seq2, sdiff)
32
+ #
33
+ # Alternatively, objects can be extended with Diff::LCS:
34
+ #
35
+ # seq1.extend(Diff::LCS)
36
+ # lcs = seq1.lcs(seq2)
37
+ # diffs = seq1.diff(seq2)
38
+ # sdiff = seq1.sdiff(seq2)
39
+ # seq = seq1.traverse_sequences(seq2, callback_obj)
40
+ # bal = seq1.traverse_balanced(seq2, callback_obj)
41
+ # seq2 == seq1.patch(diffs)
42
+ # seq2 == seq1.patch!(diffs)
43
+ # seq1 == seq2.unpatch(diffs)
44
+ # seq1 == seq2.unpatch!(diffs)
45
+ # seq2 == seq1.patch(sdiff)
46
+ # seq2 == seq1.patch!(sdiff)
47
+ # seq1 == seq2.unpatch(sdiff)
48
+ # seq1 == seq2.unpatch!(sdiff)
49
+ #
50
+ # Default extensions are provided for Array and String objects through the
51
+ # use of 'diff/lcs/array' and 'diff/lcs/string'.
52
+ #
53
+ # == Introduction (by Mark-Jason Dominus)
54
+ #
55
+ # <em>The following text is from the Perl documentation. The only changes
56
+ # have been to make the text appear better in Rdoc</em>.
57
+ #
58
+ # I once read an article written by the authors of +diff+; they said that
59
+ # they hard worked very hard on the algorithm until they found the right
60
+ # one.
61
+ #
62
+ # I think what they ended up using (and I hope someone will correct me,
63
+ # because I am not very confident about this) was the `longest common
64
+ # subsequence' method. In the LCS problem, you have two sequences of items:
65
+ #
66
+ # a b c d f g h j q z
67
+ # a b c d e f g i j k r x y z
68
+ #
69
+ # and you want to find the longest sequence of items that is present in both
70
+ # original sequences in the same order. That is, you want to find a new
71
+ # sequence *S* which can be obtained from the first sequence by deleting
72
+ # some items, and from the second sequence by deleting other items. You also
73
+ # want *S* to be as long as possible. In this case *S* is:
74
+ #
75
+ # a b c d f g j z
76
+ #
77
+ # From there it's only a small step to get diff-like output:
78
+ #
79
+ # e h i k q r x y
80
+ # + - + + - + + +
81
+ #
82
+ # This module solves the LCS problem. It also includes a canned function to
83
+ # generate +diff+-like output.
84
+ #
85
+ # It might seem from the example above that the LCS of two sequences is
86
+ # always pretty obvious, but that's not always the case, especially when the
87
+ # two sequences have many repeated elements. For example, consider
88
+ #
89
+ # a x b y c z p d q
90
+ # a b c a x b y c z
91
+ #
92
+ # A naive approach might start by matching up the +a+ and +b+ that appear at
93
+ # the beginning of each sequence, like this:
94
+ #
95
+ # a x b y c z p d q
96
+ # a b c a b y c z
97
+ #
98
+ # This finds the common subsequence +a b c z+. But actually, the LCS is +a x
99
+ # b y c z+:
100
+ #
101
+ # a x b y c z p d q
102
+ # a b c a x b y c z
103
+ #
104
+ # == Author
105
+ # This version is by Austin Ziegler <austin@rubyforge.org>.
106
+ #
107
+ # It is based on the Perl Algorithm::Diff (1.15) by Ned Konz , copyright
108
+ # &copy; 2000&ndash;2002 and the Smalltalk diff version by Mario I.
109
+ # Wolczko, copyright &copy; 1993. Documentation includes work by
110
+ # Mark-Jason Dominus.
111
+ #
112
+ # == Licence
113
+ # Copyright &copy; 2004&ndash;2013 Austin Ziegler
114
+ # This program is free software; you can redistribute it and/or modify it
115
+ # under the same terms as Ruby, or alternatively under the Perl Artistic
116
+ # licence.
117
+ #
118
+ # == Credits
119
+ # Much of the documentation is taken directly from the Perl Algorithm::Diff
120
+ # implementation and was written originally by Mark-Jason Dominus and later
121
+ # by Ned Konz. The basic Ruby implementation was re-ported from the
122
+ # Smalltalk implementation, available at
123
+ # ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
124
+ #
125
+ # #sdiff and #traverse_balanced were written for the Perl version by Mike
126
+ # Schilli <m@perlmeister.com>.
127
+ #
128
+ # "The algorithm is described in <em>A Fast Algorithm for Computing Longest
129
+ # Common Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May
130
+ # 1977, with a few minor improvements to improve the speed."
131
+ module Diff::LCS
132
+ VERSION = '1.2.0'
134
133
  end
135
134
 
136
135
  require 'diff/lcs/callbacks'
136
+ require 'diff/lcs/internals'
137
137
 
138
138
  module Diff::LCS
139
139
  # Returns an Array containing the longest common subsequence(s) between
140
140
  # +self+ and +other+. See Diff::LCS#LCS.
141
141
  #
142
142
  # lcs = seq1.lcs(seq2)
143
- def lcs(other, &block) #:yields self[ii] if there are matched subsequences:
144
- Diff::LCS.LCS(self, other, &block)
143
+ def lcs(other, &block) #:yields self[i] if there are matched subsequences:
144
+ Diff::LCS.lcs(self, other, &block)
145
145
  end
146
146
 
147
147
  # Returns the difference set between +self+ and +other+. See
148
148
  # Diff::LCS#diff.
149
149
  def diff(other, callbacks = nil, &block)
150
- Diff::LCS::diff(self, other, callbacks, &block)
150
+ Diff::LCS.diff(self, other, callbacks, &block)
151
151
  end
152
152
 
153
153
  # Returns the balanced ("side-by-side") difference set between +self+ and
154
154
  # +other+. See Diff::LCS#sdiff.
155
155
  def sdiff(other, callbacks = nil, &block)
156
- Diff::LCS::sdiff(self, other, callbacks, &block)
156
+ Diff::LCS.sdiff(self, other, callbacks, &block)
157
157
  end
158
158
 
159
159
  # Traverses the discovered longest common subsequences between +self+ and
160
160
  # +other+. See Diff::LCS#traverse_sequences.
161
161
  def traverse_sequences(other, callbacks = nil, &block)
162
162
  traverse_sequences(self, other, callbacks ||
163
- Diff::LCS::YieldingCallbacks, &block)
163
+ Diff::LCS.YieldingCallbacks, &block)
164
164
  end
165
165
 
166
166
  # Traverses the discovered longest common subsequences between +self+ and
@@ -168,477 +168,401 @@ module Diff::LCS
168
168
  # Diff::LCS#traverse_balanced.
169
169
  def traverse_balanced(other, callbacks = nil, &block)
170
170
  traverse_balanced(self, other, callbacks ||
171
- Diff::LCS::YieldingCallbacks, &block)
171
+ Diff::LCS.YieldingCallbacks, &block)
172
172
  end
173
173
 
174
- # Attempts to patch a copy of +self+ with the provided +patchset+. See
175
- # Diff::LCS#patch.
174
+ # Attempts to patch +self+ with the provided +patchset+. A new sequence
175
+ # based on +self+ and the +patchset+ will be created. See Diff::LCS#patch.
176
+ # Attempts to autodiscover the direction of the patch.
176
177
  def patch(patchset)
177
- Diff::LCS::patch(self.dup, patchset)
178
+ Diff::LCS.patch(self, patchset)
178
179
  end
180
+ alias_method :unpatch, :patch
179
181
 
180
- # Attempts to unpatch a copy of +self+ with the provided +patchset+. See
181
- # Diff::LCS#patch.
182
- def unpatch(patchset)
183
- Diff::LCS::unpatch(self.dup, patchset)
184
- end
185
-
186
- # Attempts to patch +self+ with the provided +patchset+. See
187
- # Diff::LCS#patch!. Does no autodiscovery.
182
+ # Attempts to patch +self+ with the provided +patchset+. A new sequence
183
+ # based on +self+ and the +patchset+ will be created. See Diff::LCS#patch.
184
+ # Does no patch direction autodiscovery.
188
185
  def patch!(patchset)
189
- Diff::LCS::patch!(self, patchset)
186
+ Diff::LCS.patch!(self, patchset)
190
187
  end
191
188
 
192
- # Attempts to unpatch +self+ with the provided +patchset+. See
193
- # Diff::LCS#unpatch. Does no autodiscovery.
189
+ # Attempts to unpatch +self+ with the provided +patchset+. A new sequence
190
+ # based on +self+ and the +patchset+ will be created. See Diff::LCS#unpatch.
191
+ # Does no patch direction autodiscovery.
194
192
  def unpatch!(patchset)
195
- Diff::LCS::unpatch!(self, patchset)
193
+ Diff::LCS.unpatch!(self, patchset)
196
194
  end
197
- end
198
195
 
199
- module Diff::LCS
200
- class << self
201
- # Given two sequenced Enumerables, LCS returns an Array containing their
202
- # longest common subsequences.
203
- #
204
- # lcs = Diff::LCS.LCS(seq1, seq2)
205
- #
206
- # This array whose contents is such that:
207
- #
208
- # lcs.each_with_index do |ee, ii|
209
- # assert(ee.nil? || (seq1[ii] == seq2[ee]))
210
- # end
211
- #
212
- # If a block is provided, the matching subsequences will be yielded from
213
- # +seq1+ in turn and may be modified before they are placed into the
214
- # returned Array of subsequences.
215
- def LCS(seq1, seq2, &block) #:yields seq1[ii] for each matched:
216
- matches = Diff::LCS.__lcs(seq1, seq2)
217
- ret = []
218
- matches.each_with_index do |ee, ii|
219
- unless matches[ii].nil?
220
- if block_given?
221
- ret << (yield seq1[ii])
222
- else
223
- ret << seq1[ii]
224
- end
225
- end
226
- end
227
- ret
196
+ # Attempts to patch +self+ with the provided +patchset+, using #patch!. If
197
+ # the sequence this is used on supports #replace, the value of +self+ will
198
+ # be replaced. See Diff::LCS#patch. Does no patch direction autodiscovery.
199
+ def patch_me(patchset)
200
+ if respond_to? :replace
201
+ replace(patch!(patchset))
202
+ else
203
+ patch!(patchset)
228
204
  end
205
+ end
229
206
 
230
- # Diff::LCS.diff computes the smallest set of additions and deletions
231
- # necessary to turn the first sequence into the second, and returns a
232
- # description of these changes.
233
- #
234
- # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate
235
- # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If
236
- # a Class argument is provided for +callbacks+, #diff will attempt to
237
- # initialise it. If the +callbacks+ object (possibly initialised)
238
- # responds to #finish, it will be called.
239
- def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
240
- callbacks ||= Diff::LCS::DiffCallbacks
241
- if callbacks.kind_of?(Class)
242
- cb = callbacks.new rescue callbacks
243
- callbacks = cb
244
- end
245
- traverse_sequences(seq1, seq2, callbacks)
246
- callbacks.finish if callbacks.respond_to?(:finish)
207
+ # Attempts to unpatch +self+ with the provided +patchset+, using
208
+ # #unpatch!. If the sequence this is used on supports #replace, the value
209
+ # of +self+ will be replaced. See Diff::LCS#unpatch. Does no patch direction
210
+ # autodiscovery.
211
+ def unpatch_me(patchset)
212
+ if respond_to? :replace
213
+ replace(unpatch!(patchset))
214
+ else
215
+ unpatch!(patchset)
216
+ end
217
+ end
218
+ end
247
219
 
248
- if block_given?
249
- res = callbacks.diffs.map do |hunk|
250
- if hunk.kind_of?(Array)
251
- hunk = hunk.map { |hunk_block| yield hunk_block }
252
- else
253
- yield hunk
254
- end
255
- end
256
- res
257
- else
258
- callbacks.diffs
220
+ class << Diff::LCS
221
+ def lcs(seq1, seq2, &block) #:yields seq1[i] for each matched:
222
+ matches = Diff::LCS::Internals.lcs(seq1, seq2)
223
+ ret = []
224
+ string = seq1.kind_of? String
225
+ matches.each_with_index do |e, i|
226
+ unless matches[i].nil?
227
+ v = string ? seq1[i, 1] : seq1[i]
228
+ v = block[v] if block
229
+ ret << v
259
230
  end
260
231
  end
232
+ ret
233
+ end
234
+ alias_method :LCS, :lcs
261
235
 
262
- # Diff::LCS.sdiff computes all necessary components to show two sequences
263
- # and their minimized differences side by side, just like the Unix
264
- # utility <em>sdiff</em> does:
265
- #
266
- # old < -
267
- # same same
268
- # before | after
269
- # - > new
270
- #
271
- # See Diff::LCS::SDiffCallbacks for the default behaviour. An alternate
272
- # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If
273
- # a Class argument is provided for +callbacks+, #diff will attempt to
274
- # initialise it. If the +callbacks+ object (possibly initialised)
275
- # responds to #finish, it will be called.
276
- def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
277
- callbacks ||= Diff::LCS::SDiffCallbacks
278
- if callbacks.kind_of?(Class)
279
- cb = callbacks.new rescue callbacks
280
- callbacks = cb
281
- end
282
- traverse_balanced(seq1, seq2, callbacks)
283
- callbacks.finish if callbacks.respond_to?(:finish)
236
+ # #diff computes the smallest set of additions and deletions necessary to
237
+ # turn the first sequence into the second, and returns a description of
238
+ # these changes.
239
+ #
240
+ # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate
241
+ # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If a
242
+ # Class argument is provided for +callbacks+, #diff will attempt to
243
+ # initialise it. If the +callbacks+ object (possibly initialised) responds
244
+ # to #finish, it will be called.
245
+ def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
246
+ diff_traversal(:diff, seq1, seq2, callbacks || Diff::LCS::DiffCallbacks,
247
+ &block)
248
+ end
284
249
 
285
- if block_given?
286
- res = callbacks.diffs.map do |hunk|
287
- if hunk.kind_of?(Array)
288
- hunk = hunk.map { |hunk_block| yield hunk_block }
289
- else
290
- yield hunk
291
- end
250
+ # #sdiff computes all necessary components to show two sequences and their
251
+ # minimized differences side by side, just like the Unix utility
252
+ # <em>sdiff</em> does:
253
+ #
254
+ # old < -
255
+ # same same
256
+ # before | after
257
+ # - > new
258
+ #
259
+ # See Diff::LCS::SDiffCallbacks for the default behaviour. An alternate
260
+ # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If a
261
+ # Class argument is provided for +callbacks+, #diff will attempt to
262
+ # initialise it. If the +callbacks+ object (possibly initialised) responds
263
+ # to #finish, it will be called.
264
+ def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
265
+ diff_traversal(:sdiff, seq1, seq2, callbacks || Diff::LCS::SDiffCallbacks,
266
+ &block)
267
+ end
268
+
269
+ # #traverse_sequences is the most general facility provided by this
270
+ # module; #diff and #lcs are implemented as calls to it.
271
+ #
272
+ # The arguments to #traverse_sequences are the two sequences to traverse,
273
+ # and a callback object, like this:
274
+ #
275
+ # traverse_sequences(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
276
+ #
277
+ # == Callback Methods
278
+ #
279
+ # Optional callback methods are <em>emphasized</em>.
280
+ #
281
+ # callbacks#match:: Called when +a+ and +b+ are pointing to
282
+ # common elements in +A+ and +B+.
283
+ # callbacks#discard_a:: Called when +a+ is pointing to an
284
+ # element not in +B+.
285
+ # callbacks#discard_b:: Called when +b+ is pointing to an
286
+ # element not in +A+.
287
+ # <em>callbacks#finished_a</em>:: Called when +a+ has reached the end of
288
+ # sequence +A+.
289
+ # <em>callbacks#finished_b</em>:: Called when +b+ has reached the end of
290
+ # sequence +B+.
291
+ #
292
+ # == Algorithm
293
+ #
294
+ # a---+
295
+ # v
296
+ # A = a b c e h j l m n p
297
+ # B = b c d e f j k l m r s t
298
+ # ^
299
+ # b---+
300
+ #
301
+ # If there are two arrows (+a+ and +b+) pointing to elements of sequences
302
+ # +A+ and +B+, the arrows will initially point to the first elements of
303
+ # their respective sequences. #traverse_sequences will advance the arrows
304
+ # through the sequences one element at a time, calling a method on the
305
+ # user-specified callback object before each advance. It will advance the
306
+ # arrows in such a way that if there are elements <tt>A[i]</tt> and
307
+ # <tt>B[j]</tt> which are both equal and part of the longest common
308
+ # subsequence, there will be some moment during the execution of
309
+ # #traverse_sequences when arrow +a+ is pointing to <tt>A[i]</tt> and
310
+ # arrow +b+ is pointing to <tt>B[j]</tt>. When this happens,
311
+ # #traverse_sequences will call <tt>callbacks#match</tt> and then it will
312
+ # advance both arrows.
313
+ #
314
+ # Otherwise, one of the arrows is pointing to an element of its sequence
315
+ # that is not part of the longest common subsequence. #traverse_sequences
316
+ # will advance that arrow and will call <tt>callbacks#discard_a</tt> or
317
+ # <tt>callbacks#discard_b</tt>, depending on which arrow it advanced. If
318
+ # both arrows point to elements that are not part of the longest common
319
+ # subsequence, then #traverse_sequences will advance one of them and call
320
+ # the appropriate callback, but it is not specified which it will call.
321
+ #
322
+ # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
323
+ # and <tt>callbacks#discard_b</tt> are invoked with an event comprising
324
+ # the action ("=", "+", or "-", respectively), the indicies +i+ and +j+,
325
+ # and the elements <tt>A[i]</tt> and <tt>B[j]</tt>. Return values are
326
+ # discarded by #traverse_sequences.
327
+ #
328
+ # === End of Sequences
329
+ #
330
+ # If arrow +a+ reaches the end of its sequence before arrow +b+ does,
331
+ # #traverse_sequence will try to call <tt>callbacks#finished_a</tt> with
332
+ # the last index and element of +A+ (<tt>A[-1]</tt>) and the current index
333
+ # and element of +B+ (<tt>B[j]</tt>). If <tt>callbacks#finished_a</tt>
334
+ # does not exist, then <tt>callbacks#discard_b</tt> will be called on each
335
+ # element of +B+ until the end of the sequence is reached (the call will
336
+ # be done with <tt>A[-1]</tt> and <tt>B[j]</tt> for each element).
337
+ #
338
+ # If +b+ reaches the end of +B+ before +a+ reaches the end of +A+,
339
+ # <tt>callbacks#finished_b</tt> will be called with the current index and
340
+ # element of +A+ (<tt>A[i]</tt>) and the last index and element of +B+
341
+ # (<tt>A[-1]</tt>). Again, if <tt>callbacks#finished_b</tt> does not exist
342
+ # on the callback object, then <tt>callbacks#discard_a</tt> will be called
343
+ # on each element of +A+ until the end of the sequence is reached
344
+ # (<tt>A[i]</tt> and <tt>B[-1]</tt>).
345
+ #
346
+ # There is a chance that one additional <tt>callbacks#discard_a</tt> or
347
+ # <tt>callbacks#discard_b</tt> will be called after the end of the
348
+ # sequence is reached, if +a+ has not yet reached the end of +A+ or +b+
349
+ # has not yet reached the end of +B+.
350
+ def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks, &block) #:yields change events:
351
+ callbacks ||= Diff::LCS::SequenceCallbacks
352
+ matches = Diff::LCS::Internals.lcs(seq1, seq2)
353
+
354
+ run_finished_a = run_finished_b = false
355
+ string = seq1.kind_of?(String)
356
+
357
+ a_size = seq1.size
358
+ b_size = seq2.size
359
+ ai = bj = 0
360
+
361
+ (0..matches.size).each do |i|
362
+ b_line = matches[i]
363
+
364
+ ax = string ? seq1[i, 1] : seq1[i]
365
+ bx = string ? seq2[bj, 1] : seq2[bj]
366
+
367
+ if b_line.nil?
368
+ unless ax.nil? or (string and ax.empty?)
369
+ event = Diff::LCS::ContextChange.new('-', i, ax, bj, bx)
370
+ event = yield event if block_given?
371
+ callbacks.discard_a(event)
292
372
  end
293
- res
294
373
  else
295
- callbacks.diffs
374
+ loop do
375
+ break unless bj < b_line
376
+ bx = string ? seq2[bj, 1] : seq2[bj]
377
+ event = Diff::LCS::ContextChange.new('+', i, ax, bj, bx)
378
+ event = yield event if block_given?
379
+ callbacks.discard_b(event)
380
+ bj += 1
381
+ end
382
+ bx = string ? seq2[bj, 1] : seq2[bj]
383
+ event = Diff::LCS::ContextChange.new('=', i, ax, bj, bx)
384
+ event = yield event if block_given?
385
+ callbacks.match(event)
386
+ bj += 1
296
387
  end
388
+ ai = i
297
389
  end
298
-
299
- # Diff::LCS.traverse_sequences is the most general facility provided by this
300
- # module; +diff+ and +LCS+ are implemented as calls to it.
301
- #
302
- # The arguments to #traverse_sequences are the two sequences to
303
- # traverse, and a callback object, like this:
304
- #
305
- # traverse_sequences(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
306
- #
307
- # #diff is implemented with #traverse_sequences.
308
- #
309
- # == Callback Methods
310
- # Optional callback methods are <em>emphasized</em>.
311
- #
312
- # callbacks#match:: Called when +a+ and +b+ are pointing
313
- # to common elements in +A+ and +B+.
314
- # callbacks#discard_a:: Called when +a+ is pointing to an
315
- # element not in +B+.
316
- # callbacks#discard_b:: Called when +b+ is pointing to an
317
- # element not in +A+.
318
- # <em>callbacks#finished_a</em>:: Called when +a+ has reached the end of
319
- # sequence +A+.
320
- # <em>callbacks#finished_b</em>:: Called when +b+ has reached the end of
321
- # sequence +B+.
322
- #
323
- # == Algorithm
324
- # a---+
325
- # v
326
- # A = a b c e h j l m n p
327
- # B = b c d e f j k l m r s t
328
- # ^
329
- # b---+
330
- #
331
- # If there are two arrows (+a+ and +b+) pointing to elements of
332
- # sequences +A+ and +B+, the arrows will initially point to the first
333
- # elements of their respective sequences. #traverse_sequences will
334
- # advance the arrows through the sequences one element at a time,
335
- # calling a method on the user-specified callback object before each
336
- # advance. It will advance the arrows in such a way that if there are
337
- # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
338
- # part of the longest common subsequence, there will be some moment
339
- # during the execution of #traverse_sequences when arrow +a+ is pointing
340
- # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
341
- # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
342
- # and then it will advance both arrows.
343
- #
344
- # Otherwise, one of the arrows is pointing to an element of its sequence
345
- # that is not part of the longest common subsequence.
346
- # #traverse_sequences will advance that arrow and will call
347
- # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>, depending
348
- # on which arrow it advanced. If both arrows point to elements that are
349
- # not part of the longest common subsequence, then #traverse_sequences
350
- # will advance one of them and call the appropriate callback, but it is
351
- # not specified which it will call.
352
- #
353
- # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
354
- # and <tt>callbacks#discard_b</tt> are invoked with an event comprising
355
- # the action ("=", "+", or "-", respectively), the indicies +ii+ and
356
- # +jj+, and the elements <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return
357
- # values are discarded by #traverse_sequences.
358
- #
359
- # === End of Sequences
360
- # If arrow +a+ reaches the end of its sequence before arrow +b+ does,
361
- # #traverse_sequence will try to call <tt>callbacks#finished_a</tt> with
362
- # the last index and element of +A+ (<tt>A[-1]</tt>) and the current
363
- # index and element of +B+ (<tt>B[jj]</tt>). If
364
- # <tt>callbacks#finished_a</tt> does not exist, then
365
- # <tt>callbacks#discard_b</tt> will be called on each element of +B+
366
- # until the end of the sequence is reached (the call
367
- # will be done with <tt>A[-1]</tt> and <tt>B[jj]</tt> for each element).
368
- #
369
- # If +b+ reaches the end of +B+ before +a+ reaches the end of +A+,
370
- # <tt>callbacks#finished_b</tt> will be called with the current index
371
- # and element of +A+ (<tt>A[ii]</tt>) and the last index and element of
372
- # +B+ (<tt>A[-1]</tt>). Again, if <tt>callbacks#finished_b</tt> does not
373
- # exist on the callback object, then <tt>callbacks#discard_a</tt> will
374
- # be called on each element of +A+ until the end of the sequence is
375
- # reached (<tt>A[ii]</tt> and <tt>B[-1]</tt>).
376
- #
377
- # There is a chance that one additional <tt>callbacks#discard_a</tt> or
378
- # <tt>callbacks#discard_b</tt> will be called after the end of the
379
- # sequence is reached, if +a+ has not yet reached the end of +A+ or +b+
380
- # has not yet reached the end of +B+.
381
- def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks, &block) #:yields change events:
382
- matches = Diff::LCS.__lcs(seq1, seq2)
383
-
384
- run_finished_a = run_finished_b = false
385
- string = seq1.kind_of?(String)
386
-
387
- a_size = seq1.size
388
- b_size = seq2.size
389
- ai = bj = 0
390
-
391
- (0 .. matches.size).each do |ii|
392
- b_line = matches[ii]
393
-
394
- ax = string ? seq1[ii, 1] : seq1[ii]
395
- bx = string ? seq2[bj, 1] : seq2[bj]
396
-
397
- if b_line.nil?
398
- unless ax.nil?
399
- event = Diff::LCS::ContextChange.new('-', ii, ax, bj, bx)
400
- event = yield event if block_given?
401
- callbacks.discard_a(event)
402
- end
390
+ ai += 1
391
+
392
+ # The last entry (if any) processed was a match. +ai+ and +bj+ point
393
+ # just past the last matching lines in their sequences.
394
+ while (ai < a_size) or (bj < b_size)
395
+ # last A?
396
+ if ai == a_size and bj < b_size
397
+ if callbacks.respond_to?(:finished_a) and not run_finished_a
398
+ ax = string ? seq1[-1, 1] : seq1[-1]
399
+ bx = string ? seq2[bj, 1] : seq2[bj]
400
+ event = Diff::LCS::ContextChange.new('>', (a_size - 1), ax, bj, bx)
401
+ event = yield event if block_given?
402
+ callbacks.finished_a(event)
403
+ run_finished_a = true
403
404
  else
405
+ ax = string ? seq1[ai, 1] : seq1[ai]
404
406
  loop do
405
- break unless bj < b_line
406
407
  bx = string ? seq2[bj, 1] : seq2[bj]
407
- event = Diff::LCS::ContextChange.new('+', ii, ax, bj, bx)
408
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
408
409
  event = yield event if block_given?
409
410
  callbacks.discard_b(event)
410
411
  bj += 1
412
+ break unless bj < b_size
411
413
  end
412
- bx = string ? seq2[bj, 1] : seq2[bj]
413
- event = Diff::LCS::ContextChange.new('=', ii, ax, bj, bx)
414
- event = yield event if block_given?
415
- callbacks.match(event)
416
- bj += 1
417
414
  end
418
- ai = ii
419
415
  end
420
- ai += 1
421
-
422
- # The last entry (if any) processed was a match. +ai+ and +bj+ point
423
- # just past the last matching lines in their sequences.
424
- while (ai < a_size) or (bj < b_size)
425
- # last A?
426
- if ai == a_size and bj < b_size
427
- if callbacks.respond_to?(:finished_a) and not run_finished_a
428
- ax = string ? seq1[-1, 1] : seq1[-1]
429
- bx = string ? seq2[bj, 1] : seq2[bj]
430
- event = Diff::LCS::ContextChange.new('>', (a_size - 1), ax, bj, bx)
431
- event = yield event if block_given?
432
- callbacks.finished_a(event)
433
- run_finished_a = true
434
- else
435
- ax = string ? seq1[ai, 1] : seq1[ai]
436
- loop do
437
- bx = string ? seq2[bj, 1] : seq2[bj]
438
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
439
- event = yield event if block_given?
440
- callbacks.discard_b(event)
441
- bj += 1
442
- break unless bj < b_size
443
- end
444
- end
445
- end
446
416
 
447
- # last B?
448
- if bj == b_size and ai < a_size
449
- if callbacks.respond_to?(:finished_b) and not run_finished_b
450
- ax = string ? seq1[ai, 1] : seq1[ai]
451
- bx = string ? seq2[-1, 1] : seq2[-1]
452
- event = Diff::LCS::ContextChange.new('<', ai, ax, (b_size - 1), bx)
453
- event = yield event if block_given?
454
- callbacks.finished_b(event)
455
- run_finished_b = true
456
- else
457
- bx = string ? seq2[bj, 1] : seq2[bj]
458
- loop do
459
- ax = string ? seq1[ai, 1] : seq1[ai]
460
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
461
- event = yield event if block_given?
462
- callbacks.discard_a(event)
463
- ai += 1
464
- break unless bj < b_size
465
- end
466
- end
467
- end
468
-
469
- if ai < a_size
470
- ax = string ? seq1[ai, 1] : seq1[ai]
471
- bx = string ? seq2[bj, 1] : seq2[bj]
472
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
473
- event = yield event if block_given?
474
- callbacks.discard_a(event)
475
- ai += 1
476
- end
477
-
478
- if bj < b_size
417
+ # last B?
418
+ if bj == b_size and ai < a_size
419
+ if callbacks.respond_to?(:finished_b) and not run_finished_b
479
420
  ax = string ? seq1[ai, 1] : seq1[ai]
480
- bx = string ? seq2[bj, 1] : seq2[bj]
481
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
421
+ bx = string ? seq2[-1, 1] : seq2[-1]
422
+ event = Diff::LCS::ContextChange.new('<', ai, ax, (b_size - 1), bx)
482
423
  event = yield event if block_given?
483
- callbacks.discard_b(event)
484
- bj += 1
485
- end
486
- end
487
- end
488
-
489
- # #traverse_balanced is an alternative to #traverse_sequences. It
490
- # uses a different algorithm to iterate through the entries in the
491
- # computed longest common subsequence. Instead of viewing the changes as
492
- # insertions or deletions from one of the sequences, #traverse_balanced
493
- # will report <em>changes</em> between the sequences. To represent a
494
- #
495
- # The arguments to #traverse_balanced are the two sequences to traverse
496
- # and a callback object, like this:
497
- #
498
- # traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
499
- #
500
- # #sdiff is implemented with #traverse_balanced.
501
- #
502
- # == Callback Methods
503
- # Optional callback methods are <em>emphasized</em>.
504
- #
505
- # callbacks#match:: Called when +a+ and +b+ are pointing
506
- # to common elements in +A+ and +B+.
507
- # callbacks#discard_a:: Called when +a+ is pointing to an
508
- # element not in +B+.
509
- # callbacks#discard_b:: Called when +b+ is pointing to an
510
- # element not in +A+.
511
- # <em>callbacks#change</em>:: Called when +a+ and +b+ are pointing
512
- # to the same relative position, but
513
- # <tt>A[a]</tt> and <tt>B[b]</tt> are
514
- # not the same; a <em>change</em> has
515
- # occurred.
516
- #
517
- # #traverse_balanced might be a bit slower than #traverse_sequences,
518
- # noticable only while processing huge amounts of data.
519
- #
520
- # The +sdiff+ function of this module is implemented as call to
521
- # #traverse_balanced.
522
- #
523
- # == Algorithm
524
- # a---+
525
- # v
526
- # A = a b c e h j l m n p
527
- # B = b c d e f j k l m r s t
528
- # ^
529
- # b---+
530
- #
531
- # === Matches
532
- # If there are two arrows (+a+ and +b+) pointing to elements of
533
- # sequences +A+ and +B+, the arrows will initially point to the first
534
- # elements of their respective sequences. #traverse_sequences will
535
- # advance the arrows through the sequences one element at a time,
536
- # calling a method on the user-specified callback object before each
537
- # advance. It will advance the arrows in such a way that if there are
538
- # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
539
- # part of the longest common subsequence, there will be some moment
540
- # during the execution of #traverse_sequences when arrow +a+ is pointing
541
- # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
542
- # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
543
- # and then it will advance both arrows.
544
- #
545
- # === Discards
546
- # Otherwise, one of the arrows is pointing to an element of its sequence
547
- # that is not part of the longest common subsequence.
548
- # #traverse_sequences will advance that arrow and will call
549
- # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>,
550
- # depending on which arrow it advanced.
551
- #
552
- # === Changes
553
- # If both +a+ and +b+ point to elements that are not part of the longest
554
- # common subsequence, then #traverse_sequences will try to call
555
- # <tt>callbacks#change</tt> and advance both arrows. If
556
- # <tt>callbacks#change</tt> is not implemented, then
557
- # <tt>callbacks#discard_a</tt> and <tt>callbacks#discard_b</tt> will be
558
- # called in turn.
559
- #
560
- # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
561
- # <tt>callbacks#discard_b</tt>, and <tt>callbacks#change</tt> are
562
- # invoked with an event comprising the action ("=", "+", "-", or "!",
563
- # respectively), the indicies +ii+ and +jj+, and the elements
564
- # <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return values are discarded by
565
- # #traverse_balanced.
566
- #
567
- # === Context
568
- # Note that +ii+ and +jj+ may not be the same index position, even if
569
- # +a+ and +b+ are considered to be pointing to matching or changed
570
- # elements.
571
- def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
572
- matches = Diff::LCS.__lcs(seq1, seq2)
573
- a_size = seq1.size
574
- b_size = seq2.size
575
- ai = bj = mb = 0
576
- ma = -1
577
- string = seq1.kind_of?(String)
578
-
579
- # Process all the lines in the match vector.
580
- loop do
581
- # Find next match indices +ma+ and +mb+
582
- loop do
583
- ma += 1
584
- break unless ma < matches.size and matches[ma].nil?
585
- end
586
-
587
- break if ma >= matches.size # end of matches?
588
- mb = matches[ma]
589
-
590
- # Change(seq2)
591
- while (ai < ma) or (bj < mb)
592
- ax = string ? seq1[ai, 1] : seq1[ai]
424
+ callbacks.finished_b(event)
425
+ run_finished_b = true
426
+ else
593
427
  bx = string ? seq2[bj, 1] : seq2[bj]
594
-
595
- case [(ai < ma), (bj < mb)]
596
- when [true, true]
597
- if callbacks.respond_to?(:change)
598
- event = Diff::LCS::ContextChange.new('!', ai, ax, bj, bx)
599
- event = yield event if block_given?
600
- callbacks.change(event)
601
- ai += 1
602
- bj += 1
603
- else
604
- event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
605
- event = yield event if block_given?
606
- callbacks.discard_a(event)
607
- ai += 1
608
- ax = string ? seq1[ai, 1] : seq1[ai]
609
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
610
- event = yield event if block_given?
611
- callbacks.discard_b(event)
612
- bj += 1
613
- end
614
- when [true, false]
428
+ loop do
429
+ ax = string ? seq1[ai, 1] : seq1[ai]
615
430
  event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
616
431
  event = yield event if block_given?
617
432
  callbacks.discard_a(event)
618
433
  ai += 1
619
- when [false, true]
620
- event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
621
- event = yield event if block_given?
622
- callbacks.discard_b(event)
623
- bj += 1
434
+ break unless bj < b_size
624
435
  end
625
436
  end
437
+ end
626
438
 
627
- # Match
439
+ if ai < a_size
628
440
  ax = string ? seq1[ai, 1] : seq1[ai]
629
441
  bx = string ? seq2[bj, 1] : seq2[bj]
630
- event = Diff::LCS::ContextChange.new('=', ai, ax, bj, bx)
442
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
631
443
  event = yield event if block_given?
632
- callbacks.match(event)
444
+ callbacks.discard_a(event)
633
445
  ai += 1
446
+ end
447
+
448
+ if bj < b_size
449
+ ax = string ? seq1[ai, 1] : seq1[ai]
450
+ bx = string ? seq2[bj, 1] : seq2[bj]
451
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
452
+ event = yield event if block_given?
453
+ callbacks.discard_b(event)
634
454
  bj += 1
635
455
  end
456
+ end
457
+ end
636
458
 
637
- while (ai < a_size) or (bj < b_size)
459
+ # #traverse_balanced is an alternative to #traverse_sequences. It uses a
460
+ # different algorithm to iterate through the entries in the computed
461
+ # longest common subsequence. Instead of viewing the changes as insertions
462
+ # or deletions from one of the sequences, #traverse_balanced will report
463
+ # <em>changes</em> between the sequences.
464
+ #
465
+ # The arguments to #traverse_balanced are the two sequences to traverse
466
+ # and a callback object, like this:
467
+ #
468
+ # traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
469
+ #
470
+ # #sdiff is implemented with #traverse_balanced.
471
+ #
472
+ # == Callback Methods
473
+ #
474
+ # Optional callback methods are <em>emphasized</em>.
475
+ #
476
+ # callbacks#match:: Called when +a+ and +b+ are pointing to
477
+ # common elements in +A+ and +B+.
478
+ # callbacks#discard_a:: Called when +a+ is pointing to an
479
+ # element not in +B+.
480
+ # callbacks#discard_b:: Called when +b+ is pointing to an
481
+ # element not in +A+.
482
+ # <em>callbacks#change</em>:: Called when +a+ and +b+ are pointing to
483
+ # the same relative position, but
484
+ # <tt>A[a]</tt> and <tt>B[b]</tt> are not
485
+ # the same; a <em>change</em> has
486
+ # occurred.
487
+ #
488
+ # #traverse_balanced might be a bit slower than #traverse_sequences,
489
+ # noticable only while processing huge amounts of data.
490
+ #
491
+ # == Algorithm
492
+ #
493
+ # a---+
494
+ # v
495
+ # A = a b c e h j l m n p
496
+ # B = b c d e f j k l m r s t
497
+ # ^
498
+ # b---+
499
+ #
500
+ # === Matches
501
+ #
502
+ # If there are two arrows (+a+ and +b+) pointing to elements of sequences
503
+ # +A+ and +B+, the arrows will initially point to the first elements of
504
+ # their respective sequences. #traverse_sequences will advance the arrows
505
+ # through the sequences one element at a time, calling a method on the
506
+ # user-specified callback object before each advance. It will advance the
507
+ # arrows in such a way that if there are elements <tt>A[i]</tt> and
508
+ # <tt>B[j]</tt> which are both equal and part of the longest common
509
+ # subsequence, there will be some moment during the execution of
510
+ # #traverse_sequences when arrow +a+ is pointing to <tt>A[i]</tt> and
511
+ # arrow +b+ is pointing to <tt>B[j]</tt>. When this happens,
512
+ # #traverse_sequences will call <tt>callbacks#match</tt> and then it will
513
+ # advance both arrows.
514
+ #
515
+ # === Discards
516
+ #
517
+ # Otherwise, one of the arrows is pointing to an element of its sequence
518
+ # that is not part of the longest common subsequence. #traverse_sequences
519
+ # will advance that arrow and will call <tt>callbacks#discard_a</tt> or
520
+ # <tt>callbacks#discard_b</tt>, depending on which arrow it advanced.
521
+ #
522
+ # === Changes
523
+ #
524
+ # If both +a+ and +b+ point to elements that are not part of the longest
525
+ # common subsequence, then #traverse_sequences will try to call
526
+ # <tt>callbacks#change</tt> and advance both arrows. If
527
+ # <tt>callbacks#change</tt> is not implemented, then
528
+ # <tt>callbacks#discard_a</tt> and <tt>callbacks#discard_b</tt> will be
529
+ # called in turn.
530
+ #
531
+ # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
532
+ # <tt>callbacks#discard_b</tt>, and <tt>callbacks#change</tt> are invoked
533
+ # with an event comprising the action ("=", "+", "-", or "!",
534
+ # respectively), the indicies +i+ and +j+, and the elements
535
+ # <tt>A[i]</tt> and <tt>B[j]</tt>. Return values are discarded by
536
+ # #traverse_balanced.
537
+ #
538
+ # === Context
539
+ # Note that +i+ and +j+ may not be the same index position, even if +a+
540
+ # and +b+ are considered to be pointing to matching or changed elements.
541
+ def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
542
+ matches = Diff::LCS::Internals.lcs(seq1, seq2)
543
+ a_size = seq1.size
544
+ b_size = seq2.size
545
+ ai = bj = mb = 0
546
+ ma = -1
547
+ string = seq1.kind_of?(String)
548
+
549
+ # Process all the lines in the match vector.
550
+ loop do
551
+ # Find next match indices +ma+ and +mb+
552
+ loop do
553
+ ma += 1
554
+ break unless ma < matches.size and matches[ma].nil?
555
+ end
556
+
557
+ break if ma >= matches.size # end of matches?
558
+ mb = matches[ma]
559
+
560
+ # Change(seq2)
561
+ while (ai < ma) or (bj < mb)
638
562
  ax = string ? seq1[ai, 1] : seq1[ai]
639
563
  bx = string ? seq2[bj, 1] : seq2[bj]
640
564
 
641
- case [(ai < a_size), (bj < b_size)]
565
+ case [(ai < ma), (bj < mb)]
642
566
  when [true, true]
643
567
  if callbacks.respond_to?(:change)
644
568
  event = Diff::LCS::ContextChange.new('!', ai, ax, bj, bx)
@@ -669,437 +593,213 @@ module Diff::LCS
669
593
  bj += 1
670
594
  end
671
595
  end
672
- end
673
-
674
- PATCH_MAP = { #:nodoc:
675
- :patch => { '+' => '+', '-' => '-', '!' => '!', '=' => '=' },
676
- :unpatch => { '+' => '-', '-' => '+', '!' => '!', '=' => '=' }
677
- }
678
-
679
- # Given a patchset, convert the current version to the new
680
- # version. If +direction+ is not specified (must be
681
- # <tt>:patch</tt> or <tt>:unpatch</tt>), then discovery of the
682
- # direction of the patch will be attempted.
683
- def patch(src, patchset, direction = nil)
684
- string = src.kind_of?(String)
685
- # Start with a new empty type of the source's class
686
- res = src.class.new
687
-
688
- # Normalize the patchset.
689
- patchset = __normalize_patchset(patchset)
690
-
691
- direction ||= Diff::LCS.__diff_direction(src, patchset)
692
- direction ||= :patch
693
-
694
- ai = bj = 0
695
-
696
- patchset.each do |change|
697
- # Both Change and ContextChange support #action
698
- action = PATCH_MAP[direction][change.action]
699
-
700
- case change
701
- when Diff::LCS::ContextChange
702
- case direction
703
- when :patch
704
- el = change.new_element
705
- op = change.old_position
706
- np = change.new_position
707
- when :unpatch
708
- el = change.old_element
709
- op = change.new_position
710
- np = change.old_position
711
- end
712
-
713
- case action
714
- when '-' # Remove details from the old string
715
- while ai < op
716
- res << (string ? src[ai, 1] : src[ai])
717
- ai += 1
718
- bj += 1
719
- end
720
- ai += 1
721
- when '+'
722
- while bj < np
723
- res << (string ? src[ai, 1] : src[ai])
724
- ai += 1
725
- bj += 1
726
- end
727
-
728
- res << el
729
- bj += 1
730
- when '='
731
- # This only appears in sdiff output with the SDiff callback.
732
- # Therefore, we only need to worry about dealing with a single
733
- # element.
734
- res << el
735
-
736
- ai += 1
737
- bj += 1
738
- when '!'
739
- while ai < op
740
- res << (string ? src[ai, 1] : src[ai])
741
- ai += 1
742
- bj += 1
743
- end
744
596
 
745
- bj += 1
746
- ai += 1
747
-
748
- res << el
749
- end
750
- when Diff::LCS::Change
751
- case action
752
- when '-'
753
- while ai < change.position
754
- res << (string ? src[ai, 1] : src[ai])
755
- ai += 1
756
- bj += 1
757
- end
758
- ai += 1
759
- when '+'
760
- while bj < change.position
761
- res << (string ? src[ai, 1] : src[ai])
762
- ai += 1
763
- bj += 1
764
- end
597
+ # Match
598
+ ax = string ? seq1[ai, 1] : seq1[ai]
599
+ bx = string ? seq2[bj, 1] : seq2[bj]
600
+ event = Diff::LCS::ContextChange.new('=', ai, ax, bj, bx)
601
+ event = yield event if block_given?
602
+ callbacks.match(event)
603
+ ai += 1
604
+ bj += 1
605
+ end
765
606
 
766
- bj += 1
607
+ while (ai < a_size) or (bj < b_size)
608
+ ax = string ? seq1[ai, 1] : seq1[ai]
609
+ bx = string ? seq2[bj, 1] : seq2[bj]
767
610
 
768
- res << change.element
769
- end
611
+ case [(ai < a_size), (bj < b_size)]
612
+ when [true, true]
613
+ if callbacks.respond_to?(:change)
614
+ event = Diff::LCS::ContextChange.new('!', ai, ax, bj, bx)
615
+ event = yield event if block_given?
616
+ callbacks.change(event)
617
+ ai += 1
618
+ bj += 1
619
+ else
620
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
621
+ event = yield event if block_given?
622
+ callbacks.discard_a(event)
623
+ ai += 1
624
+ ax = string ? seq1[ai, 1] : seq1[ai]
625
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
626
+ event = yield event if block_given?
627
+ callbacks.discard_b(event)
628
+ bj += 1
770
629
  end
771
- end
772
-
773
- while ai < src.size
774
- res << (string ? src[ai, 1] : src[ai])
630
+ when [true, false]
631
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
632
+ event = yield event if block_given?
633
+ callbacks.discard_a(event)
775
634
  ai += 1
635
+ when [false, true]
636
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
637
+ event = yield event if block_given?
638
+ callbacks.discard_b(event)
776
639
  bj += 1
777
640
  end
778
-
779
- res
780
641
  end
642
+ end
781
643
 
782
- # Given a set of patchset, convert the current version to the prior
783
- # version. Does no auto-discovery.
784
- def unpatch!(src, patchset)
785
- Diff::LCS.patch(src, patchset, :unpatch)
786
- end
644
+ PATCH_MAP = { #:nodoc:
645
+ :patch => { '+' => '+', '-' => '-', '!' => '!', '=' => '=' },
646
+ :unpatch => { '+' => '-', '-' => '+', '!' => '!', '=' => '=' }
647
+ }
787
648
 
788
- # Given a set of patchset, convert the current version to the next
789
- # version. Does no auto-discovery.
790
- def patch!(src, patchset)
791
- Diff::LCS.patch(src, patchset, :patch)
649
+ # Applies a +patchset+ to the sequence +src+ according to the +direction+
650
+ # (<tt>:patch</tt> or <tt>:unpatch</tt>), producing a new sequence.
651
+ #
652
+ # If the +direction+ is not specified, Diff::LCS::patch will attempt to
653
+ # discover the direction of the +patchset+.
654
+ #
655
+ # A +patchset+ can be considered to apply forward (<tt>:patch</tt>) if the
656
+ # following expression is true:
657
+ #
658
+ # patch(s1, diff(s1, s2)) -> s2
659
+ #
660
+ # A +patchset+ can be considered to apply backward (<tt>:unpatch</tt>) if
661
+ # the following expression is true:
662
+ #
663
+ # patch(s2, diff(s1, s2)) -> s1
664
+ #
665
+ # If the +patchset+ contains no changes, the +src+ value will be returned
666
+ # as either <tt>src.dup</tt> or +src+. A +patchset+ can be deemed as
667
+ # having no changes if the following predicate returns true:
668
+ #
669
+ # patchset.empty? or
670
+ # patchset.flatten.all? { |change| change.unchanged? }
671
+ #
672
+ # === Patchsets
673
+ #
674
+ # A +patchset+ is always an enumerable sequence of changes, hunks of
675
+ # changes, or a mix of the two. A hunk of changes is an enumerable
676
+ # sequence of changes:
677
+ #
678
+ # [ # patchset
679
+ # # change
680
+ # [ # hunk
681
+ # # change
682
+ # ]
683
+ # ]
684
+ #
685
+ # The +patch+ method accepts <tt>patchset</tt>s that are enumerable
686
+ # sequences containing either Diff::LCS::Change objects (or a subclass) or
687
+ # the array representations of those objects. Prior to application, array
688
+ # representations of Diff::LCS::Change objects will be reified.
689
+ def patch(src, patchset, direction = nil)
690
+ # Normalize the patchset.
691
+ has_changes, patchset = Diff::LCS::Internals.analyze_patchset(patchset)
692
+
693
+ if not has_changes
694
+ return src.dup if src.respond_to? :dup
695
+ return src
792
696
  end
793
697
 
794
- # private
795
- # Compute the longest common subsequence between the sequenced
796
- # Enumerables +a+ and +b+. The result is an array whose contents is such
797
- # that
798
- #
799
- # result = Diff::LCS.__lcs(a, b)
800
- # result.each_with_index do |e, ii|
801
- # assert_equal(a[ii], b[e]) unless e.nil?
802
- # end
803
- #
804
- # Note: This will be deprecated as a public function in a future release.
805
- def __lcs(a, b)
806
- a_start = b_start = 0
807
- a_finish = a.size - 1
808
- b_finish = b.size - 1
809
- vector = []
698
+ string = src.kind_of?(String)
699
+ # Start with a new empty type of the source's class
700
+ res = src.class.new
810
701
 
811
- # Prune off any common elements at the beginning...
812
- while (a_start <= a_finish) and
813
- (b_start <= b_finish) and
814
- (a[a_start] == b[b_start])
815
- vector[a_start] = b_start
816
- a_start += 1
817
- b_start += 1
818
- end
702
+ direction ||= Diff::LCS::Internals.intuit_diff_direction(src, patchset)
819
703
 
820
- # Now the end...
821
- while (a_start <= a_finish) and
822
- (b_start <= b_finish) and
823
- (a[a_finish] == b[b_finish])
824
- vector[a_finish] = b_finish
825
- a_finish -= 1
826
- b_finish -= 1
827
- end
704
+ ai = bj = 0
828
705
 
829
- # Now, compute the equivalence classes of positions of elements.
830
- b_matches = Diff::LCS.__position_hash(b, b_start .. b_finish)
706
+ patch_map = PATCH_MAP[direction]
831
707
 
832
- thresh = []
833
- links = []
708
+ patchset.flatten.each do |change|
709
+ # Both Change and ContextChange support #action
710
+ action = patch_map[change.action]
834
711
 
835
- (a_start .. a_finish).each do |ii|
836
- ai = a.kind_of?(String) ? a[ii, 1] : a[ii]
837
- bm = b_matches[ai]
838
- kk = nil
839
- bm.reverse_each do |jj|
840
- if kk and (thresh[kk] > jj) and (thresh[kk - 1] < jj)
841
- thresh[kk] = jj
842
- else
843
- kk = Diff::LCS.__replace_next_larger(thresh, jj, kk)
844
- end
845
- links[kk] = [ (kk > 0) ? links[kk - 1] : nil, ii, jj ] unless kk.nil?
712
+ case change
713
+ when Diff::LCS::ContextChange
714
+ case direction
715
+ when :patch
716
+ el = change.new_element
717
+ op = change.old_position
718
+ np = change.new_position
719
+ when :unpatch
720
+ el = change.old_element
721
+ op = change.new_position
722
+ np = change.old_position
846
723
  end
847
- end
848
-
849
- unless thresh.empty?
850
- link = links[thresh.size - 1]
851
- while not link.nil?
852
- vector[link[1]] = link[2]
853
- link = link[0]
854
- end
855
- end
856
724
 
857
- vector
858
- end
725
+ case action
726
+ when '-' # Remove details from the old string
727
+ while ai < op
728
+ res << (string ? src[ai, 1] : src[ai])
729
+ ai += 1
730
+ bj += 1
731
+ end
732
+ ai += 1
733
+ when '+'
734
+ while bj < np
735
+ res << (string ? src[ai, 1] : src[ai])
736
+ ai += 1
737
+ bj += 1
738
+ end
859
739
 
860
- # Find the place at which +value+ would normally be inserted into the
861
- # Enumerable. If that place is already occupied by +value+, do nothing
862
- # and return +nil+. If the place does not exist (i.e., it is off the end
863
- # of the Enumerable), add it to the end. Otherwise, replace the element
864
- # at that point with +value+. It is assumed that the Enumerable's values
865
- # are numeric.
866
- #
867
- # This operation preserves the sort order.
868
- #
869
- # Note: This will be deprecated as a public function in a future release.
870
- def __replace_next_larger(enum, value, last_index = nil)
871
- # Off the end?
872
- if enum.empty? or (value > enum[-1])
873
- enum << value
874
- return enum.size - 1
875
- end
740
+ res << el
741
+ bj += 1
742
+ when '='
743
+ # This only appears in sdiff output with the SDiff callback.
744
+ # Therefore, we only need to worry about dealing with a single
745
+ # element.
746
+ res << el
876
747
 
877
- # Binary search for the insertion point
878
- last_index ||= enum.size
879
- first_index = 0
880
- while (first_index <= last_index)
881
- ii = (first_index + last_index) >> 1
748
+ ai += 1
749
+ bj += 1
750
+ when '!'
751
+ while ai < op
752
+ res << (string ? src[ai, 1] : src[ai])
753
+ ai += 1
754
+ bj += 1
755
+ end
882
756
 
883
- found = enum[ii]
757
+ bj += 1
758
+ ai += 1
884
759
 
885
- if value == found
886
- return nil
887
- elsif value > found
888
- first_index = ii + 1
889
- else
890
- last_index = ii - 1
760
+ res << el
891
761
  end
892
- end
762
+ when Diff::LCS::Change
763
+ case action
764
+ when '-'
765
+ while ai < change.position
766
+ res << (string ? src[ai, 1] : src[ai])
767
+ ai += 1
768
+ bj += 1
769
+ end
770
+ ai += 1
771
+ when '+'
772
+ while bj < change.position
773
+ res << (string ? src[ai, 1] : src[ai])
774
+ ai += 1
775
+ bj += 1
776
+ end
893
777
 
894
- # The insertion point is in first_index; overwrite the next larger
895
- # value.
896
- enum[first_index] = value
897
- return first_index
898
- end
778
+ bj += 1
899
779
 
900
- # If +vector+ maps the matching elements of another collection onto this
901
- # Enumerable, compute the inverse +vector+ that maps this Enumerable
902
- # onto the collection. (Currently unused.)
903
- #
904
- # Note: This will be deprecated as a public function in a future release.
905
- def __inverse_vector(a, vector)
906
- inverse = a.dup
907
- (0 ... vector.size).each do |ii|
908
- inverse[vector[ii]] = ii unless vector[ii].nil?
780
+ res << change.element
781
+ end
909
782
  end
910
- inverse
911
783
  end
912
784
 
913
- # Returns a hash mapping each element of an Enumerable to the set of
914
- # positions it occupies in the Enumerable, optionally restricted to the
915
- # elements specified in the range of indexes specified by +interval+.
916
- #
917
- # Note: This will be deprecated as a public function in a future release.
918
- def __position_hash(enum, interval = 0 .. -1)
919
- hash = Hash.new { |hh, kk| hh[kk] = [] }
920
- interval.each do |ii|
921
- kk = enum.kind_of?(String) ? enum[ii, 1] : enum[ii]
922
- hash[kk] << ii
923
- end
924
- hash
785
+ while ai < src.size
786
+ res << (string ? src[ai, 1] : src[ai])
787
+ ai += 1
788
+ bj += 1
925
789
  end
926
790
 
927
- # Examine the patchset and the source to see in which direction the
928
- # patch should be applied.
929
- #
930
- # WARNING: By default, this examines the whole patch, so this could take
931
- # some time. This also works better with Diff::LCS::ContextChange or
932
- # Diff::LCS::Change as its source, as an array will cause the creation
933
- # of one of the above.
934
- #
935
- # Note: This will be deprecated as a public function in a future release.
936
- def __diff_direction(src, patchset, limit = nil)
937
- count = left = left_miss = right = right_miss = 0
938
- string = src.kind_of?(String)
939
-
940
- patchset.each do |change|
941
- count += 1
942
-
943
- case change
944
- when Diff::LCS::Change
945
- # With a simplistic change, we can't tell the difference between
946
- # the left and right on '!' actions, so we ignore those. On '='
947
- # actions, if there's a miss, we miss both left and right.
948
- element = string ? src[change.position, 1] : src[change.position]
949
-
950
- case change.action
951
- when '-'
952
- if element == change.element
953
- left += 1
954
- else
955
- left_miss += 1
956
- end
957
- when '+'
958
- if element == change.element
959
- right += 1
960
- else
961
- right_miss += 1
962
- end
963
- when '='
964
- if element != change.element
965
- left_miss += 1
966
- right_miss += 1
967
- end
968
- end
969
- when Diff::LCS::ContextChange
970
- case change.action
971
- when '-' # Remove details from the old string
972
- element = string ? src[change.old_position, 1] : src[change.old_position]
973
- if element == change.old_element
974
- left += 1
975
- else
976
- left_miss += 1
977
- end
978
- when '+'
979
- element = string ? src[change.new_position, 1] : src[change.new_position]
980
- if element == change.new_element
981
- right += 1
982
- else
983
- right_miss += 1
984
- end
985
- when '='
986
- le = string ? src[change.old_position, 1] : src[change.old_position]
987
- re = string ? src[change.new_position, 1] : src[change.new_position]
988
-
989
- left_miss += 1 if le != change.old_element
990
- right_miss += 1 if re != change.new_element
991
- when '!'
992
- element = string ? src[change.old_position, 1] : src[change.old_position]
993
- if element == change.old_element
994
- left += 1
995
- else
996
- element = string ? src[change.new_position, 1] : src[change.new_position]
997
- if element == change.new_element
998
- right += 1
999
- else
1000
- left_miss += 1
1001
- right_miss += 1
1002
- end
1003
- end
1004
- end
1005
- end
1006
-
1007
- break if (not limit.nil?) && (count > limit)
1008
- end
1009
-
1010
- no_left = (left == 0) and (left_miss >= 0)
1011
- no_right = (right == 0) and (right_miss >= 0)
791
+ res
792
+ end
1012
793
 
1013
- case [no_left, no_right]
1014
- when [false, true]
1015
- return :patch
1016
- when [true, false]
1017
- return :unpatch
1018
- else
1019
- raise "The provided patchset does not appear to apply to the provided value as either source or destination value."
1020
- end
1021
- end
794
+ # Given a set of patchset, convert the current version to the prior
795
+ # version. Does no auto-discovery.
796
+ def unpatch!(src, patchset)
797
+ patch(src, patchset, :unpatch)
798
+ end
1022
799
 
1023
- # Normalize the patchset. A patchset is always a sequence of changes, but
1024
- # how those changes are represented may vary, depending on how they were
1025
- # generated. In all cases we support, we also support the array
1026
- # representation of the changes. The formats are:
1027
- #
1028
- # [ # patchset <- Diff::LCS.diff(a, b)
1029
- # [ # one or more hunks
1030
- # Diff::LCS::Change # one or more changes
1031
- # ] ]
1032
- #
1033
- # [ # patchset, equivalent to the above
1034
- # [ # one or more hunks
1035
- # [ action, line, value ] # one or more changes
1036
- # ] ]
1037
- #
1038
- # [ # patchset <- Diff::LCS.diff(a, b, Diff::LCS::ContextDiffCallbacks)
1039
- # # OR <- Diff::LCS.sdiff(a, b, Diff::LCS::ContextDiffCallbacks)
1040
- # [ # one or more hunks
1041
- # Diff::LCS::ContextChange # one or more changes
1042
- # ] ]
1043
- #
1044
- # [ # patchset, equivalent to the above
1045
- # [ # one or more hunks
1046
- # [ action, [ old line, old value ], [ new line, new value ] ]
1047
- # # one or more changes
1048
- # ] ]
1049
- #
1050
- # [ # patchset <- Diff::LCS.sdiff(a, b)
1051
- # # OR <- Diff::LCS.diff(a, b, Diff::LCS::SDiffCallbacks)
1052
- # Diff::LCS::ContextChange # one or more changes
1053
- # ]
1054
- #
1055
- # [ # patchset, equivalent to the above
1056
- # [ action, [ old line, old value ], [ new line, new value ] ]
1057
- # # one or more changes
1058
- # ]
1059
- #
1060
- # The result of this will be either of the following.
1061
- #
1062
- # [ # patchset
1063
- # Diff::LCS::ContextChange # one or more changes
1064
- # ]
1065
- #
1066
- # [ # patchset
1067
- # Diff::LCS::Change # one or more changes
1068
- # ]
1069
- #
1070
- # If either of the above is provided, it will be returned as such.
1071
- #
1072
- # Note: This will be deprecated as a public function in a future release.
1073
- def __normalize_patchset(patchset)
1074
- patchset.map do |hunk|
1075
- case hunk
1076
- when Diff::LCS::ContextChange, Diff::LCS::Change
1077
- hunk
1078
- when Array
1079
- if (not hunk[0].kind_of?(Array)) and hunk[1].kind_of?(Array) and hunk[2].kind_of?(Array)
1080
- Diff::LCS::ContextChange.from_a(hunk)
1081
- else
1082
- hunk.map do |change|
1083
- case change
1084
- when Diff::LCS::ContextChange, Diff::LCS::Change
1085
- change
1086
- when Array
1087
- # change[1] will ONLY be an array in a ContextChange#to_a call.
1088
- # In Change#to_a, it represents the line (singular).
1089
- if change[1].kind_of?(Array)
1090
- Diff::LCS::ContextChange.from_a(change)
1091
- else
1092
- Diff::LCS::Change.from_a(change)
1093
- end
1094
- end
1095
- end
1096
- end
1097
- else
1098
- raise ArgumentError, "Cannot normalise a hunk of class #{hunk.class}."
1099
- end
1100
- end.flatten
1101
- end
800
+ # Given a set of patchset, convert the current version to the next
801
+ # version. Does no auto-discovery.
802
+ def patch!(src, patchset)
803
+ patch(src, patchset, :patch)
1102
804
  end
1103
805
  end
1104
-
1105
- # vim: ft=ruby