Skip to content

faster charsearcher #141808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion library/core/src/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,18 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
SearchStep::Done
}
}
#[inline]
#[inline(always)]
fn next_match(&mut self) -> Option<(usize, usize)> {
if self.utf8_size == 1 {
let find = |haystack: &[u8]| memchr::memchr(self.utf8_encoded[0], haystack);
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
Some(x) => {
self.finger += x + 1;
Some((self.finger - 1, self.finger))
}
None => None,
};
}
loop {
// get the haystack after the last character found
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
Expand Down Expand Up @@ -498,6 +508,16 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
}
#[inline]
fn next_match_back(&mut self) -> Option<(usize, usize)> {
if self.utf8_size == 1 {
let find = |haystack: &[u8]| memchr::memrchr(self.utf8_encoded[0], haystack);
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
Some(x) => {
self.finger_back = self.finger + x;
Some((self.finger_back, self.finger_back + 1))
}
None => None,
};
}
let haystack = self.haystack.as_bytes();
loop {
// get the haystack up to but not including the last character searched
Expand Down
Loading