Skip to content

Commit f621193

Browse files
committed
Accept 0 as a valid str char boundary
Index 0 must be a valid char boundary (invariant of str that it contains valid UTF-8 data). 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 zero. With this change, the following examples all change from having a read of the byte at 0 and a branch to possibly panicing, to having the bounds checking optimized away. ```rust pub fn split(s: &str) -> (&str, &str) { s.split_at(0) } pub fn both(s: &str) -> &str { &s[0..s.len()] } pub fn first(s: &str) -> &str { &s[..0] } pub fn last(s: &str) -> &str { &s[0..] } ```
1 parent 80e7a1b commit f621193

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/libcore/str/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,10 @@ impl StrExt for str {
18921892

18931893
#[inline]
18941894
fn is_char_boundary(&self, index: usize) -> bool {
1895-
if index == self.len() { return true; }
1895+
// 0 and len are always ok.
1896+
// Test for 0 explicitly so that it can optimize out the check
1897+
// easily and skip reading string data for that case.
1898+
if index == 0 || index == self.len() { return true; }
18961899
match self.as_bytes().get(index) {
18971900
None => false,
18981901
Some(&b) => b < 128 || b >= 192,

0 commit comments

Comments
 (0)