Skip to content

Commit 7bd9d9f

Browse files
committed
str::is_char_boundary - few comments
1 parent 2ea0410 commit 7bd9d9f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

library/core/src/str/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,26 @@ impl str {
192192
#[stable(feature = "is_char_boundary", since = "1.9.0")]
193193
#[inline]
194194
pub fn is_char_boundary(&self, index: usize) -> bool {
195-
// 0 and len are always ok.
195+
// 0 is always ok.
196196
// Test for 0 explicitly so that it can optimize out the check
197197
// easily and skip reading string data for that case.
198+
// Note that optimizing `self.get(..index)` relies on this.
198199
if index == 0 {
199200
return true;
200201
}
202+
201203
match self.as_bytes().get(index) {
204+
// For `None` we have two options:
205+
//
206+
// - index == self.len()
207+
// Empty strings are valid, so return true
208+
// - index > self.len()
209+
// In this case return false
210+
//
211+
// The check is placed exactly here, because it improves generated
212+
// code on higher opt-levels. See PR #84751 for more details.
202213
None => index == self.len(),
214+
203215
// This is bit magic equivalent to: b < 128 || b >= 192
204216
Some(&b) => (b as i8) >= -0x40,
205217
}

0 commit comments

Comments
 (0)