Commit 79ffaec
committed
Fix Boyer-Moore bad character shift having no effect
The bad_character_heuristic() reassigned the for-loop variable i
inside the loop body, which has no effect on iteration in Python.
As a result the bad-character shift was dead code and the search
degenerated into brute-force O(n*m) checking every position,
while still claiming O(n/m) in the module docstring.
Convert the loop to a while loop so the shift actually applies,
guaranteeing at least one position of progress per iteration via
max(i + 1, mismatch_index - match_index).
Verified: all doctests pass, 2000 randomized comparisons against
brute-force search pass, and the example from the issue now takes
9 iterations instead of 29.
Fixes #148441 parent f5988cc commit 79ffaec
1 file changed
Lines changed: 7 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| 94 | + | |
93 | 95 | | |
94 | 96 | | |
95 | | - | |
96 | | - | |
97 | | - | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
98 | 101 | | |
99 | 102 | | |
100 | 103 | | |
| |||
0 commit comments