/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/patiencediff.py

  • Committer: Aaron Bentley
  • Date: 2006-05-23 14:02:57 UTC
  • mto: This revision was merged to the branch mainline in revision 1727.
  • Revision ID: abentley@panoramicfeedback.com-20060523140257-352b84f597fb5a3a
Fix style issues and duplicated tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bisect import bisect
20
20
from copy import copy
21
21
import difflib
22
 
import time
23
22
import os
24
23
import sys
 
24
import time
 
25
 
25
26
 
26
27
__all__ = ['SequenceMatcher', 'unified_diff', 'unified_diff_files']
27
28
 
 
29
 
28
30
def unique_lcs(a, b):
29
31
    """Find the longest common subset for unique lines.
30
32
 
79
81
            k = len(stacks)
80
82
        # as an optimization, check if the next line comes right after
81
83
        # the previous line, because usually it does
82
 
        elif stacks and stacks[k] < apos and (k == len(stacks) - 1 or stacks[k+1] > apos):
 
84
        elif stacks and stacks[k] < apos and (k == len(stacks) - 1 or 
 
85
                                              stacks[k+1] > apos):
83
86
            k += 1
84
87
        else:
85
88
            k = bisect(stacks, apos)
101
104
    result.reverse()
102
105
    return result
103
106
 
104
 
assert unique_lcs('', '') == []
105
 
assert unique_lcs('a', 'a') == [(0, 0)]
106
 
assert unique_lcs('a', 'b') == []
107
 
assert unique_lcs('ab', 'ab') == [(0, 0), (1, 1)]
108
 
assert unique_lcs('abcde', 'cdeab') == [(2, 0), (3, 1), (4, 2)]
109
 
assert unique_lcs('cdeab', 'abcde') == [(0, 2), (1, 3), (2, 4)]
110
 
assert unique_lcs('abXde', 'abYde') == [(0, 0), (1, 1), (3, 3), (4, 4)]
111
 
assert unique_lcs('acbac', 'abc') == [(2, 1)]
112
107
 
113
108
def recurse_matches(a, b, ahi, bhi, answer, maxrecursion):
114
109
    """Find all of the matching text in the lines of a and b.
165
160
        for i in xrange(ahi - nahi):
166
161
            answer.append((nahi + i, nbhi + i))
167
162
 
168
 
a1 = []
169
 
recurse_matches(['a', None, 'b', None, 'c'], ['a', 'a', 'b', 'c', 'c'], 5, 5, a1, 10)
170
 
assert a1 == [(0, 0), (2, 2), (4, 4)]
171
 
a2 = []
172
 
recurse_matches(['a', 'c', 'b', 'a', 'c'], ['a', 'b', 'c'], 5, 3, a2, 10)
173
 
assert  a2 == [(0, 0), (2, 1), (4, 2)]
174
 
 
175
 
a3 = []
176
 
recurse_matches(['a', 'B', 'c', 'c', 'D', 'e'], ['a', 'b', 'c', 'c', 'd', 'e'], 6, 6, a3, 10)
177
 
# FIXME: recurse_matches won't match non-unique lines, surrounded by bogus text
178
 
# This is what it should be
179
 
#assert a2 == [(0,0), (2,2), (3,3), (5,5)]
180
 
# This is what it currently gives:
181
 
assert a3 == [(0,0), (5,5)]
182
 
 
183
163
 
184
164
class SequenceMatcher(difflib.SequenceMatcher):
185
165
    """Compare a pair of sequences using longest common subset."""
244
224
                # New block
245
225
                if start_a is None:
246
226
                    # We need to check from 0,0 until the current match
247
 
                    self._check_with_diff(alo-1, i_a+alo, blo-1, i_b+blo, answer)
 
227
                    self._check_with_diff(alo-1, i_a+alo, blo-1, i_b+blo, 
 
228
                                          answer)
248
229
                else:
249
230
                    answer.append((start_a+alo, start_b+blo, length))
250
231
                    self._check_with_diff(start_a+alo+length, i_a+alo,
274
255
                next_a = a + match_len
275
256
                next_b = b + match_len
276
257
 
 
258
 
277
259
# This is a version of unified_diff which only adds a factory parameter
278
260
# so that you can override the default SequenceMatcher
279
261
# this has been submitted as a patch to python
280
 
 
281
262
def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
282
263
                 tofiledate='', n=3, lineterm='\n',
283
264
                 sequencematcher=None):
342
323
                for line in b[j1:j2]:
343
324
                    yield '+' + line
344
325
 
 
326
 
345
327
def unified_diff_files(a, b, sequencematcher=None):
346
328
    """Generate the diff for two files.
347
329
    """
367
349
                        fromfile=a, tofile=b,
368
350
                        sequencematcher=sequencematcher)
369
351
 
 
352
 
370
353
def main(args):
371
354
    import optparse
372
355
    p = optparse.OptionParser(usage='%prog [options] file_a file_b'