Skip to content

Commit 79eb2f7

Browse files
committed
_expand_blocks utility simplified
1 parent 3d6139f commit 79eb2f7

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

Lib/difflib.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,23 @@ class SequenceMatcherBase:
4242
### Utilities
4343
########################################################################
4444

45-
def _expand_block_to_junk(junk, block, a, b, alo, ahi, blo, bhi, *, inverse=False):
46-
"""
47-
Expands block for consecutive matches at both sides if:
48-
a) characters match
49-
b) matching characters are in junk
50-
If inverse == True, (b) condition is inverted to: "are not in junk"
45+
def _expand_block(block, a, b, alo, ahi, blo, bhi, *, pred=None):
46+
"""Expands block for consecutive matches at both sides if characters match
47+
48+
pred: callable
49+
additionally, only expand if pred(matching_element) returns True
5150
"""
5251
i, j, k = block
5352
while i > alo and j > blo:
5453
el2 = b[j - 1]
55-
ok = el2 not in junk if inverse else el2 in junk
56-
if not ok or a[i - 1] != el2:
54+
if a[i - 1] != el2 or pred is not None and not pred(el2):
5755
break
5856
i -= 1
5957
j -= 1
6058
k += 1
6159
while i + k < ahi and j + k < bhi:
6260
el2 = b[j + k]
63-
ok = el2 not in junk if inverse else el2 in junk
64-
if not ok or a[i + k] != el2:
61+
if a[i + k] != el2 or pred is not None and not pred(el2):
6562
break
6663
k += 1
6764
return (i, j, k)
@@ -722,7 +719,8 @@ def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None):
722719
# "popular" non-junk elements aren't in b2j, which greatly speeds
723720
# the inner loop above, but also means "the best" match so far
724721
# doesn't contain any junk *or* popular non-junk elements.
725-
block = _expand_block_to_junk(bpopular, block, a, b, alo, ahi, blo, bhi)
722+
block = _expand_block(block, a, b, alo, ahi, blo, bhi,
723+
pred=bpopular.__contains__)
726724

727725
if bjunk:
728726
# Now that we have a wholly interesting match (albeit possibly
@@ -732,7 +730,8 @@ def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None):
732730
# figuring out what to do with it. In the case of an empty
733731
# interesting match, this is clearly the right thing to do,
734732
# because no other kind of match is possible in the regions.
735-
block = _expand_block_to_junk(bjunk, block, a, b, alo, ahi, blo, bhi)
733+
block = _expand_block(block, a, b, alo, ahi, blo, bhi,
734+
pred=bjunk.__contains__)
736735

737736
return Match._make(block)
738737

0 commit comments

Comments
 (0)