diff-lcs 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ # -*- ruby encoding: utf-8 -*-
2
+
3
+ require 'diff/lcs'
4
+
5
+ # vim: ft=ruby
@@ -1,211 +1,196 @@
1
- #! /usr/env/bin ruby
2
- #--
3
- # Copyright 2004 Austin Ziegler <diff-lcs@halostatue.ca>
4
- # adapted from:
5
- # Algorithm::Diff (Perl) by Ned Konz <perl@bike-nomad.com>
6
- # Smalltalk by Mario I. Wolczko <mario@wolczko.com>
7
- # implements McIlroy-Hunt diff algorithm
8
- #
9
- # This program is free software. It may be redistributed and/or modified
10
- # under the terms of the GPL version 2 (or later), the Perl Artistic
11
- # licence, or the Ruby licence.
12
- #
13
- # $Id: lcs.rb,v 1.9 2004/10/17 20:31:10 austin Exp $
14
- #++
1
+ # -*- ruby encoding: utf-8 -*-
15
2
 
16
3
  module Diff
17
- # = Diff::LCS 1.1.2
18
- # Computes "intelligent" differences between two sequenced Enumerables.
19
- # This is an implementation of the McIlroy-Hunt "diff" algorithm for
20
- # Enumerable objects that include Diffable.
21
- #
22
- # Based on Mario I. Wolczko's <mario@wolczko.com> Smalltalk version
23
- # (1.2, 1993) and Ned Konz's <perl@bike-nomad.com> Perl version
24
- # (Algorithm::Diff).
25
- #
26
- # == Synopsis
27
- # require 'diff/lcs'
28
- #
29
- # seq1 = %w(a b c e h j l m n p)
30
- # seq2 = %w(b c d e f j k l m r s t)
31
- #
32
- # lcs = Diff::LCS.LCS(seq1, seq2)
33
- # diffs = Diff::LCS.diff(seq1, seq2)
34
- # sdiff = Diff::LCS.sdiff(seq1, seq2)
35
- # seq = Diff::LCS.traverse_sequences(seq1, seq2, callback_obj)
36
- # bal = Diff::LCS.traverse_balanced(seq1, seq2, callback_obj)
37
- # seq2 == Diff::LCS.patch(seq1, diffs)
38
- # seq2 == Diff::LCS.patch!(seq1, diffs)
39
- # seq1 == Diff::LCS.unpatch(seq2, diffs)
40
- # seq1 == Diff::LCS.unpatch!(seq2, diffs)
41
- # seq2 == Diff::LCS.patch(seq1, sdiff)
42
- # seq2 == Diff::LCS.patch!(seq1, sdiff)
43
- # seq1 == Diff::LCS.unpatch(seq2, sdiff)
44
- # seq1 == Diff::LCS.unpatch!(seq2, sdiff)
45
- #
46
- # Alternatively, objects can be extended with Diff::LCS:
47
- #
48
- # seq1.extend(Diff::LCS)
49
- # lcs = seq1.lcs(seq2)
50
- # diffs = seq1.diff(seq2)
51
- # sdiff = seq1.sdiff(seq2)
52
- # seq = seq1.traverse_sequences(seq2, callback_obj)
53
- # bal = seq1.traverse_balanced(seq2, callback_obj)
54
- # seq2 == seq1.patch(diffs)
55
- # seq2 == seq1.patch!(diffs)
56
- # seq1 == seq2.unpatch(diffs)
57
- # seq1 == seq2.unpatch!(diffs)
58
- # seq2 == seq1.patch(sdiff)
59
- # seq2 == seq1.patch!(sdiff)
60
- # seq1 == seq2.unpatch(sdiff)
61
- # seq1 == seq2.unpatch!(sdiff)
62
- #
63
- # Default extensions are provided for Array and String objects through
64
- # the use of 'diff/lcs/array' and 'diff/lcs/string'.
65
- #
66
- # == Introduction (by Mark-Jason Dominus)
67
- #
68
- # <em>The following text is from the Perl documentation. The only
69
- # changes have been to make the text appear better in Rdoc</em>.
70
- #
71
- # I once read an article written by the authors of +diff+; they said
72
- # that they hard worked very hard on the algorithm until they found the
73
- # right one.
74
- #
75
- # I think what they ended up using (and I hope someone will correct me,
76
- # because I am not very confident about this) was the `longest common
77
- # subsequence' method. In the LCS problem, you have two sequences of
78
- # items:
79
- #
80
- # a b c d f g h j q z
81
- # a b c d e f g i j k r x y z
82
- #
83
- # and you want to find the longest sequence of items that is present in
84
- # both original sequences in the same order. That is, you want to find a
85
- # new sequence *S* which can be obtained from the first sequence by
86
- # deleting some items, and from the second sequence by deleting other
87
- # items. You also want *S* to be as long as possible. In this case *S*
88
- # is:
89
- #
90
- # a b c d f g j z
91
- #
92
- # From there it's only a small step to get diff-like output:
93
- #
94
- # e h i k q r x y
95
- # + - + + - + + +
96
- #
97
- # This module solves the LCS problem. It also includes a canned function
98
- # to generate +diff+-like output.
99
- #
100
- # It might seem from the example above that the LCS of two sequences is
101
- # always pretty obvious, but that's not always the case, especially when
102
- # the two sequences have many repeated elements. For example, consider
103
- #
104
- # a x b y c z p d q
105
- # a b c a x b y c z
106
- #
107
- # A naive approach might start by matching up the +a+ and +b+ that
108
- # appear at the beginning of each sequence, like this:
109
- #
110
- # a x b y c z p d q
111
- # a b c a b y c z
112
- #
113
- # This finds the common subsequence +a b c z+. But actually, the LCS is
114
- # +a x b y c z+:
115
- #
116
- # a x b y c z p d q
117
- # a b c a x b y c z
118
- #
119
- # == Author
120
- # This version is by Austin Ziegler <diff-lcs@halostatue.ca>.
121
- #
122
- # It is based on the Perl Algorithm::Diff by Ned Konz
123
- # <perl@bike-nomad.com>, copyright &copy; 2000 - 2002 and the Smalltalk
124
- # diff version by Mario I. Wolczko <mario@wolczko.com>, copyright &copy;
125
- # 1993. Documentation includes work by Mark-Jason Dominus.
126
- #
127
- # == Licence
128
- # Copyright &copy; 2004 Austin Ziegler
129
- # This program is free software; you can redistribute it and/or modify it
130
- # under the same terms as Ruby, or alternatively under the Perl Artistic
131
- # licence.
132
- #
133
- # == Credits
134
- # Much of the documentation is taken directly from the Perl
135
- # Algorithm::Diff implementation and was written originally by Mark-Jason
136
- # Dominus <mjd-perl-diff@plover.com> and later by Ned Konz. The basic Ruby
137
- # implementation was re-ported from the Smalltalk implementation, available
138
- # at ftp://st.cs.uiuc.edu/pub/Smalltalk/MANCHESTER/manchester/4.0/diff.st
139
- #
140
- # #sdiff and #traverse_balanced were written for the Perl version by Mike
141
- # Schilli <m@perlmeister.com>.
142
- #
143
- # "The algorithm is described in <em>A Fast Algorithm for Computing Longest
144
- # Common Subsequences</em>, CACM, vol.20, no.5, pp.350-353, May 1977, with
145
- # a few minor improvements to improve the speed."
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."
146
131
  module LCS
