]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unix/memchr.rs
New upstream version 1.59.0+dfsg1
[rustc.git] / library / std / src / sys / unix / memchr.rs
1 // Original implementation taken from rust-memchr.
2 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
3
4 pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
5 let p = unsafe {
6 libc::memchr(
7 haystack.as_ptr() as *const libc::c_void,
8 needle as libc::c_int,
9 haystack.len(),
10 )
11 };
12 if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) }
13 }
14
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() {
20 return None;
21 }
22 let p = unsafe {
23 libc::memrchr(
24 haystack.as_ptr() as *const libc::c_void,
25 needle as libc::c_int,
26 haystack.len(),
27 )
28 };
29 if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) }
30 }
31
32 #[cfg(not(target_os = "linux"))]
33 fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
34 core::slice::memchr::memrchr(needle, haystack)
35 }
36
37 memrchr_specific(needle, haystack)
38 }