Skip to content

Commit

Permalink
Rollup merge of rust-lang#92724 - inteon:cleanup, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Cleanup c_str.rs

Some code cleanups in `c_str.rs`.
No functional changes.

ref: bytecodealliance/rustix#163
  • Loading branch information
m-ou-se authored Feb 7, 2022
2 parents fadf0fd + afb7a50 commit 74bde99
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions library/std/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,15 +1252,16 @@ impl CStr {
/// assert!(cstr.is_err());
/// ```
#[stable(feature = "cstr_from_bytes", since = "1.10.0")]
pub fn from_bytes_with_nul(bytes: &[u8]) -> Result<&CStr, FromBytesWithNulError> {
pub fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError> {
let nul_pos = memchr::memchr(0, bytes);
if let Some(nul_pos) = nul_pos {
if nul_pos + 1 != bytes.len() {
return Err(FromBytesWithNulError::interior_nul(nul_pos));
match nul_pos {
Some(nul_pos) if nul_pos + 1 == bytes.len() => {
// SAFETY: We know there is only one nul byte, at the end
// of the byte slice.
Ok(unsafe { Self::from_bytes_with_nul_unchecked(bytes) })
}
Ok(unsafe { CStr::from_bytes_with_nul_unchecked(bytes) })
} else {
Err(FromBytesWithNulError::not_nul_terminated())
Some(nul_pos) => Err(FromBytesWithNulError::interior_nul(nul_pos)),
None => Err(FromBytesWithNulError::not_nul_terminated()),
}
}

Expand Down

0 comments on commit 74bde99

Please sign in to comment.