147
- VERSION = '1.1.2'
132
+ VERSION = '1.1.3'
148
133
  end
149
134
  end
150
135
 
151
136
  require 'diff/lcs/callbacks'
152
137
 
153
138
  module Diff::LCS
154
- # Returns an Array containing the longest common subsequence(s) between
155
- # +self+ and +other+. See Diff::LCS#LCS.
156
- #
157
- # lcs = seq1.lcs(seq2)
139
+ # Returns an Array containing the longest common subsequence(s) between
140
+ # +self+ and +other+. See Diff::LCS#LCS.
141
+ #
142
+ # lcs = seq1.lcs(seq2)
158
143
  def lcs(other, &block) #:yields self[ii] if there are matched subsequences:
159
144
  Diff::LCS.LCS(self, other, &block)
160
145
  end
161
146
 
162
- # Returns the difference set between +self+ and +other+. See
163
- # Diff::LCS#diff.
147
+ # Returns the difference set between +self+ and +other+. See
148
+ # Diff::LCS#diff.
164
149
  def diff(other, callbacks = nil, &block)
165
150
  Diff::LCS::diff(self, other, callbacks, &block)
166
151
  end
167
152
 
168
- # Returns the balanced ("side-by-side") difference set between +self+ and
169
- # +other+. See Diff::LCS#sdiff.
153
+ # Returns the balanced ("side-by-side") difference set between +self+ and
154
+ # +other+. See Diff::LCS#sdiff.
170
155
  def sdiff(other, callbacks = nil, &block)
