File tree 1 file changed +15
-3
lines changed
1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -192,14 +192,26 @@ impl str {
192
192
#[ stable( feature = "is_char_boundary" , since = "1.9.0" ) ]
193
193
#[ inline]
194
194
pub fn is_char_boundary ( & self , index : usize ) -> bool {
195
- // 0 and len are always ok.
195
+ // 0 is always ok.
196
196
// Test for 0 explicitly so that it can optimize out the check
197
197
// easily and skip reading string data for that case.
198
- if index == 0 || index == self . len ( ) {
198
+ // Note that optimizing `self.get(..index)` relies on this.
199
+ if index == 0 {
199
200
return true ;
200
201
}
202
+
201
203
match self . as_bytes ( ) . get ( index) {
202
- None => false ,
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.
213
+ None => index == self . len ( ) ,
214
+
203
215
// This is bit magic equivalent to: b < 128 || b >= 192
204
216
Some ( & b) => ( b as i8 ) >= -0x40 ,
205
217
}
You can’t perform that action at this time.
0 commit comments