File tree 1 file changed +8
-7
lines changed
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -1252,15 +1252,16 @@ impl CStr {
1252
1252
/// assert!(cstr.is_err());
1253
1253
/// ```
1254
1254
#[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
1255
- pub fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesWithNulError > {
1255
+ pub fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
1256
1256
let nul_pos = memchr:: memchr ( 0 , bytes) ;
1257
- if let Some ( nul_pos) = nul_pos {
1258
- if nul_pos + 1 != bytes. len ( ) {
1259
- return Err ( FromBytesWithNulError :: interior_nul ( nul_pos) ) ;
1257
+ match nul_pos {
1258
+ Some ( nul_pos) if nul_pos + 1 == bytes. len ( ) => {
1259
+ // SAFETY: We know there is only one nul byte, at the end
1260
+ // of the byte slice.
1261
+ Ok ( unsafe { Self :: from_bytes_with_nul_unchecked ( bytes) } )
1260
1262
}
1261
- Ok ( unsafe { CStr :: from_bytes_with_nul_unchecked ( bytes) } )
1262
- } else {
1263
- Err ( FromBytesWithNulError :: not_nul_terminated ( ) )
1263
+ Some ( nul_pos) => Err ( FromBytesWithNulError :: interior_nul ( nul_pos) ) ,
1264
+ None => Err ( FromBytesWithNulError :: not_nul_terminated ( ) ) ,
1264
1265
}
1265
1266
}
1266
1267
You can’t perform that action at this time.
0 commit comments