171
156
  Diff::LCS::sdiff(self, other, callbacks, &block)
172
157
  end
173
158
 
174
- # Traverses the discovered longest common subsequences between +self+ and
175
- # +other+. See Diff::LCS#traverse_sequences.
159
+ # Traverses the discovered longest common subsequences between +self+ and
160
+ # +other+. See Diff::LCS#traverse_sequences.
176
161
  def traverse_sequences(other, callbacks = nil, &block)
177
- traverse_sequences(self, other, callbacks || Diff::LCS::YieldingCallbacks,
178
- &block)
162
+ traverse_sequences(self, other, callbacks ||
163
+ Diff::LCS::YieldingCallbacks, &block)
179
164
  end
180
165
 
181
- # Traverses the discovered longest common subsequences between +self+ and
182
- # +other+ using the alternate, balanced algorithm. See
183
- # Diff::LCS#traverse_balanced.
166
+ # Traverses the discovered longest common subsequences between +self+ and
167
+ # +other+ using the alternate, balanced algorithm. See
168
+ # Diff::LCS#traverse_balanced.
184
169
  def traverse_balanced(other, callbacks = nil, &block)
185
- traverse_balanced(self, other, callbacks || Diff::LCS::YieldingCallbacks,
186
- &block)
170
+ traverse_balanced(self, other, callbacks ||
171
+ Diff::LCS::YieldingCallbacks, &block)
187
172
  end
188
173
 
189
- # Attempts to patch a copy of +self+ with the provided +patchset+. See
190
- # Diff::LCS#patch.
174
+ # Attempts to patch a copy of +self+ with the provided +patchset+. See
175
+ # Diff::LCS#patch.
191
176
  def patch(patchset)
192
177
  Diff::LCS::patch(self.dup, patchset)
193
178
  end
194
179
 
195
- # Attempts to unpatch a copy of +self+ with the provided +patchset+.
196
- # See Diff::LCS#patch.
180
+ # Attempts to unpatch a copy of +self+ with the provided +patchset+. See
181
+ # Diff::LCS#patch.
197
182
  def unpatch(patchset)
198
183
  Diff::LCS::unpatch(self.dup, patchset)
199
184
  end
200
185
 
201
- # Attempts to patch +self+ with the provided +patchset+. See
202
- # Diff::LCS#patch!. Does no autodiscovery.
186
+ # Attempts to patch +self+ with the provided +patchset+. See
187
+ # Diff::LCS#patch!. Does no autodiscovery.
203
188
  def patch!(patchset)
204
189
  Diff::LCS::patch!(self, patchset)
205
190
  end
206
191
 
207
- # Attempts to unpatch +self+ with the provided +patchset+. See
208
- # Diff::LCS#unpatch. Does no autodiscovery.
192
+ # Attempts to unpatch +self+ with the provided +patchset+. See
193
+ # Diff::LCS#unpatch. Does no autodiscovery.
209
194
  def unpatch!(patchset)
210
195
  Diff::LCS::unpatch!(self, patchset)
211
196
  end
@@ -213,20 +198,20 @@ end
213
198
 
214
199
  module Diff::LCS
215
200
  class << self
216
- # Given two sequenced Enumerables, LCS returns an Array containing their
217
- # longest common subsequences.
218
- #
219
- # lcs = Diff::LCS.LCS(seq1, seq2)
220
- #
221
- # This array whose contents is such that:
222
- #
223
- # lcs.each_with_index do |ee, ii|
224
- # assert(ee.nil? || (seq1[ii] == seq2[ee]))
225
- # end
226
- #
227
- # If a block is provided, the matching subsequences will be yielded from
228
- # +seq1+ in turn and may be modified before they are placed into the
229
- # returned Array of subsequences.
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.
230
215
  def LCS(seq1, seq2, &block) #:yields seq1[ii] for each matched:
231
216
  matches = Diff::LCS.__lcs(seq1, seq2)
