mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-08-10 09:01:59 -07:00
regex: fix matching bug when text anchors are used
It turns out that if there are text anchors (that is, \A or \z, or ^/$ when multi-line is disabled), then the "fast" line searching path isn't quite correct. Since searching without multi-line mode is exceptionally rare, we just look for the presence of text anchors and specifically disable the line terminator option in 'grep-regex'. This in turn inhibits the "fast" line searching path. Fixes #2260
This commit is contained in:
@@ -108,7 +108,7 @@ impl<'s, M: Matcher, S: Sink> Core<'s, M, S> {
|
||||
}
|
||||
|
||||
pub fn match_by_line(&mut self, buf: &[u8]) -> Result<bool, S::Error> {
|
||||
if self.is_line_by_line_fast() {
|
||||
if dbg!(self.is_line_by_line_fast()) {
|
||||
self.match_by_line_fast(buf)
|
||||
} else {
|
||||
self.match_by_line_slow(buf)
|
||||
|
@@ -1512,4 +1512,31 @@ and exhibited clearly, with a label attached.\
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// See: https://github.com/BurntSushi/ripgrep/issues/2260
|
||||
#[test]
|
||||
fn regression_2260() {
|
||||
use grep_regex::RegexMatcherBuilder;
|
||||
|
||||
use crate::SearcherBuilder;
|
||||
|
||||
let matcher = RegexMatcherBuilder::new()
|
||||
.line_terminator(Some(b'\n'))
|
||||
.build(r"^\w+$")
|
||||
.unwrap();
|
||||
let mut searcher = SearcherBuilder::new().line_number(true).build();
|
||||
|
||||
let mut matched = false;
|
||||
searcher
|
||||
.search_slice(
|
||||
&matcher,
|
||||
b"GATC\n",
|
||||
crate::sinks::UTF8(|_, _| {
|
||||
matched = true;
|
||||
Ok(true)
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matched);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user