@@ -194,15 +194,65 @@ fn slice_encoded_bytes() {
194
194
}
195
195
196
196
#[ test]
197
- #[ should_panic( expected = "byte index 2 is not an OsStr boundary" ) ]
197
+ #[ should_panic]
198
+ fn slice_out_of_bounds ( ) {
199
+ let crab = OsStr :: new ( "🦀" ) ;
200
+ let _ = crab. slice_encoded_bytes ( ..5 ) ;
201
+ }
202
+
203
+ #[ test]
204
+ #[ should_panic]
198
205
fn slice_mid_char ( ) {
199
206
let crab = OsStr :: new ( "🦀" ) ;
200
207
let _ = crab. slice_encoded_bytes ( ..2 ) ;
201
208
}
202
209
210
+ #[ cfg( unix) ]
211
+ #[ test]
212
+ #[ should_panic( expected = "byte index 1 is not an OsStr boundary" ) ]
213
+ fn slice_invalid_data ( ) {
214
+ use crate :: os:: unix:: ffi:: OsStrExt ;
215
+
216
+ let os_string = OsStr :: from_bytes ( b"\xFF \xFF " ) ;
217
+ let _ = os_string. slice_encoded_bytes ( 1 ..) ;
218
+ }
219
+
220
+ #[ cfg( unix) ]
221
+ #[ test]
222
+ #[ should_panic( expected = "byte index 1 is not an OsStr boundary" ) ]
223
+ fn slice_partial_utf8 ( ) {
224
+ use crate :: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
225
+
226
+ let part_crab = OsStr :: from_bytes ( & "🦀" . as_bytes ( ) [ ..3 ] ) ;
227
+ let mut os_string = OsString :: from_vec ( vec ! [ 0xFF ] ) ;
228
+ os_string. push ( part_crab) ;
229
+ let _ = os_string. slice_encoded_bytes ( 1 ..) ;
230
+ }
231
+
232
+ #[ cfg( unix) ]
233
+ #[ test]
234
+ fn slice_invalid_edge ( ) {
235
+ use crate :: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
236
+
237
+ let os_string = OsStr :: from_bytes ( b"a\xFF a" ) ;
238
+ assert_eq ! ( os_string. slice_encoded_bytes( ..1 ) , "a" ) ;
239
+ assert_eq ! ( os_string. slice_encoded_bytes( 1 ..) , OsStr :: from_bytes( b"\xFF a" ) ) ;
240
+ assert_eq ! ( os_string. slice_encoded_bytes( ..2 ) , OsStr :: from_bytes( b"a\xFF " ) ) ;
241
+ assert_eq ! ( os_string. slice_encoded_bytes( 2 ..) , "a" ) ;
242
+
243
+ let os_string = OsStr :: from_bytes ( & "abc🦀" . as_bytes ( ) [ ..6 ] ) ;
244
+ assert_eq ! ( os_string. slice_encoded_bytes( ..3 ) , "abc" ) ;
245
+ assert_eq ! ( os_string. slice_encoded_bytes( 3 ..) , OsStr :: from_bytes( b"\xF0 \x9F \xA6 " ) ) ;
246
+
247
+ let mut os_string = OsString :: from_vec ( vec ! [ 0xFF ] ) ;
248
+ os_string. push ( "🦀" ) ;
249
+ assert_eq ! ( os_string. slice_encoded_bytes( ..1 ) , OsStr :: from_bytes( b"\xFF " ) ) ;
250
+ assert_eq ! ( os_string. slice_encoded_bytes( 1 ..) , "🦀" ) ;
251
+ }
252
+
203
253
#[ cfg( windows) ]
204
254
#[ test]
205
- #[ should_panic( expected = "byte index 3 is not an OsStr boundary " ) ]
255
+ #[ should_panic( expected = "byte index 3 lies between surrogate codepoints " ) ]
206
256
fn slice_between_surrogates ( ) {
207
257
use crate :: os:: windows:: ffi:: OsStringExt ;
208
258
@@ -216,10 +266,14 @@ fn slice_between_surrogates() {
216
266
fn slice_surrogate_edge ( ) {
217
267
use crate :: os:: windows:: ffi:: OsStringExt ;
218
268
219
- let os_string = OsString :: from_wide ( & [ 0xD800 ] ) ;
220
- let mut with_crab = os_string. clone ( ) ;
221
- with_crab. push ( "🦀" ) ;
269
+ let surrogate = OsString :: from_wide ( & [ 0xD800 ] ) ;
270
+ let mut pre_crab = surrogate. clone ( ) ;
271
+ pre_crab. push ( "🦀" ) ;
272
+ assert_eq ! ( pre_crab. slice_encoded_bytes( ..3 ) , surrogate) ;
273
+ assert_eq ! ( pre_crab. slice_encoded_bytes( 3 ..) , "🦀" ) ;
222
274
223
- assert_eq ! ( with_crab. slice_encoded_bytes( ..3 ) , os_string) ;
224
- assert_eq ! ( with_crab. slice_encoded_bytes( 3 ..) , "🦀" ) ;
275
+ let mut post_crab = OsString :: from ( "🦀" ) ;
276
+ post_crab. push ( & surrogate) ;
277
+ assert_eq ! ( post_crab. slice_encoded_bytes( ..4 ) , "🦀" ) ;
278
+ assert_eq ! ( post_crab. slice_encoded_bytes( 4 ..) , surrogate) ;
225
279
}
0 commit comments