Skip to content

Commit 0c48358

Browse files
authored
Unrolled build for #141516
Rollup merge of #141516 - bend-n:okay, r=workingjubilee speed up charsearcher for ascii chars attempt at fixing #82471 this implementation should be valid because ascii characters are always one byte and there are no continuation bytes that overlap with ascii characters im not completely sure that this is _always_ an improvement but it seems to be an improvement for this case and i dont think it can significantly regress any cases
2 parents 40d2563 + 245bf50 commit 0c48358

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

library/core/src/str/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ impl<'a, P: Pattern> SplitInternal<'a, P> {
656656
None
657657
}
658658

659-
#[inline]
659+
#[inline(always)]
660660
fn next(&mut self) -> Option<&'a str> {
661661
if self.finished {
662662
return None;

library/core/src/str/pattern.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,23 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
429429
SearchStep::Done
430430
}
431431
}
432-
#[inline]
432+
#[inline(always)]
433433
fn next_match(&mut self) -> Option<(usize, usize)> {
434+
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+
{
442+
Some(x) => {
443+
self.finger += x + 1;
444+
Some((self.finger - 1, self.finger))
445+
}
446+
None => None,
447+
};
448+
}
434449
loop {
435450
// get the haystack after the last character found
436451
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
@@ -498,6 +513,21 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
498513
}
499514
#[inline]
500515
fn next_match_back(&mut self) -> Option<(usize, usize)> {
516+
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+
{
524+
Some(x) => {
525+
self.finger_back = self.finger + x;
526+
Some((self.finger_back, self.finger_back + 1))
527+
}
528+
None => None,
529+
};
530+
}
501531
let haystack = self.haystack.as_bytes();
502532
loop {
503533
// get the haystack up to but not including the last character searched

0 commit comments

Comments
 (0)