]>
git.proxmox.com Git - rustc.git/blob - library/std/src/sys_common/memchr.rs
1 // Original implementation taken from rust-memchr.
2 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
4 use crate::sys
::memchr
as sys
;
9 /// A safe interface to `memchr`.
11 /// Returns the index corresponding to the first occurrence of `needle` in
12 /// `haystack`, or `None` if one is not found.
14 /// memchr reduces to super-optimized machine code at around an order of
15 /// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
20 /// This shows how to find the first position of a byte in a byte string.
22 /// ```ignore (cannot-doctest-private-modules)
23 /// use memchr::memchr;
25 /// let haystack = b"the quick brown fox";
26 /// assert_eq!(memchr(b'k', haystack), Some(8));
29 pub fn memchr(needle
: u8, haystack
: &[u8]) -> Option
<usize> {
30 sys
::memchr(needle
, haystack
)
33 /// A safe interface to `memrchr`.
35 /// Returns the index corresponding to the last occurrence of `needle` in
36 /// `haystack`, or `None` if one is not found.
40 /// This shows how to find the last position of a byte in a byte string.
42 /// ```ignore (cannot-doctest-private-modules)
43 /// use memchr::memrchr;
45 /// let haystack = b"the quick brown fox";
46 /// assert_eq!(memrchr(b'o', haystack), Some(17));
49 pub fn memrchr(needle
: u8, haystack
: &[u8]) -> Option
<usize> {
50 sys
::memrchr(needle
, haystack
)