File tree 3 files changed +55
-4
lines changed 3 files changed +55
-4
lines changed Original file line number Diff line number Diff line change 22
22
#![ feature( rand) ]
23
23
#![ feature( slice_rotate) ]
24
24
#![ feature( splice) ]
25
+ #![ feature( str_checked_slicing) ]
25
26
#![ feature( str_escape) ]
26
27
#![ feature( test) ]
27
28
#![ feature( unboxed_closures) ]
Original file line number Diff line number Diff line change @@ -358,6 +358,48 @@ fn test_slice_fail() {
358
358
& "中华Việt Nam" [ 0 ..2 ] ;
359
359
}
360
360
361
+ #[ test]
362
+ #[ should_panic]
363
+ fn test_str_slice_rangetoinclusive_max_panics ( ) {
364
+ & "hello" [ ... usize :: max_value ( ) ] ;
365
+ }
366
+
367
+ #[ test]
368
+ #[ should_panic]
369
+ fn test_str_slice_rangeinclusive_max_panics ( ) {
370
+ & "hello" [ 1 ...usize:: max_value ( ) ] ;
371
+ }
372
+
373
+ #[ test]
374
+ #[ should_panic]
375
+ fn test_str_slicemut_rangetoinclusive_max_panics ( ) {
376
+ let mut s = "hello" . to_owned ( ) ;
377
+ let s: & mut str = & mut s;
378
+ & mut s[ ... usize :: max_value ( ) ] ;
379
+ }
380
+
381
+ #[ test]
382
+ #[ should_panic]
383
+ fn test_str_slicemut_rangeinclusive_max_panics ( ) {
384
+ let mut s = "hello" . to_owned ( ) ;
385
+ let s: & mut str = & mut s;
386
+ & mut s[ 1 ...usize:: max_value ( ) ] ;
387
+ }
388
+
389
+ #[ test]
390
+ fn test_str_get_maxinclusive ( ) {
391
+ let mut s = "hello" . to_owned ( ) ;
392
+ {
393
+ let s: & str = & s;
394
+ assert_eq ! ( s. get( ...usize :: max_value( ) ) , None ) ;
395
+ assert_eq ! ( s. get( 1 ...usize :: max_value( ) ) , None ) ;
396
+ }
397
+ {
398
+ let s: & mut str = & mut s;
399
+ assert_eq ! ( s. get( ...usize :: max_value( ) ) , None ) ;
400
+ assert_eq ! ( s. get( 1 ...usize :: max_value( ) ) , None ) ;
401
+ }
402
+ }
361
403
362
404
#[ test]
363
405
fn test_is_char_boundary ( ) {
Original file line number Diff line number Diff line change @@ -1918,11 +1918,19 @@ mod traits {
1918
1918
type Output = str ;
1919
1919
#[ inline]
1920
1920
fn get ( self , slice : & str ) -> Option < & Self :: Output > {
1921
- ( self . start ..self . end +1 ) . get ( slice)
1921
+ if let Some ( end) = self . end . checked_add ( 1 ) {
1922
+ ( self . start ..end) . get ( slice)
1923
+ } else {
1924
+ None
1925
+ }
1922
1926
}
1923
1927
#[ inline]
1924
1928
fn get_mut ( self , slice : & mut str ) -> Option < & mut Self :: Output > {
1925
- ( self . start ..self . end +1 ) . get_mut ( slice)
1929
+ if let Some ( end) = self . end . checked_add ( 1 ) {
1930
+ ( self . start ..end) . get_mut ( slice)
1931
+ } else {
1932
+ None
1933
+ }
1926
1934
}
1927
1935
#[ inline]
1928
1936
unsafe fn get_unchecked ( self , slice : & str ) -> & Self :: Output {
@@ -1953,15 +1961,15 @@ mod traits {
1953
1961
type Output = str ;
1954
1962
#[ inline]
1955
1963
fn get ( self , slice : & str ) -> Option < & Self :: Output > {
1956
- if slice. is_char_boundary ( self . end + 1 ) {
1964
+ if self . end < usize :: max_value ( ) && slice. is_char_boundary ( self . end + 1 ) {
1957
1965
Some ( unsafe { self . get_unchecked ( slice) } )
1958
1966
} else {
1959
1967
None
1960
1968
}
1961
1969
}
1962
1970
#[ inline]
1963
1971
fn get_mut ( self , slice : & mut str ) -> Option < & mut Self :: Output > {
1964
- if slice. is_char_boundary ( self . end + 1 ) {
1972
+ if self . end < usize :: max_value ( ) && slice. is_char_boundary ( self . end + 1 ) {
1965
1973
Some ( unsafe { self . get_unchecked_mut ( slice) } )
1966
1974
} else {
1967
1975
None
You can’t perform that action at this time.
0 commit comments