1 // Original implementation taken from rust-memchr.
2 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
4 pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
7 haystack.as_ptr() as *const libc::c_void,
12 if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
15 pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
16 #[cfg(target_os = "linux")]
17 fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
18 // GNU's memrchr() will - unlike memchr() - error if haystack is empty.
19 if haystack.is_empty() {
24 haystack.as_ptr() as *const libc::c_void,
25 needle as libc::c_int,
29 // FIXME: this should *likely* use `offset_from`, but more
30 // investigation is needed (including running tests in miri).
31 if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) }
34 #[cfg(not(target_os = "linux"))]
35 fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
36 core::slice::memchr::memrchr(needle, haystack)
39 memrchr_specific(needle, haystack)