Skip to content

Commit 37ba052

Browse files
committed
faster charsearcher
1 parent e0d014a commit 37ba052

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

library/core/src/str/pattern.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,22 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
431431
}
432432
#[inline]
433433
fn next_match(&mut self) -> Option<(usize, usize)> {
434+
if self.utf8_size == 1 {
435+
let find = |haystack: &[u8]| {
436+
if haystack.len() < 64 {
437+
haystack.iter().position(|&x| x == self.utf8_encoded[0])
438+
} else {
439+
memchr::memchr(self.utf8_encoded[0], haystack)
440+
}
441+
};
442+
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
443+
Some(x) => {
444+
self.finger += x + 1;
445+
Some((self.finger - 1, self.finger))
446+
}
447+
None => None,
448+
};
449+
}
434450
loop {
435451
// get the haystack after the last character found
436452
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
@@ -498,6 +514,22 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
498514
}
499515
#[inline]
500516
fn next_match_back(&mut self) -> Option<(usize, usize)> {
517+
if self.utf8_size == 1 {
518+
let find = |haystack: &[u8]| {
519+
if haystack.len() < 64 {
520+
memchr::memrchr(self.utf8_encoded[0], haystack)
521+
} else {
522+
haystack.iter().rposition(|&x| x == self.utf8_encoded[0])
523+
}
524+
};
525+
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
526+
Some(x) => {
527+
self.finger_back = self.finger + x;
528+
Some((self.finger_back, self.finger_back + 1))
529+
}
530+
None => None,
531+
};
532+
}
501533
let haystack = self.haystack.as_bytes();
502534
loop {
503535
// get the haystack up to but not including the last character searched

0 commit comments

Comments
 (0)