232
217
  ret = []
@@ -242,15 +227,15 @@ module Diff::LCS
242
227
  ret
243
228
  end
244
229
 
245
- # Diff::LCS.diff computes the smallest set of additions and deletions
246
- # necessary to turn the first sequence into the second, and returns a
247
- # description of these changes.
248
- #
249
- # See Diff::LCS::DiffCallbacks for the default behaviour. An alternate
250
- # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks.
251
- # If a Class argument is provided for +callbacks+, #diff will attempt
252
- # to initialise it. If the +callbacks+ object (possibly initialised)
253
- # responds to #finish, it will be called.
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.
254
239
  def diff(seq1, seq2, callbacks = nil, &block) # :yields diff changes:
255
240
  callbacks ||= Diff::LCS::DiffCallbacks
256
241
  if callbacks.kind_of?(Class)
@@ -263,7 +248,7 @@ module Diff::LCS
263
248
  if block_given?
264
249
  res = callbacks.diffs.map do |hunk|
265
250
  if hunk.kind_of?(Array)
266
- hunk = hunk.map { |block| yield block }
251
+ hunk = hunk.map { |hunk_block| yield hunk_block }
267
252
  else
268
253
  yield hunk
269
254
  end
@@ -274,20 +259,20 @@ module Diff::LCS
274
259
  end
275
260
  end
276
261
 
277
- # Diff::LCS.sdiff computes all necessary components to show two sequences
278
- # and their minimized differences side by side, just like the Unix
279
- # utility <em>sdiff</em> does:
280
- #
281
- # old < -
282
- # same same
283
- # before | after
284
- # - > new
285
- #
286
- # See Diff::LCS::SDiffCallbacks for the default behaviour. An alternate
287
- # behaviour may be implemented with Diff::LCS::ContextDiffCallbacks. If
288
- # a Class argument is provided for +callbacks+, #diff will attempt to
289
- # initialise it. If the +callbacks+ object (possibly initialised)
290
- # responds to #finish, it will be called.
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.
291
276
  def sdiff(seq1, seq2, callbacks = nil, &block) #:yields diff changes:
292
277
  callbacks ||= Diff::LCS::SDiffCallbacks
293
278
  if callbacks.kind_of?(Class)
@@ -300,7 +285,7 @@ module Diff::LCS
300
285
  if block_given?
301
286
  res = callbacks.diffs.map do |hunk|
302
287
  if hunk.kind_of?(Array)
303
- hunk = hunk.map { |block| yield block }
288
+ hunk = hunk.map { |hunk_block| yield hunk_block }
304
289
  else
305
290
  yield hunk
306
291
  end
@@ -311,87 +296,88 @@ module Diff::LCS
311
296
  end
312
297
  end
313
298
 
