slimkeyfy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +34 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +22 -0
  6. data/LICENSE +21 -0
  7. data/README.md +172 -0
  8. data/bin/slimkeyfy +5 -0
  9. data/lib/.DS_Store +0 -0
  10. data/lib/slimkeyfy/console/command_line.rb +90 -0
  11. data/lib/slimkeyfy/console/io_action.rb +30 -0
  12. data/lib/slimkeyfy/console/printer.rb +50 -0
  13. data/lib/slimkeyfy/console/translate.rb +101 -0
  14. data/lib/slimkeyfy/console.rb +8 -0
  15. data/lib/slimkeyfy/slimutils/file_utils.rb +61 -0
  16. data/lib/slimkeyfy/slimutils/hash_merging.rb +84 -0
  17. data/lib/slimkeyfy/slimutils/key_generator.rb +70 -0
  18. data/lib/slimkeyfy/slimutils/yaml_processor.rb +56 -0
  19. data/lib/slimkeyfy/slimutils.rb +8 -0
  20. data/lib/slimkeyfy/transformer/base_transformer.rb +42 -0
  21. data/lib/slimkeyfy/transformer/controller_transformer.rb +20 -0
  22. data/lib/slimkeyfy/transformer/slim_transformer.rb +92 -0
  23. data/lib/slimkeyfy/transformer/whitespacer.rb +48 -0
  24. data/lib/slimkeyfy/transformer/word.rb +49 -0
  25. data/lib/slimkeyfy/transformer.rb +9 -0
  26. data/lib/slimkeyfy/version.rb +3 -0
  27. data/lib/slimkeyfy.rb +12 -0
  28. data/slimkeyfy.gemspec +21 -0
  29. data/vendor/bundle/bin/htmldiff +25 -0
  30. data/vendor/bundle/bin/ldiff +25 -0
  31. data/vendor/bundle/bin/rspec +23 -0
  32. data/vendor/bundle/build_info/diff-lcs-1.2.5.info +1 -0
  33. data/vendor/bundle/build_info/rspec-3.0.0.info +1 -0
  34. data/vendor/bundle/build_info/rspec-core-3.0.2.info +1 -0
  35. data/vendor/bundle/build_info/rspec-expectations-3.0.2.info +1 -0
  36. data/vendor/bundle/build_info/rspec-mocks-3.0.2.info +1 -0
  37. data/vendor/bundle/build_info/rspec-support-3.0.2.info +1 -0
  38. data/vendor/bundle/gems/diff-lcs-1.2.5/.autotest +3 -0
  39. data/vendor/bundle/gems/diff-lcs-1.2.5/.gemtest +0 -0
  40. data/vendor/bundle/gems/diff-lcs-1.2.5/.hoerc +2 -0
  41. data/vendor/bundle/gems/diff-lcs-1.2.5/.rspec +2 -0
  42. data/vendor/bundle/gems/diff-lcs-1.2.5/.travis.yml +22 -0
  43. data/vendor/bundle/gems/diff-lcs-1.2.5/Contributing.rdoc +64 -0
  44. data/vendor/bundle/gems/diff-lcs-1.2.5/Gemfile +20 -0
  45. data/vendor/bundle/gems/diff-lcs-1.2.5/History.rdoc +152 -0
  46. data/vendor/bundle/gems/diff-lcs-1.2.5/License.rdoc +39 -0
  47. data/vendor/bundle/gems/diff-lcs-1.2.5/Manifest.txt +38 -0
  48. data/vendor/bundle/gems/diff-lcs-1.2.5/README.rdoc +85 -0
  49. data/vendor/bundle/gems/diff-lcs-1.2.5/Rakefile +41 -0
  50. data/vendor/bundle/gems/diff-lcs-1.2.5/autotest/discover.rb +1 -0
  51. data/vendor/bundle/gems/diff-lcs-1.2.5/bin/htmldiff +32 -0
  52. data/vendor/bundle/gems/diff-lcs-1.2.5/bin/ldiff +6 -0
  53. data/vendor/bundle/gems/diff-lcs-1.2.5/docs/COPYING.txt +339 -0
  54. data/vendor/bundle/gems/diff-lcs-1.2.5/docs/artistic.txt +127 -0
  55. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/array.rb +7 -0
  56. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/block.rb +37 -0
  57. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/callbacks.rb +322 -0
  58. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/change.rb +177 -0
  59. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/htmldiff.rb +149 -0
  60. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/hunk.rb +276 -0
  61. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/internals.rb +301 -0
  62. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/ldiff.rb +195 -0
  63. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs/string.rb +5 -0
  64. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff/lcs.rb +805 -0
  65. data/vendor/bundle/gems/diff-lcs-1.2.5/lib/diff-lcs.rb +3 -0
  66. data/vendor/bundle/gems/rspec-3.0.0/License.txt +24 -0
  67. data/vendor/bundle/gems/rspec-3.0.0/README.md +47 -0
  68. data/vendor/bundle/gems/rspec-3.0.0/lib/rspec.rb +3 -0
  69. data/vendor/bundle/gems/rspec-core-3.0.2/.document +5 -0
  70. data/vendor/bundle/gems/rspec-core-3.0.2/.yardopts +7 -0
  71. data/vendor/bundle/gems/rspec-core-3.0.2/Changelog.md +1466 -0
  72. data/vendor/bundle/gems/rspec-core-3.0.2/License.txt +25 -0
  73. data/vendor/bundle/gems/rspec-core-3.0.2/README.md +243 -0
  74. data/vendor/bundle/gems/rspec-core-3.0.2/exe/rspec +4 -0
  75. data/vendor/bundle/gems/rspec-expectations-3.0.2/.document +5 -0
  76. data/vendor/bundle/gems/rspec-expectations-3.0.2/.yardopts +6 -0
  77. data/vendor/bundle/gems/rspec-expectations-3.0.2/Changelog.md +749 -0
  78. data/vendor/bundle/gems/rspec-expectations-3.0.2/License.txt +24 -0
  79. data/vendor/bundle/gems/rspec-expectations-3.0.2/README.md +278 -0
  80. data/vendor/bundle/gems/rspec-mocks-3.0.2/.document +5 -0
  81. data/vendor/bundle/gems/rspec-mocks-3.0.2/.yardopts +6 -0
  82. data/vendor/bundle/gems/rspec-mocks-3.0.2/Changelog.md +733 -0
  83. data/vendor/bundle/gems/rspec-mocks-3.0.2/License.txt +24 -0
  84. data/vendor/bundle/gems/rspec-mocks-3.0.2/README.md +380 -0
  85. data/vendor/bundle/gems/rspec-support-3.0.2/Changelog.md +30 -0
  86. data/vendor/bundle/gems/rspec-support-3.0.2/LICENSE.txt +22 -0
  87. data/vendor/bundle/gems/rspec-support-3.0.2/README.md +17 -0
  88. data/vendor/bundle/specifications/diff-lcs-1.2.5.gemspec +68 -0
  89. data/vendor/bundle/specifications/rspec-3.0.0.gemspec +43 -0
  90. data/vendor/bundle/specifications/rspec-core-3.0.2.gemspec +66 -0
  91. data/vendor/bundle/specifications/rspec-expectations-3.0.2.gemspec +51 -0
  92. data/vendor/bundle/specifications/rspec-mocks-3.0.2.gemspec +48 -0
  93. data/vendor/bundle/specifications/rspec-support-3.0.2.gemspec +42 -0
  94. metadata +153 -0
