30
30
' isjunk for sequence matching')
31
31
difflib.SequenceMatcher.__init__(self, isjunk, a, b)
33
def _check_with_diff(self, alo, ahi, blo, bhi, answer):
34
"""Use the original diff algorithm on an unmatched section.
36
This will check to make sure the range is worth checking,
37
before doing any work.
39
:param alo: The last line that actually matched
40
:param ahi: The next line that actually matches
41
:param blo: Same as alo, only for the 'b' set
42
:param bhi: Same as ahi
43
:param answer: An array which will have the new ranges appended to it
47
# recurse_matches has an implementation design
48
# which does not match non-unique lines in the
49
# if they do not touch matching unique lines
50
# so we rerun the regular diff algorithm
51
# if find a large enough chunk.
53
# recurse_matches already looked at the direct
54
# neighbors, so we only need to run if there is
55
# enough space to do so
56
if ahi - alo > 2 and bhi - blo > 2:
57
a = self.a[alo+1:bhi-1]
58
b = self.b[blo+1:bhi-1]
59
m = difflib.SequenceMatcher(None, a, b)
60
new_blocks = m.get_matching_blocks()
61
# difflib always adds a final match
63
for blk in new_blocks:
64
answer.append((blk[0]+alo+1,
33
68
def __helper(self, alo, ahi, blo, bhi, answer):
35
70
a = self.a[alo:ahi]
50
if start_a is not None:
86
# We need to check from 0,0 until the current match
87
self._check_with_diff(alo-1, i_a+alo, blo-1, i_b+blo, answer)
51
89
answer.append((start_a+alo, start_b+blo, length))
53
# recurse_matches has an implementation design
54
# which does not match non-unique lines in the
55
# if they do not touch matching unique lines
56
# so we rerun the regular diff algorithm
57
# if find a large enough chunk.
90
self._check_with_diff(start_a+alo+length, i_a+alo,
91
start_b+blo+length, i_b+blo,
59
last_match_a = start_a + length
60
last_match_b = start_b + length
61
# recurse_matches already looked at the direct
62
# neighbors, so we only need to run if there is
63
# enough space to do so
64
if (i_a - last_match_a > 2
65
and i_b - last_match_b > 2):
66
m = SequenceMatcher(None,
67
a[last_match_a+1:i_a-1],
68
b[last_match_b+1:i_b-1])
69
new_blocks = m.get_matching_blocks()
70
# difflib always adds a final match
72
for blk in new_blocks:
73
answer.append((blk[0]+last_match_a+1,
74
blk[1]+last_match_b+1,
81
answer.append((start_a+blo, start_b+blo, length))
99
answer.append((start_a+alo, start_b+blo, length))
100
self._check_with_diff(start_a+alo+length, ahi+1,
101
start_b+blo+length, bhi+1,
104
# Nothing matched, so we need to send the complete text
105
self._check_with_diff(alo-1, ahi+1, blo-1, bhi+1, answer)
83
107
# This is a version of unified_diff which only adds a factory parameter
84
108
# so that you can override the default SequenceMatcher