1 // Original implementation taken from rust-memchr.
2 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
7 /// A safe interface to `memchr`.
9 /// Returns the index corresponding to the first occurrence of `needle` in
10 /// `haystack`, or `None` if one is not found.
12 /// memchr reduces to super-optimized machine code at around an order of
13 /// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
18 /// This shows how to find the first position of a byte in a byte string.
20 /// ```ignore (cannot-doctest-private-modules)
21 /// use memchr::memchr;
23 /// let haystack = b"the quick brown fox";
24 /// assert_eq!(memchr(b'k', haystack), Some(8));
27 pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
28 crate::sys::memchr::memchr(needle, haystack)
31 /// A safe interface to `memrchr`.
33 /// Returns the index corresponding to the last occurrence of `needle` in
34 /// `haystack`, or `None` if one is not found.
38 /// This shows how to find the last position of a byte in a byte string.
40 /// ```ignore (cannot-doctest-private-modules)
41 /// use memchr::memrchr;
43 /// let haystack = b"the quick brown fox";
44 /// assert_eq!(memrchr(b'o', haystack), Some(17));
47 pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
48 crate::sys::memchr::memrchr(needle, haystack)