@@ -0,0 +1,805 @@
1
+ # -*- ruby encoding: utf-8 -*-
2
+
3
+ module Diff; end unless defined? Diff
4
+ # = Diff::LCS 1.2.5
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.5'
133
+ end
134
+
135
+ require 'diff/lcs/callbacks'
136
+ require 'diff/lcs/internals'
137
+
138
+ module Diff::LCS
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)
143
+ def lcs(other, &block) #:yields self[i] if there are matched subsequences:
144
+ Diff::LCS.lcs(self, other, &block)
145
+ end
146
+
147
+ # Returns the difference set between +self+ and +other+. See
148
+ # Diff::LCS#diff.
149
+ def diff(other, callbacks = nil, &block)
150
+ Diff::LCS.diff(self, other, callbacks, &block)
151
+ end
152
+
153
+ # Returns the balanced ("side-by-side") difference set between +self+ and
154
+ # +other+. See Diff::LCS#sdiff.
155
+ def sdiff(other, callbacks = nil, &block)
156
+ Diff::LCS.sdiff(self, other, callbacks, &block)
157
+ end
158
+
159
+ # Traverses the discovered longest common subsequences between +self+ and
160
+ # +other+. See Diff::LCS#traverse_sequences.
161
+ def traverse_sequences(other, callbacks = nil, &block)
162
+ traverse_sequences(self, other, callbacks ||
163
+ Diff::LCS.YieldingCallbacks, &block)
164
+ end
165
+
166
+ # Traverses the discovered longest common subsequences between +self+ and
167
+ # +other+ using the alternate, balanced algorithm. See
168
+ # Diff::LCS#traverse_balanced.
169
+ def traverse_balanced(other, callbacks = nil, &block)
170
+ traverse_balanced(self, other, callbacks ||
171
+ Diff::LCS.YieldingCallbacks, &block)
172
+ end
173
+
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.
177
+ def patch(patchset)
178
+ Diff::LCS.patch(self, patchset)
179
+ end
180
+ alias_method :unpatch, :patch
181
+
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.
185
+ def patch!(patchset)
186
+ Diff::LCS.patch!(self, patchset)
187
+ end
188
+
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.
192
+ def unpatch!(patchset)
193
+ Diff::LCS.unpatch!(self, patchset)
194
+ end
195
+
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)
204
+ end
205
+ end
206
+
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
219
+
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
230
+ end
231
+ end
232
+ ret
233
+ end
234
+ alias_method :LCS, :lcs
235
+
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
249
+
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)
372
+ end
373
+ else
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
387
+ end
388
+ ai = i
389
+ 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
404
+ else
405
+ ax = string ? seq1[ai, 1] : seq1[ai]
406
+ loop do
407
+ bx = string ? seq2[bj, 1] : seq2[bj]
408
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
409
+ event = yield event if block_given?
410
+ callbacks.discard_b(event)
411
+ bj += 1
412
+ break unless bj < b_size
413
+ end
414
+ end
415
+ end
416
+
417
+ # last B?
418
+ if bj == b_size and ai < a_size
419
+ if callbacks.respond_to?(:finished_b) and not run_finished_b
420
+ ax = string ? seq1[ai, 1] : seq1[ai]
421
+ bx = string ? seq2[-1, 1] : seq2[-1]
422
+ event = Diff::LCS::ContextChange.new('<', ai, ax, (b_size - 1), bx)
423
+ event = yield event if block_given?
424
+ callbacks.finished_b(event)
425
+ run_finished_b = true
426
+ else
427
+ bx = string ? seq2[bj, 1] : seq2[bj]
428
+ loop do
429
+ ax = string ? seq1[ai, 1] : seq1[ai]
430
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
431
+ event = yield event if block_given?
432
+ callbacks.discard_a(event)
433
+ ai += 1
434
+ break unless bj < b_size
435
+ end
436
+ end
437
+ end
438
+
439
+ if ai < a_size
440
+ ax = string ? seq1[ai, 1] : seq1[ai]
441
+ bx = string ? seq2[bj, 1] : seq2[bj]
442
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
443
+ event = yield event if block_given?
444
+ callbacks.discard_a(event)
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)
454
+ bj += 1
455
+ end
456
+ end
457
+ end
458
+
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)
562
+ ax = string ? seq1[ai, 1] : seq1[ai]
563
+ bx = string ? seq2[bj, 1] : seq2[bj]
564
+
565
+ case [(ai < ma), (bj < mb)]
566
+ when [true, true]
567
+ if callbacks.respond_to?(:change)
568
+ event = Diff::LCS::ContextChange.new('!', ai, ax, bj, bx)
569
+ event = yield event if block_given?
570
+ callbacks.change(event)
571
+ ai += 1
572
+ bj += 1
573
+ else
574
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
575
+ event = yield event if block_given?
576
+ callbacks.discard_a(event)
577
+ ai += 1
578
+ ax = string ? seq1[ai, 1] : seq1[ai]
579
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
580
+ event = yield event if block_given?
581
+ callbacks.discard_b(event)
582
+ bj += 1
583
+ end
584
+ when [true, false]
585
+ event = Diff::LCS::ContextChange.new('-', ai, ax, bj, bx)
586
+ event = yield event if block_given?
587
+ callbacks.discard_a(event)
588
+ ai += 1
589
+ when [false, true]
590
+ event = Diff::LCS::ContextChange.new('+', ai, ax, bj, bx)
591
+ event = yield event if block_given?
592
+ callbacks.discard_b(event)
593
+ bj += 1
594
+ end
595
+ end
596
+
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
606
+
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]
610
+
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
629
+ end
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)
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)
639
+ bj += 1
640
+ end
641
+ end
642
+ end
643
+
644
+ PATCH_MAP = { #:nodoc:
645
+ :patch => { '+' => '+', '-' => '-', '!' => '!', '=' => '=' },
646
+ :unpatch => { '+' => '-', '-' => '+', '!' => '!', '=' => '=' }
647
+ }
648
+
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
696
+ end
697
+
698
+ string = src.kind_of?(String)
699
+ # Start with a new empty type of the source's class
700
+ res = src.class.new
701
+
702
+ direction ||= Diff::LCS::Internals.intuit_diff_direction(src, patchset)
703
+
704
+ ai = bj = 0
705
+
706
+ patch_map = PATCH_MAP[direction]
707
+
708
+ patchset.flatten.each do |change|
709
+ # Both Change and ContextChange support #action
710
+ action = patch_map[change.action]
711
+
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
723
+ end
724
+
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
739
+
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
747
+
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
756
+
757
+ bj += 1
758
+ ai += 1
759
+
760
+ res << el
761
+ 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
777
+
778
+ bj += 1
779
+
780
+ res << change.element
781
+ end
782
+ end
783
+ end
784
+
785
+ while ai < src.size
786
+ res << (string ? src[ai, 1] : src[ai])
787
+ ai += 1
788
+ bj += 1
789
+ end
790
+
791
+ res
792
+ end
793
+
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
799
+
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)
804
+ end
805
+ end