Make NulTerminated::new return option

This commit is contained in:
Jeremy Soller
2025-01-09 13:33:56 -07:00
parent af50d18d16
commit 90f30c5f51
4 changed files with 12 additions and 12 deletions
+3 -3
View File
@@ -394,7 +394,7 @@ pub unsafe extern "C" fn strcoll(s1: *const c_char, s2: *const c_char) -> c_int
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strcpy.html>.
#[no_mangle]
pub unsafe extern "C" fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char {
let src_iter = unsafe { NulTerminated::new(src) };
let src_iter = unsafe { NulTerminated::new(src).unwrap() };
let src_dest_iter = unsafe { SrcDstPtrIter::new(src_iter.chain(once(&0)), dst) };
for (src_item, dst_item) in src_dest_iter {
dst_item.write(*src_item);
@@ -524,7 +524,7 @@ pub unsafe extern "C" fn strlcpy(dst: *mut c_char, src: *const c_char, n: size_t
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strlen.html>.
#[no_mangle]
pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t {
unsafe { NulTerminated::new(s) }.count()
unsafe { NulTerminated::new(s).unwrap() }.count()
}
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strncat.html>.
@@ -592,7 +592,7 @@ pub unsafe extern "C" fn strndup(s1: *const c_char, size: size_t) -> *mut c_char
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/strlen.html>.
#[no_mangle]
pub unsafe extern "C" fn strnlen(s: *const c_char, size: size_t) -> size_t {
unsafe { NulTerminated::new(s) }.take(size).count()
unsafe { NulTerminated::new(s).unwrap() }.take(size).count()
}
/// Non-POSIX, see <https://en.cppreference.com/w/c/string/byte/strlen>.
+4 -4
View File
@@ -67,8 +67,8 @@ pub unsafe extern "C" fn rindex(s: *const c_char, c: c_int) -> *mut c_char {
#[no_mangle]
pub unsafe extern "C" fn strcasecmp(s1: *const c_char, s2: *const c_char) -> c_int {
// SAFETY: the caller must ensure that s1 and s2 point to nul-terminated buffers.
let s1_iter = unsafe { NulTerminated::new(s1) }.chain(once(&0));
let s2_iter = unsafe { NulTerminated::new(s2) }.chain(once(&0));
let s1_iter = unsafe { NulTerminated::new(s1).unwrap() }.chain(once(&0));
let s2_iter = unsafe { NulTerminated::new(s2).unwrap() }.chain(once(&0));
let zipped = zip(s1_iter, s2_iter);
inner_casecmp(zipped)
@@ -77,8 +77,8 @@ pub unsafe extern "C" fn strcasecmp(s1: *const c_char, s2: *const c_char) -> c_i
#[no_mangle]
pub unsafe extern "C" fn strncasecmp(s1: *const c_char, s2: *const c_char, n: size_t) -> c_int {
// SAFETY: the caller must ensure that s1 and s2 point to nul-terminated buffers.
let s1_iter = unsafe { NulTerminated::new(s1) }.chain(once(&0));
let s2_iter = unsafe { NulTerminated::new(s2) }.chain(once(&0));
let s1_iter = unsafe { NulTerminated::new(s1).unwrap() }.chain(once(&0));
let s2_iter = unsafe { NulTerminated::new(s2).unwrap() }.chain(once(&0));
let zipped = zip(s1_iter, s2_iter).take(n);
inner_casecmp(zipped)
+1 -1
View File
@@ -505,7 +505,7 @@ pub extern "C" fn wcsftime(
#[no_mangle]
pub unsafe extern "C" fn wcslen(ws: *const wchar_t) -> size_t {
unsafe { NulTerminated::new(ws) }.count()
unsafe { NulTerminated::new(ws).unwrap() }.count()
}
#[no_mangle]
+4 -4
View File
@@ -73,12 +73,12 @@ impl<'a, T: Zero> NulTerminated<'a, T> {
/// elements of type `T`, and the value 0 must be present within the
/// buffer at or after `ptr` (not necessarily at the end). The buffer must
/// not be written to for the lifetime of the iterator.
pub unsafe fn new(ptr: *const T) -> Self {
NulTerminated {
pub unsafe fn new(ptr: *const T) -> Option<Self> {
Some(NulTerminated {
// NonNull can only wrap only *mut pointers...
ptr: NonNull::new(ptr.cast_mut()).unwrap(),
ptr: NonNull::new(ptr.cast_mut())?,
phantom: PhantomData,
}
})
}
}