From edf45e6f5fa54705298ba14f3216cfb5277c0908 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 27 Feb 2019 17:37:40 -0500 Subject: [PATCH] literal: fix reverse suffix literal optimization again The position at which we start searching for the literal should begin at the given starting position, not 0. --- src/exec.rs | 12 ++++++------ tests/regression.rs | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/exec.rs b/src/exec.rs index dc5c26860f..dd30c81ec5 100644 --- a/src/exec.rs +++ b/src/exec.rs @@ -745,13 +745,13 @@ impl<'c> ExecNoSync<'c> { debug_assert!(lcs.len() >= 1); let mut start = original_start; let mut end = start; - let mut last_literal_match = 0; + let mut last_literal = start; while end <= text.len() { - last_literal_match += match lcs.find(&text[last_literal_match..]) { + last_literal += match lcs.find(&text[last_literal..]) { None => return Some(NoMatch(text.len())), Some(i) => i, }; - end = last_literal_match + lcs.len(); + end = last_literal + lcs.len(); match dfa::Fsm::reverse( &self.ro.dfa_reverse, self.cache, @@ -760,10 +760,10 @@ impl<'c> ExecNoSync<'c> { end - start, ) { Match(0) | NoMatch(0) => return None, - Match(s) => return Some(Match((s + start, end))), + Match(i) => return Some(Match((start + i, end))), NoMatch(i) => { - start = i; - last_literal_match += 1; + start += i; + last_literal += 1; continue; } Quit => return Some(Quit), diff --git a/tests/regression.rs b/tests/regression.rs index 8dcc189942..724e01bfa0 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -83,7 +83,9 @@ ismatch!(strange_anchor_non_complete_prefix, r"a^{2}", "", false); ismatch!(strange_anchor_non_complete_suffix, r"${2}a", "", false); // See: https://github.com/BurntSushi/ripgrep/issues/1203 -ismatch!(wat1, r"[0-4][0-4][0-4]000", "153.230000", true); +ismatch!(reverse_suffix1, r"[0-4][0-4][0-4]000", "153.230000", true); +ismatch!(reverse_suffix2, r"\d\d\d000", "153.230000\n", true); +matiter!(reverse_suffix3, r"\d\d\d000", "153.230000\n", (4, 10)); // See: https://github.com/rust-lang/regex/issues/334 mat!(captures_after_dfa_premature_end, r"a(b*(X|$))?", "abcbX",