]>
git.proxmox.com Git - cargo.git/blob - vendor/memchr/src/memchr/c.rs
1 // This module defines safe wrappers around memchr (POSIX) and memrchr (GNU
6 use libc
::{c_int, c_void, size_t}
;
8 pub fn memchr(needle
: u8, haystack
: &[u8]) -> Option
<usize> {
9 // SAFETY: This is safe to call since all pointers are valid.
12 haystack
.as_ptr() as *const c_void
,
14 haystack
.len() as size_t
,
20 Some(p
as usize - (haystack
.as_ptr() as usize))
24 // memrchr is a GNU extension. We know it's available on Linux at least.
25 #[cfg(target_os = "linux")]
26 pub fn memrchr(needle
: u8, haystack
: &[u8]) -> Option
<usize> {
27 // GNU's memrchr() will - unlike memchr() - error if haystack is empty.
28 if haystack
.is_empty() {
31 // SAFETY: This is safe to call since all pointers are valid.
34 haystack
.as_ptr() as *const c_void
,
36 haystack
.len() as size_t
,
42 Some(p
as usize - (haystack
.as_ptr() as usize))