Use memmem for glob literal substring searches

This commit is contained in:
Rui Ueyama committed 2026-09-13 17:53:17 +09:00
1 parent e09ce32f73
commit 4fa0487aa3
1 file changed
+1 -18
+1 -18
View File
@@ -153,7 +153,7 @@ impl Pattern {
next = Some((x + 1, y));
y += 1;
if let Some(Token::Str(literal)) = self.tokens.get(y) {
let Some(pos) = find(&s[x..], literal) else {
let Some(pos) = memchr::memmem::find(&s[x..], literal) else {
return false;
};
let pos = x + pos;
@@ -201,23 +201,6 @@ fn push_char(tokens: &mut Vec<Token>, c: u8) {
}
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
if needle.is_empty() {
return Some(0);
}
let end = haystack.len().checked_sub(needle.len())? + 1;
let mut pos = 0;
while pos < end {
pos += memchr::memchr(needle[0], &haystack[pos..end])?;
if haystack[pos..].starts_with(needle) {
return Some(pos);
}
pos += 1;
}
None
}
// Nfa matches many glob patterns in parallel by representing each state
// with one bit. It is used only for large pattern sets; matching individual
// patterns is faster when there are only a few of them.