Skip to content

Commit c210227

Browse files
committed
faster charsearcher
1 parent ec28ae9 commit c210227

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

library/core/src/str/pattern.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,14 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
432432
#[inline(always)]
433433
fn next_match(&mut self) -> Option<(usize, usize)> {
434434
if self.utf8_size == 1 {
435-
return match self
436-
.haystack
437-
.as_bytes()
438-
.get(self.finger..self.finger_back)?
439-
.iter()
440-
.position(|x| *x == self.utf8_encoded[0])
441-
{
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)?) {
442443
Some(x) => {
443444
self.finger += x + 1;
444445
Some((self.finger - 1, self.finger))
@@ -514,13 +515,14 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
514515
#[inline]
515516
fn next_match_back(&mut self) -> Option<(usize, usize)> {
516517
if self.utf8_size == 1 {
517-
return match self
518-
.haystack
519-
.get(self.finger..self.finger_back)?
520-
.as_bytes()
521-
.iter()
522-
.rposition(|&x| x == self.utf8_encoded[0])
523-
{
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)?) {
524526
Some(x) => {
525527
self.finger_back = self.finger + x;
526528
Some((self.finger_back, self.finger_back + 1))

0 commit comments

Comments
 (0)