314
- # Diff::LCS.traverse_sequences is the most general facility provided by this
315
- # module; +diff+ and +LCS+ are implemented as calls to it.
316
- #
317
- # The arguments to #traverse_sequences are the two sequences to
318
- # traverse, and a callback object, like this:
319
- #
320
- # traverse_sequences(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
321
- #
322
- # #diff is implemented with #traverse_sequences.
323
- #
324
- # == Callback Methods
325
- # Optional callback methods are <em>emphasized</em>.
326
- #
327
- # callbacks#match:: Called when +a+ and +b+ are pointing
328
- # to common elements in +A+ and +B+.
329
- # callbacks#discard_a:: Called when +a+ is pointing to an
330
- # element not in +B+.
331
- # callbacks#discard_b:: Called when +b+ is pointing to an
332
- # element not in +A+.
333
- # <em>callbacks#finished_a</em>:: Called when +a+ has reached the end of
334
- # sequence +A+.
335
- # <em>callbacks#finished_b</em>:: Called when +b+ has reached the end of
336
- # sequence +B+.
337
- #
338
- # == Algorithm
339
- # a---+
340
- # v
341
- # A = a b c e h j l m n p
342
- # B = b c d e f j k l m r s t
343
- # ^
344
- # b---+
345
- #
346
- # If there are two arrows (+a+ and +b+) pointing to elements of
347
- # sequences +A+ and +B+, the arrows will initially point to the first
348
- # elements of their respective sequences. #traverse_sequences will
349
- # advance the arrows through the sequences one element at a time,
350
- # calling a method on the user-specified callback object before each
351
- # advance. It will advance the arrows in such a way that if there are
352
- # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
353
- # part of the longest common subsequence, there will be some moment
354
- # during the execution of #traverse_sequences when arrow +a+ is pointing
355
- # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
356
- # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
357
- # and then it will advance both arrows.
358
- #
359
- # Otherwise, one of the arrows is pointing to an element of its sequence
360
- # that is not part of the longest common subsequence.
361
- # #traverse_sequences will advance that arrow and will call
362
- # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>, depending
363
- # on which arrow it advanced. If both arrows point to elements that are
364
- # not part of the longest common subsequence, then #traverse_sequences
365
- # will advance one of them and call the appropriate callback, but it is
366
- # not specified which it will call.
367
- #
368
- # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
369
- # and <tt>callbacks#discard_b</tt> are invoked with an event comprising
370
- # the action ("=", "+", or "-", respectively), the indicies +ii+ and
371
- # +jj+, and the elements <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return
372
- # values are discarded by #traverse_sequences.
373
- #
374
- # === End of Sequences
375
- # If arrow +a+ reaches the end of its sequence before arrow +b+ does,
376
- # #traverse_sequence try to call <tt>callbacks#finished_a</tt> with the
377
- # last index and element of +A+ (<tt>A[-1]</tt>) and the current index
378
- # and element of +B+ (<tt>B[jj]</tt>). If <tt>callbacks#finished_a</tt>
379
- # does not exist, then <tt>callbacks#discard_b</tt> will be called on
380
- # each element of +B+ until the end of the sequence is reached (the call
381
- # will be done with <tt>A[-1]</tt> and <tt>B[jj]</tt> for each element).
382
- #
383
- # If +b+ reaches the end of +B+ before +a+ reaches the end of +A+,
384
- # <tt>callbacks#finished_b</tt> will be called with the current index
385
- # and element of +A+ (<tt>A[ii]</tt>) and the last index and element of
386
- # +B+ (<tt>A[-1]</tt>). Again, if <tt>callbacks#finished_b</tt> does not
387
- # exist on the callback object, then <tt>callbacks#discard_a</tt> will
388
- # be called on each element of +A+ until the end of the sequence is
389
- # reached (<tt>A[ii]</tt> and <tt>B[-1]</tt>).
390
- #
391
- # There is a chance that one additional <tt>callbacks#discard_a</tt> or
392
- # <tt>callbacks#discard_b</tt> will be called after the end of the
393
- # sequence is reached, if +a+ has not yet reached the end of +A+ or +b+
394
- # has not yet reached the end of +B+.
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+.
395
381
  def traverse_sequences(seq1, seq2, callbacks = Diff::LCS::SequenceCallbacks, &block) #:yields change events:
396
382
  matches = Diff::LCS.__lcs(seq1, seq2)
397
383
 
@@ -433,10 +419,10 @@ module Diff::LCS
433
419
  end
434
420
  ai += 1
435
421
 
436
- # The last entry (if any) processed was a match. +ai+ and +bj+ point
437
- # just past the last matching lines in their sequences.
422
+ # The last entry (if any) processed was a match. +ai+ and +bj+ point
423
+ # just past the last matching lines in their sequences.
438
424
  while (ai < a_size) or (bj < b_size)
439
- # last A?
425
+ # last A?
440
426
  if ai == a_size and bj < b_size
441
427
  if callbacks.respond_to?(:finished_a) and not run_finished_a
442
428
  ax = string ? seq1[-1, 1] : seq1[-1]
@@ -458,7 +444,7 @@ module Diff::LCS
458
444
  end
459
445
  end
460
446
 
461
- # last B?
447
+ # last B?
462
448
  if bj == b_size and ai < a_size
463
449
  if callbacks.respond_to?(:finished_b) and not run_finished_b
464
450
  ax = string ? seq1[ai, 1] : seq1[ai]
@@ -500,88 +486,88 @@ module Diff::LCS
500
486
  end
501
487
  end
502
488
 
