File tree 2 files changed +18
-7
lines changed
2 files changed +18
-7
lines changed Original file line number Diff line number Diff line change @@ -401,6 +401,22 @@ fn test_str_get_maxinclusive() {
401
401
}
402
402
}
403
403
404
+ #[ test]
405
+ fn test_str_slicemut_rangetoinclusive_ok ( ) {
406
+ let mut s = "abcαβγ" . to_owned ( ) ;
407
+ let s: & mut str = & mut s;
408
+ & mut s[ ..=3 ] ; // before alpha
409
+ & mut s[ ..=5 ] ; // after alpha
410
+ }
411
+
412
+ #[ test]
413
+ #[ should_panic]
414
+ fn test_str_slicemut_rangetoinclusive_notok ( ) {
415
+ let mut s = "abcαβγ" . to_owned ( ) ;
416
+ let s: & mut str = & mut s;
417
+ & mut s[ ..=4 ] ; // middle of alpha, which is 2 bytes long
418
+ }
419
+
404
420
#[ test]
405
421
fn test_is_char_boundary ( ) {
406
422
let s = "ศไทย中华Việt Nam β-release 🐱123" ;
Original file line number Diff line number Diff line change @@ -2100,18 +2100,13 @@ mod traits {
2100
2100
fn index ( self , slice : & str ) -> & Self :: Output {
2101
2101
assert ! ( self . end != usize :: max_value( ) ,
2102
2102
"attempted to index str up to maximum usize" ) ;
2103
- let end = self . end + 1 ;
2104
- self . get ( slice) . unwrap_or_else ( || super :: slice_error_fail ( slice, 0 , end) )
2103
+ ( ..self . end +1 ) . index ( slice)
2105
2104
}
2106
2105
#[ inline]
2107
2106
fn index_mut ( self , slice : & mut str ) -> & mut Self :: Output {
2108
2107
assert ! ( self . end != usize :: max_value( ) ,
2109
2108
"attempted to index str up to maximum usize" ) ;
2110
- if slice. is_char_boundary ( self . end ) {
2111
- unsafe { self . get_unchecked_mut ( slice) }
2112
- } else {
2113
- super :: slice_error_fail ( slice, 0 , self . end + 1 )
2114
- }
2109
+ ( ..self . end +1 ) . index_mut ( slice)
2115
2110
}
2116
2111
}
2117
2112
You can’t perform that action at this time.
0 commit comments