Skip to content

Commit 7645ff2

Browse files
ethanpailesBurntSushi
ethanpailes
authored andcommitted
regex/literal: fix bug in Boyer-Moore
This patch fixes an issue where skip resolution would go strait to the default value (the md2_shift) on a match failure after the shift_loop. Now we do the right thing, and first check in the skip table. The problem with going strait to the md2_shift is that you can accidentally shift to far when `window_end` actually is in the pattern (as is the case for the failing match).
1 parent 43bb64b commit 7645ff2

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/literals.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,9 @@ impl BoyerMooreSearch {
627627
if self.check_match(haystack, window_end) {
628628
return Some(window_end - (self.pattern.len() - 1));
629629
} else {
630-
window_end += self.md2_shift;
630+
let skip = self.skip_table[haystack[window_end] as usize];
631+
window_end +=
632+
if skip == 0 { self.md2_shift } else { skip };
631633
continue;
632634
}
633635
}
@@ -946,7 +948,6 @@ mod tests {
946948
}
947949

948950
#[test]
949-
#[should_panic]
950951
fn bm_backstop_boundary() {
951952
let haystack = b"\
952953
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

0 commit comments

Comments
 (0)