diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2016-03-26 09:07:22 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2016-03-26 13:42:04 +0530 |
| commit | 671027817c6ef7351db6dbb47cfac42cf5f0707a (patch) | |
| tree | 14bd20964129c705112fb078874230aaf5419fad | |
| parent | 023fae61751a572f722b59ef78862878580f204d (diff) | |
| parent | f621193e5ea7ac54fcd37f0e730e955fd9f61200 (diff) | |
| download | rust-671027817c6ef7351db6dbb47cfac42cf5f0707a.tar.gz rust-671027817c6ef7351db6dbb47cfac42cf5f0707a.zip | |
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.
| -rw-r--r-- | src/libcore/str/mod.rs | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 8f21f109e81..d5a5e2b4741 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -1953,7 +1953,10 @@ impl StrExt for str { #[inline] 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) { None => false, Some(&b) => b < 128 || b >= 192, @@ -2026,6 +2029,7 @@ impl StrExt for str { self.find(pat) } + #[inline] fn split_at(&self, mid: usize) -> (&str, &str) { // is_char_boundary checks that the index is in [0, .len()] if self.is_char_boundary(mid) { |
