Skip to content

Commit 41b5a12

Browse files
committed
add automatic null block removal
1 parent e44b27d commit 41b5a12

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

Lib/difflib.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ def _expand_block(block, a, b, alo, ahi, blo, bhi, *, pred=None):
6363
k += 1
6464
return (i, j, k)
6565

66-
def _collapse_adjacent_blocks(blocks):
66+
def _collapse_adjacent_blocks(blocks, remove_zero_length=True):
6767
"""Collapses adjacent blocks
6868
"""
6969
i1 = j1 = k1 = 0
7070
for i2, j2, k2 in blocks:
71+
if remove_zero_length and not k2:
72+
continue
7173
# Is this block adjacent to i1, j1, k1?
7274
if i1 + k1 == i2 and j1 + k1 == j2:
7375
# Yes, so collapse them -- this just increases the length of
@@ -274,13 +276,13 @@ def get_matching_blocks(self):
274276
275277
When `_get_matching_blocks` is implemented, this method takes care of:
276278
1. Appending last dummy tripple
277-
2. Collapsing adjacent blocks
279+
2. Collapsing adjacent blocks (after removing empty blocks)
278280
3. Caching
279281
"""
280282
blocks = self.matching_blocks
281283
if blocks is None:
282284
blocks = self._get_matching_blocks()
283-
blocks = _collapse_adjacent_blocks(blocks)
285+
blocks = _collapse_adjacent_blocks(blocks, remove_zero_length=True)
284286
blocks = list(map(Match._make, blocks))
285287
# Append dummy at the end
286288
blocks.append(Match(len(self.a), len(self.b), 0))

0 commit comments

Comments
 (0)