From bc576a1348d026e5a8653bfed9447b98c7a20a4f Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 29 Aug 2019 11:22:30 +0200 Subject: Fix some indexing issues in resetboring.py If we're too near to the end of a token list to accommodate a match to a sequence of words, we can stop searching (else we IndexError). When replacing one sequence of tokens with another, the search for more matches for the former is using the indexing in the token list we started with, before the first replacement. If the replacement's length doesn't match the original, we need to adjust match positions to take that into account. Change-Id: I727576c047be7bfaf047b618c5bc49604e3e7aad Reviewed-by: Frederik Gladhorn --- packaging-tools/resetboring.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'packaging-tools/resetboring.py') diff --git a/packaging-tools/resetboring.py b/packaging-tools/resetboring.py index 1d133f979..a9f01e8c6 100755 --- a/packaging-tools/resetboring.py +++ b/packaging-tools/resetboring.py @@ -685,6 +685,8 @@ class Selector(object): # Select interesting changes, discard boring. ind = 0 while True: ind = words.index(after[0], ind) + if len(words) < ind + len(after): + break # definitely doesn't match, here or later if all(words[i + ind] == tok for i, tok in enumerate(after)): yield ind ind += 1 @@ -695,8 +697,11 @@ class Selector(object): # Select interesting changes, discard boring. return True return False def purge(words, pair=swap, get=find): + offset, step = 0, len(pair[0]) - len(pair[1]) for ind in get(words): + ind += offset # Correct for earlier edits words[ind : ind + len(pair[1])] = pair[0] + offset += step # Update the correction return words yield test, purge -- cgit v1.2.3