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 as usize - (haystack.as_ptr() as usize)) }
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 if p
.is_null() { None }
else { Some(p as usize - (haystack.as_ptr() as usize)) }
32 #[cfg(not(target_os = "linux"))]
33 fn memrchr_specific(needle
: u8, haystack
: &[u8]) -> Option
<usize> {
34 core
::slice
::memchr
::memrchr(needle
, haystack
)
37 memrchr_specific(needle
, haystack
)