@@ -198,15 +198,65 @@ fn slice_encoded_bytes() {
198198}
199199
200200#[ test]
201- #[ should_panic( expected = "byte index 2 is not an OsStr boundary" ) ]
201+ #[ should_panic]
202+ fn slice_out_of_bounds ( ) {
203+ let crab = OsStr :: new ( "🦀" ) ;
204+ let _ = crab. slice_encoded_bytes ( ..5 ) ;
205+ }
206+
207+ #[ test]
208+ #[ should_panic]
202209fn slice_mid_char ( ) {
203210 let crab = OsStr :: new ( "🦀" ) ;
204211 let _ = crab. slice_encoded_bytes ( ..2 ) ;
205212}
206213
214+ #[ cfg( unix) ]
215+ #[ test]
216+ #[ should_panic( expected = "byte index 1 is not an OsStr boundary" ) ]
217+ fn slice_invalid_data ( ) {
218+ use crate :: os:: unix:: ffi:: OsStrExt ;
219+
220+ let os_string = OsStr :: from_bytes ( b"\xFF \xFF " ) ;
221+ let _ = os_string. slice_encoded_bytes ( 1 ..) ;
222+ }
223+
224+ #[ cfg( unix) ]
225+ #[ test]
226+ #[ should_panic( expected = "byte index 1 is not an OsStr boundary" ) ]
227+ fn slice_partial_utf8 ( ) {
228+ use crate :: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
229+
230+ let part_crab = OsStr :: from_bytes ( & "🦀" . as_bytes ( ) [ ..3 ] ) ;
231+ let mut os_string = OsString :: from_vec ( vec ! [ 0xFF ] ) ;
232+ os_string. push ( part_crab) ;
233+ let _ = os_string. slice_encoded_bytes ( 1 ..) ;
234+ }
235+
236+ #[ cfg( unix) ]
237+ #[ test]
238+ fn slice_invalid_edge ( ) {
239+ use crate :: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
240+
241+ let os_string = OsStr :: from_bytes ( b"a\xFF a" ) ;
242+ assert_eq ! ( os_string. slice_encoded_bytes( ..1 ) , "a" ) ;
243+ assert_eq ! ( os_string. slice_encoded_bytes( 1 ..) , OsStr :: from_bytes( b"\xFF a" ) ) ;
244+ assert_eq ! ( os_string. slice_encoded_bytes( ..2 ) , OsStr :: from_bytes( b"a\xFF " ) ) ;
245+ assert_eq ! ( os_string. slice_encoded_bytes( 2 ..) , "a" ) ;
246+
247+ let os_string = OsStr :: from_bytes ( & "abc🦀" . as_bytes ( ) [ ..6 ] ) ;
248+ assert_eq ! ( os_string. slice_encoded_bytes( ..3 ) , "abc" ) ;
249+ assert_eq ! ( os_string. slice_encoded_bytes( 3 ..) , OsStr :: from_bytes( b"\xF0 \x9F \xA6 " ) ) ;
250+
251+ let mut os_string = OsString :: from_vec ( vec ! [ 0xFF ] ) ;
252+ os_string. push ( "🦀" ) ;
253+ assert_eq ! ( os_string. slice_encoded_bytes( ..1 ) , OsStr :: from_bytes( b"\xFF " ) ) ;
254+ assert_eq ! ( os_string. slice_encoded_bytes( 1 ..) , "🦀" ) ;
255+ }
256+
207257#[ cfg( windows) ]
208258#[ test]
209- #[ should_panic( expected = "byte index 3 is not an OsStr boundary " ) ]
259+ #[ should_panic( expected = "byte index 3 lies between surrogate codepoints " ) ]
210260fn slice_between_surrogates ( ) {
211261 use crate :: os:: windows:: ffi:: OsStringExt ;
212262
@@ -220,10 +270,14 @@ fn slice_between_surrogates() {
220270fn slice_surrogate_edge ( ) {
221271 use crate :: os:: windows:: ffi:: OsStringExt ;
222272
223- let os_string = OsString :: from_wide ( & [ 0xD800 ] ) ;
224- let mut with_crab = os_string. clone ( ) ;
225- with_crab. push ( "🦀" ) ;
273+ let surrogate = OsString :: from_wide ( & [ 0xD800 ] ) ;
274+ let mut pre_crab = surrogate. clone ( ) ;
275+ pre_crab. push ( "🦀" ) ;
276+ assert_eq ! ( pre_crab. slice_encoded_bytes( ..3 ) , surrogate) ;
277+ assert_eq ! ( pre_crab. slice_encoded_bytes( 3 ..) , "🦀" ) ;
226278
227- assert_eq ! ( with_crab. slice_encoded_bytes( ..3 ) , os_string) ;
228- assert_eq ! ( with_crab. slice_encoded_bytes( 3 ..) , "🦀" ) ;
279+ let mut post_crab = OsString :: from ( "🦀" ) ;
280+ post_crab. push ( & surrogate) ;
281+ assert_eq ! ( post_crab. slice_encoded_bytes( ..4 ) , "🦀" ) ;
282+ assert_eq ! ( post_crab. slice_encoded_bytes( 4 ..) , surrogate) ;
229283}
0 commit comments