Skip to content

Commit 5a3e272

Browse files
Slightly optimized nstd_core_cstr_is_null_terminated.
1 parent 6c651d3 commit 5a3e272

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/core/cstr.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
mem::nstd_core_mem_search,
77
slice::{nstd_core_slice_new, NSTDSlice},
88
},
9-
NSTDBool, NSTDChar, NSTDUInt, NSTD_FALSE,
9+
NSTDBool, NSTDChar, NSTDUInt,
1010
};
1111

1212
/// An immutable slice of a C string.
@@ -232,17 +232,11 @@ pub extern "C" fn nstd_core_cstr_len(cstr: &NSTDCStr) -> NSTDUInt {
232232
/// assert!(nstd_core_cstr_is_null_terminated(&mn_cstr) == NSTD_FALSE);
233233
/// }
234234
/// ```
235+
#[inline]
235236
#[cfg_attr(feature = "clib", no_mangle)]
236237
pub unsafe extern "C" fn nstd_core_cstr_is_null_terminated(cstr: &NSTDCStr) -> NSTDBool {
237238
assert!(cstr.len <= isize::MAX as usize);
238-
let mut i = 0;
239-
while i < cstr.len {
240-
if *cstr.ptr.add(i) == 0 {
241-
return i == cstr.len - 1;
242-
}
243-
i += 1;
244-
}
245-
NSTD_FALSE
239+
nstd_core_mem_search(cstr.ptr.cast(), cstr.len, 0) == nstd_core_cstr_last(cstr).cast()
246240
}
247241

248242
/// Returns a pointer to the first null byte in a C string slice if one is present.

0 commit comments

Comments
 (0)