diff options
| author | bendn <bend.n@outlook.com> | 2025-05-25 02:01:04 +0700 |
|---|---|---|
| committer | bendn <bend.n@outlook.com> | 2025-05-26 01:50:13 +0700 |
| commit | 245bf503e2a948ac98170516d11df632e85a948b (patch) | |
| tree | ed9d53345cc19124eddcf50e5cbe7f120bb0c86a | |
| parent | 283db70ace62a0ae704a624e43b68c2ee44b87a6 (diff) | |
| download | rust-245bf503e2a948ac98170516d11df632e85a948b.tar.gz rust-245bf503e2a948ac98170516d11df632e85a948b.zip | |
increase perf of charsearcher for single ascii characters
| -rw-r--r-- | library/core/src/str/iter.rs | 2 | ||||
| -rw-r--r-- | library/core/src/str/pattern.rs | 32 |
2 files changed, 32 insertions, 2 deletions
diff --git a/library/core/src/str/iter.rs b/library/core/src/str/iter.rs index 425c4eaee28..49c581f352e 100644 --- a/library/core/src/str/iter.rs +++ b/library/core/src/str/iter.rs @@ -656,7 +656,7 @@ impl<'a, P: Pattern> SplitInternal<'a, P> { None } - #[inline] + #[inline(always)] fn next(&mut self) -> Option<&'a str> { if self.finished { return None; diff --git a/library/core/src/str/pattern.rs b/library/core/src/str/pattern.rs index bcbbb11c83b..e8189a2187b 100644 --- a/library/core/src/str/pattern.rs +++ b/library/core/src/str/pattern.rs @@ -429,8 +429,23 @@ 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 { + return match self + .haystack + .as_bytes() + .get(self.finger..self.finger_back)? + .iter() + .position(|x| *x == self.utf8_encoded[0]) + { + 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)?; @@ -498,6 +513,21 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> { } #[inline] fn next_match_back(&mut self) -> Option<(usize, usize)> { + if self.utf8_size == 1 { + return match self + .haystack + .get(self.finger..self.finger_back)? + .as_bytes() + .iter() + .rposition(|&x| x == self.utf8_encoded[0]) + { + 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 |