503
- # #traverse_balanced is an alternative to #traverse_sequences. It
504
- # uses a different algorithm to iterate through the entries in the
505
- # computed longest common subsequence. Instead of viewing the changes as
506
- # insertions or deletions from one of the sequences, #traverse_balanced
507
- # will report <em>changes</em> between the sequences. To represent a
508
- #
509
- # The arguments to #traverse_balanced are the two sequences to traverse
510
- # and a callback object, like this:
511
- #
512
- # traverse_balanced(seq1, seq2, Diff::LCS::ContextDiffCallbacks.new)
513
- #
514
- # #sdiff is implemented with #traverse_balanced.
515
- #
516
- # == Callback Methods
517
- # Optional callback methods are <em>emphasized</em>.
518
- #
519
- # callbacks#match:: Called when +a+ and +b+ are pointing
520
- # to common elements in +A+ and +B+.
521
- # callbacks#discard_a:: Called when +a+ is pointing to an
522
- # element not in +B+.
523
- # callbacks#discard_b:: Called when +b+ is pointing to an
524
- # element not in +A+.
525
- # <em>callbacks#change</em>:: Called when +a+ and +b+ are pointing
526
- # to the same relative position, but
527
- # <tt>A[a]</tt> and <tt>B[b]</tt> are
528
- # not the same; a <em>change</em> has
529
- # occurred.
530
- #
531
- # #traverse_balanced might be a bit slower than #traverse_sequences,
532
- # noticable only while processing huge amounts of data.
533
- #
534
- # The +sdiff+ function of this module is implemented as call to
535
- # #traverse_balanced.
536
- #
537
- # == Algorithm
538
- # a---+
539
- # v
540
- # A = a b c e h j l m n p
541
- # B = b c d e f j k l m r s t
542
- # ^
543
- # b---+
544
- #
545
- # === Matches
546
- # If there are two arrows (+a+ and +b+) pointing to elements of
547
- # sequences +A+ and +B+, the arrows will initially point to the first
548
- # elements of their respective sequences. #traverse_sequences will
549
- # advance the arrows through the sequences one element at a time,
550
- # calling a method on the user-specified callback object before each
551
- # advance. It will advance the arrows in such a way that if there are
552
- # elements <tt>A[ii]</tt> and <tt>B[jj]</tt> which are both equal and
553
- # part of the longest common subsequence, there will be some moment
554
- # during the execution of #traverse_sequences when arrow +a+ is pointing
555
- # to <tt>A[ii]</tt> and arrow +b+ is pointing to <tt>B[jj]</tt>. When
556
- # this happens, #traverse_sequences will call <tt>callbacks#match</tt>
557
- # and then it will advance both arrows.
558
- #
559
- # === Discards
560
- # Otherwise, one of the arrows is pointing to an element of its sequence
561
- # that is not part of the longest common subsequence.
562
- # #traverse_sequences will advance that arrow and will call
563
- # <tt>callbacks#discard_a</tt> or <tt>callbacks#discard_b</tt>,
564
- # depending on which arrow it advanced.
565
- #
566
- # === Changes
567
- # If both +a+ and +b+ point to elements that are not part of the longest
568
- # common subsequence, then #traverse_sequences will try to call
569
- # <tt>callbacks#change</tt> and advance both arrows. If
570
- # <tt>callbacks#change</tt> is not implemented, then
571
- # <tt>callbacks#discard_a</tt> and <tt>callbacks#discard_b</tt> will be
572
- # called in turn.
573
- #
574
- # The methods for <tt>callbacks#match</tt>, <tt>callbacks#discard_a</tt>,
575
- # <tt>callbacks#discard_b</tt>, and <tt>callbacks#change</tt> are
576
- # invoked with an event comprising the action ("=", "+", "-", or "!",
577
- # respectively), the indicies +ii+ and +jj+, and the elements
578
- # <tt>A[ii]</tt> and <tt>B[jj]</tt>. Return values are discarded by
579
- # #traverse_balanced.
580
- #
581
- # === Context
582
- # Note that +ii+ and +jj+ may not be the same index position, even if
583
- # +a+ and +b+ are considered to be pointing to matching or changed
584
- # elements.
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.
585
571
  def traverse_balanced(seq1, seq2, callbacks = Diff::LCS::BalancedCallbacks)
586
572
  matches = Diff::LCS.__lcs(seq1, seq2)
587
573
  a_size = seq1.size
@@ -690,10 +676,10 @@ module Diff::LCS
690
676
  :unpatch => { '+' => '-', '-' => '+', '!' => '!', '=' => '=' }
691
677
  }
692
678
 
