Commit 5681855a authored by Matija Skala's avatar Matija Skala

strchr, strlen: use optimized memchr

parent 4f2a93ea
Pipeline #5998 failed with stages
in 17 minutes and 15 seconds
......@@ -130,15 +130,16 @@ pub unsafe extern "C" fn memset(s: *mut c_void, c: c_int, n: size_t) -> *mut c_v
}
#[no_mangle]
pub unsafe extern "C" fn strchr(mut s: *const c_char, c: c_int) -> *mut c_char {
let c = c as c_char;
while *s != 0 {
if *s == c {
return s as *mut c_char;
}
s = s.offset(1);
pub unsafe extern "C" fn strchr(s: *const c_char, c: c_int) -> *mut c_char {
let haystack = slice::from_raw_parts(s as *const u8, usize::MAX);
match memchr::memchr2(c as u8, 0u8, haystack) {
Some(index) => match haystack[index] != 0 {
true => haystack[index..].as_ptr() as *mut c_char,
false => ptr::null_mut(),
},
None => ptr::null_mut(),
}
ptr::null_mut()
}
#[no_mangle]
......@@ -267,14 +268,12 @@ pub unsafe extern "C" fn strlen(s: *const c_char) -> size_t {
#[no_mangle]
pub unsafe extern "C" fn strnlen(s: *const c_char, size: size_t) -> size_t {
let mut i = 0;
while i < size {
if *s.add(i) == 0 {
break;
}
i += 1;
let haystack = slice::from_raw_parts(s as *const u8, size as usize);
match memchr::memchr(0u8, haystack) {
Some(index) => index as size_t,
None => size,
}
i as size_t
}
#[no_mangle]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment