@@ -373,6 +373,36 @@ impl ops::Index<ops::RangeFull> for OsString {
373
373
}
374
374
}
375
375
376
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
377
+ impl ops:: Index < ops:: Range < usize > > for OsString {
378
+ type Output = OsStr ;
379
+
380
+ #[ inline]
381
+ fn index ( & self , index : ops:: Range < usize > ) -> & OsStr {
382
+ OsStr :: from_inner ( & self . inner . as_slice ( ) [ index] )
383
+ }
384
+ }
385
+
386
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
387
+ impl ops:: Index < ops:: RangeFrom < usize > > for OsString {
388
+ type Output = OsStr ;
389
+
390
+ #[ inline]
391
+ fn index ( & self , index : ops:: RangeFrom < usize > ) -> & OsStr {
392
+ OsStr :: from_inner ( & self . inner . as_slice ( ) [ index] )
393
+ }
394
+ }
395
+
396
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
397
+ impl ops:: Index < ops:: RangeTo < usize > > for OsString {
398
+ type Output = OsStr ;
399
+
400
+ #[ inline]
401
+ fn index ( & self , index : ops:: RangeTo < usize > ) -> & OsStr {
402
+ OsStr :: from_inner ( & self . inner . as_slice ( ) [ index] )
403
+ }
404
+ }
405
+
376
406
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
377
407
impl ops:: Deref for OsString {
378
408
type Target = OsStr ;
@@ -966,6 +996,46 @@ impl AsInner<Slice> for OsStr {
966
996
}
967
997
}
968
998
999
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
1000
+ impl ops:: Index < ops:: RangeFull > for OsStr {
1001
+ type Output = OsStr ;
1002
+
1003
+ #[ inline]
1004
+ fn index ( & self , _: ops:: RangeFull ) -> & OsStr {
1005
+ self
1006
+ }
1007
+ }
1008
+
1009
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
1010
+ impl ops:: Index < ops:: Range < usize > > for OsStr {
1011
+ type Output = OsStr ;
1012
+
1013
+ #[ inline]
1014
+ fn index ( & self , index : ops:: Range < usize > ) -> & OsStr {
1015
+ OsStr :: from_inner ( & self . inner [ index] )
1016
+ }
1017
+ }
1018
+
1019
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
1020
+ impl ops:: Index < ops:: RangeFrom < usize > > for OsStr {
1021
+ type Output = OsStr ;
1022
+
1023
+ #[ inline]
1024
+ fn index ( & self , index : ops:: RangeFrom < usize > ) -> & OsStr {
1025
+ OsStr :: from_inner ( & self . inner [ index] )
1026
+ }
1027
+ }
1028
+
1029
+ #[ stable( feature = "os_str_slice" , since = "1.33.0" ) ]
1030
+ impl ops:: Index < ops:: RangeTo < usize > > for OsStr {
1031
+ type Output = OsStr ;
1032
+
1033
+ #[ inline]
1034
+ fn index ( & self , index : ops:: RangeTo < usize > ) -> & OsStr {
1035
+ OsStr :: from_inner ( & self . inner [ index] )
1036
+ }
1037
+ }
1038
+
969
1039
#[ cfg( test) ]
970
1040
mod tests {
971
1041
use super :: * ;
@@ -1133,4 +1203,54 @@ mod tests {
1133
1203
assert_eq ! ( & * rc2, os_str) ;
1134
1204
assert_eq ! ( & * arc2, os_str) ;
1135
1205
}
1206
+
1207
+ #[ test]
1208
+ fn slice_with_utf8_boundary ( ) {
1209
+ let os_str = OsStr :: new ( "Hello🌍🌎🌏" ) ;
1210
+ assert_eq ! ( os_str. len( ) , 17 ) ;
1211
+
1212
+ assert_eq ! ( os_str, & os_str[ ..] ) ;
1213
+ assert_eq ! ( os_str, & os_str[ ..17 ] ) ;
1214
+ assert_eq ! ( os_str, & os_str[ 0 ..] ) ;
1215
+ assert_eq ! ( os_str, & os_str[ 0 ..17 ] ) ;
1216
+
1217
+ assert_eq ! ( OsStr :: new( "Hello" ) , & os_str[ ..5 ] ) ;
1218
+ assert_eq ! ( OsStr :: new( "🌎🌏" ) , & os_str[ 9 ..] ) ;
1219
+ assert_eq ! ( OsStr :: new( "lo🌍" ) , & os_str[ 3 ..9 ] ) ;
1220
+
1221
+ let os_string = os_str. to_owned ( ) ;
1222
+ assert_eq ! ( os_str, & os_string[ ..] ) ;
1223
+ assert_eq ! ( os_str, & os_string[ ..17 ] ) ;
1224
+ assert_eq ! ( os_str, & os_string[ 0 ..] ) ;
1225
+ assert_eq ! ( os_str, & os_string[ 0 ..17 ] ) ;
1226
+
1227
+ assert_eq ! ( OsStr :: new( "Hello" ) , & os_string[ ..5 ] ) ;
1228
+ assert_eq ! ( OsStr :: new( "🌎🌏" ) , & os_string[ 9 ..] ) ;
1229
+ assert_eq ! ( OsStr :: new( "lo🌍" ) , & os_string[ 3 ..9 ] ) ;
1230
+ }
1231
+
1232
+ #[ test]
1233
+ #[ cfg( any( unix, target_os = "redox" , target_arch = "wasm32" ) ) ]
1234
+ fn slice_with_non_utf8_boundary_unix ( ) {
1235
+ #[ cfg( unix) ]
1236
+ use os:: unix:: ffi:: OsStrExt ;
1237
+ #[ cfg( target_os = "redox" ) ]
1238
+ use os:: redox:: ffi:: OsStrExt ;
1239
+
1240
+ let os_str = OsStr :: new ( "Hello🌍🌎🌏" ) ;
1241
+ assert_eq ! ( OsStr :: from_bytes( b"Hello\xf0 " ) , & os_str[ ..6 ] ) ;
1242
+ assert_eq ! ( OsStr :: from_bytes( b"\x9f \x8c \x8e \xf0 \x9f \x8c \x8f " ) , & os_str[ 10 ..] ) ;
1243
+ assert_eq ! ( OsStr :: from_bytes( b"\x8d \xf0 \x9f \x8c \x8e " ) , & os_str[ 8 ..13 ] ) ;
1244
+ }
1245
+
1246
+ #[ test]
1247
+ #[ cfg( windows) ]
1248
+ fn slice_with_non_utf8_boundary_windows ( ) {
1249
+ use os:: windows:: ffi:: OsStringExt ;
1250
+
1251
+ let os_str = OsStr :: new ( "Hello🌍🌎🌏" ) ;
1252
+ assert_eq ! ( OsString :: from_wide( & [ 0x48 , 0x65 , 0x6C , 0x6C , 0x6F , 0xD83C ] ) , & os_str[ ..7 ] ) ;
1253
+ assert_eq ! ( OsString :: from_wide( & [ 0xDF0E , 0xD83C , 0xDF0F ] ) , & os_str[ 11 ..] ) ;
1254
+ assert_eq ! ( OsString :: from_wide( & [ 0xDF0D , 0xD83C ] ) , & os_str[ 7 ..11 ] ) ;
1255
+ }
1136
1256
}
0 commit comments