Rollup merge of #32456 - bluss:str-zero, r=alexcrichton
Hardcode accepting 0 as a valid str char boundary If we check explicitly for index == 0, that removes the need to read the byte at index 0, so it avoids a trip to the string's memory, and it optimizes out the slicing index' bounds check whenever it is (a constant) zero.
This commit is contained in:
commit
671027817c
1 changed files with 5 additions and 1 deletions
|
@ -1953,7 +1953,10 @@ impl StrExt for str {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_char_boundary(&self, index: usize) -> bool {
|
fn is_char_boundary(&self, index: usize) -> bool {
|
||||||
if index == self.len() { return true; }
|
// 0 and len are always ok.
|
||||||
|
// Test for 0 explicitly so that it can optimize out the check
|
||||||
|
// easily and skip reading string data for that case.
|
||||||
|
if index == 0 || index == self.len() { return true; }
|
||||||
match self.as_bytes().get(index) {
|
match self.as_bytes().get(index) {
|
||||||
None => false,
|
None => false,
|
||||||
Some(&b) => b < 128 || b >= 192,
|
Some(&b) => b < 128 || b >= 192,
|
||||||
|
@ -2026,6 +2029,7 @@ impl StrExt for str {
|
||||||
self.find(pat)
|
self.find(pat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn split_at(&self, mid: usize) -> (&str, &str) {
|
fn split_at(&self, mid: usize) -> (&str, &str) {
|
||||||
// is_char_boundary checks that the index is in [0, .len()]
|
// is_char_boundary checks that the index is in [0, .len()]
|
||||||
if self.is_char_boundary(mid) {
|
if self.is_char_boundary(mid) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue