Commit d62db7b1 authored by Mateusz Mikuła's avatar Mateusz Mikuła Committed by Mateusz Mikuła

Add memrchr()

parent 15aa52a8
......@@ -107,6 +107,20 @@ pub unsafe extern "C" fn memmove(s1: *mut c_void, s2: *const c_void, n: size_t)
s1
}
#[no_mangle]
pub unsafe extern "C" fn memrchr(
haystack: *const c_void,
needle: c_int,
len: size_t,
) -> *mut c_void {
let haystack = slice::from_raw_parts(haystack as *const u8, len as usize);
match memchr::memrchr(needle as u8, haystack) {
Some(index) => haystack[index..].as_ptr() as *mut c_void,
None => ptr::null_mut(),
}
}
#[no_mangle]
pub unsafe extern "C" fn memset(s: *mut c_void, c: c_int, n: size_t) -> *mut c_void {
for i in 0..n {
......
# mem #
Correct memchr
Correct memrchr
Correct memccpy
Correct memcmp
......@@ -14,6 +14,11 @@ int main(void) {
exit(EXIT_FAILURE);
}
puts("Correct memchr");
if ((size_t)memrchr((void *)arr, 1, 100) - (size_t)arr != 50) {
puts("Incorrect memrchr");
exit(EXIT_FAILURE);
}
puts("Correct memrchr");
char arr2[51];
memset(arr2, 0, 51); // Compiler builtin, should work
memccpy((void *)arr2, (void *)arr, 1, 100);
......
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