693
- # Given a patchset, convert the current version to the new
694
- # version. If +direction+ is not specified (must be
695
- # <tt>:patch</tt> or <tt>:unpatch</tt>), then discovery of the
696
- # direction of the patch will be attempted.
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.
697
683
  def patch(src, patchset, direction = nil)
698
684
  string = src.kind_of?(String)
699
685
  # Start with a new empty type of the source's class
@@ -793,51 +779,54 @@ module Diff::LCS
793
779
  res
794
780
  end
795
781
 
796
- # Given a set of patchset, convert the current version to the prior
797
- # version. Does no auto-discovery.
782
+ # Given a set of patchset, convert the current version to the prior
783
+ # version. Does no auto-discovery.
798
784
  def unpatch!(src, patchset)
799
785
  Diff::LCS.patch(src, patchset, :unpatch)
800
786
  end
801
787
 
802
- # Given a set of patchset, convert the current version to the next
803
- # version. Does no auto-discovery.
788
+ # Given a set of patchset, convert the current version to the next
789
+ # version. Does no auto-discovery.
804
790
  def patch!(src, patchset)
805
791
  Diff::LCS.patch(src, patchset, :patch)
806
792
  end
807
793
 
808
794
  # private
809
- # Compute the longest common subsequence between the sequenced Enumerables
810
- # +a+ and +b+. The result is an array whose contents is such that
811
- #
812
- # result = Diff::LCS.__lcs(a, b)
813
- # result.each_with_index do |e, ii|
814
- # assert_equal(a[ii], b[e]) unless e.nil?
815
- # end
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.
816
805
  def __lcs(a, b)
817
806
  a_start = b_start = 0
818
807
  a_finish = a.size - 1
819
808
  b_finish = b.size - 1
820
809
  vector = []
821
810
 
822
- # Prune off any common elements at the beginning...
811
+ # Prune off any common elements at the beginning...
823
812
  while (a_start <= a_finish) and
824
- (b_start <= b_finish) and
825
- (a[a_start] == b[b_start])
813
+ (b_start <= b_finish) and
814
+ (a[a_start] == b[b_start])
826
815
  vector[a_start] = b_start
827
816
  a_start += 1
828
817
  b_start += 1
829
818
  end
830
819
 
831
- # Now the end...
820
+ # Now the end...
832
821
  while (a_start <= a_finish) and
833
- (b_start <= b_finish) and
834
- (a[a_finish] == b[b_finish])
822
+ (b_start <= b_finish) and
823
+ (a[a_finish] == b[b_finish])
835
824
  vector[a_finish] = b_finish
836
825
  a_finish -= 1
837
826
  b_finish -= 1
838
827
  end
839
828
 
840
- # Now, compute the equivalence classes of positions of elements.
829
+ # Now, compute the equivalence classes of positions of elements.
841
830
  b_matches = Diff::LCS.__position_hash(b, b_start .. b_finish)
842
831
 
843
832
  thresh = []
@@ -868,14 +857,16 @@ module Diff::LCS
868
857
  vector
869
858
  end
870
859
 
871
- # Find the place at which +value+ would normally be inserted into the
872
- # Enumerable. If that place is already occupied by +value+, do nothing
873
- # and return +nil+. If the place does not exist (i.e., it is off the end
874
- # of the Enumerable), add it to the end. Otherwise, replace the element
875
- # at that point with +value+. It is assumed that the Enumerable's values
876
- # are numeric.
877
- #
878
- # This operation preserves the sort order.
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.
879
870
  def __replace_next_larger(enum, value, last_index = nil)
880
871
  # Off the end?
881
872
  if enum.empty? or (value > enum[-1])
@@ -906,9 +897,11 @@ module Diff::LCS
906
897
  return first_index
907
898
  end
908
899
 
909
- # If +vector+ maps the matching elements of another collection onto this
910
- # Enumerable, compute the inverse +vector+ that maps this Enumerable
911
- # onto the collection. (Currently unused.)
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.
912
905
  def __inverse_vector(a, vector)
913
906
  inverse = a.dup
914
907
  (0 ... vector.size).each do |ii|
@@ -917,9 +910,11 @@ module Diff::LCS
917
910
  inverse
918
911
  end
919
912
 
920
- # Returns a hash mapping each element of an Enumerable to the set of
921
- # positions it occupies in the Enumerable, optionally restricted to the
922
- # elements specified in the range of indexes specified by +interval+.
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.
923
918
  def __position_hash(enum, interval = 0 .. -1)
924
919
  hash = Hash.new { |hh, kk| hh[kk] = [] }
925
920
  interval.each do |ii|
@@ -929,13 +924,15 @@ module Diff::LCS
929
924
  hash
930
925
  end
931
926
 
932
- # Examine the patchset and the source to see in which direction the
933
- # patch should be applied.
934
- #
935
- # WARNING: By default, this examines the whole patch, so this could take
936
- # some time. This also works better with Diff::LCS::ContextChange or
937
- # Diff::LCS::Change as its source, as an array will cause the creation
938
- # of one of the above.
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.
939
936
  def __diff_direction(src, patchset, limit = nil)
940
937
  count = left = left_miss = right = right_miss = 0
941
938
  string = src.kind_of?(String)
@@ -945,9 +942,9 @@ module Diff::LCS
945
942
 
946
943
  case change
947
944
  when Diff::LCS::Change
948
- # With a simplistic change, we can't tell the difference between
949
- # the left and right on '!' actions, so we ignore those. On '='
950
- # actions, if there's a miss, we miss both left and right.
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.
951
948
  element = string ? src[change.position, 1] : src[change.position]
952
949
 
953
950
  case change.action
@@ -1007,7 +1004,7 @@ module Diff::LCS
1007
1004
  end
1008
1005
  end
1009
1006
 
1010
- break if not limit.nil? and count > limit
1007
+ break if (not limit.nil?) && (count > limit)
1011
1008
  end
1012
1009
 
1013
1010
  no_left = (left == 0) and (left_miss >= 0)
@@ -1023,55 +1020,56 @@ module Diff::LCS
1023
1020
  end
1024
1021
  end
1025
1022
 
1026
- # Normalize the patchset. A patchset is always a sequence of changes, but
1027
- # how those changes are represented may vary, depending on how they were
1028
- # generated. In all cases we support, we also support the array
1029
- # representation of the changes. The formats are:
1030
- #
1031
- # [ # patchset <- Diff::LCS.diff(a, b)
1032
- # [ # one or more hunks
1033
- # Diff::LCS::Change # one or more changes
1034
- # ] ]
1035
- #
1036
- # [ # patchset, equivalent to the above
1037
- # [ # one or more hunks
1038
- # [ action, line, value ] # one or more changes
1039
- # ] ]
1040
- #
1041
- # [ # patchset <- Diff::LCS.diff(a, b, Diff::LCS::ContextDiffCallbacks)
1042
- # # OR <- Diff::LCS.sdiff(a, b, Diff::LCS::ContextDiffCallbacks)
1043
- # [ # one or more hunks
1044
- # Diff::LCS::ContextChange # one or more changes
1045
- # ] ]
1046
- #
1047
- # [ # patchset, equivalent to the above
1048
- # [ # one or more hunks
1049
- # [ action, [ old line, old value ], [ new line, new value ] ]
1050
- # # one or more changes
1051
- # ] ]
1052
- #
1053
- # [ # patchset <- Diff::LCS.sdiff(a, b)
1054
- # # OR <- Diff::LCS.diff(a, b, Diff::LCS::SDiffCallbacks)
1055
- # Diff::LCS::ContextChange # one or more changes
1056
- # ]
1057
- #
1058
- # [ # patchset, equivalent to the above
1059
- # [ action, [ old line, old value ], [ new line, new value ] ]
1060
- # # one or more changes
1061
- # ]
1062
- #
1063
- # The result of this will be either of the following.
1064
- #
1065
- # [ # patchset
1066
- # Diff::LCS::ContextChange # one or more changes
1067
- # ]
1068
- #
1069
- # [ # patchset
1070
- # Diff::LCS::Change # one or more changes
1071
- # ]
1072
- #
1073
- # If either of the above is provided, it will be returned as such.
1074
- #
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.
1075
1073
  def __normalize_patchset(patchset)
1076
1074
  patchset.map do |hunk|
1077
1075
  case hunk
@@ -1103,3 +1101,5 @@ module Diff::LCS
1103
1101
  end
1104
1102
  end
1105
1103
  end
1104
+
1105
+ # vim: ft=ruby