]> git.lizzy.rs Git - rust.git/blob - src/libstd/memchr.rs
Rollup merge of #57042 - pnkfelix:issue-57038-sidestep-ice-in-fieldplacement-count...
[rust.git] / src / libstd / memchr.rs
1 //
2 // Original implementation taken from rust-memchr
3 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
4
5 /// A safe interface to `memchr`.
6 ///
7 /// Returns the index corresponding to the first occurrence of `needle` in
8 /// `haystack`, or `None` if one is not found.
9 ///
10 /// memchr reduces to super-optimized machine code at around an order of
11 /// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
12 /// (See benchmarks.)
13 ///
14 /// # Examples
15 ///
16 /// This shows how to find the first position of a byte in a byte string.
17 ///
18 /// ```ignore (cannot-doctest-private-modules)
19 /// use memchr::memchr;
20 ///
21 /// let haystack = b"the quick brown fox";
22 /// assert_eq!(memchr(b'k', haystack), Some(8));
23 /// ```
24 #[inline]
25 pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
26     ::sys::memchr::memchr(needle, haystack)
27 }
28
29 /// A safe interface to `memrchr`.
30 ///
31 /// Returns the index corresponding to the last occurrence of `needle` in
32 /// `haystack`, or `None` if one is not found.
33 ///
34 /// # Examples
35 ///
36 /// This shows how to find the last position of a byte in a byte string.
37 ///
38 /// ```ignore (cannot-doctest-private-modules)
39 /// use memchr::memrchr;
40 ///
41 /// let haystack = b"the quick brown fox";
42 /// assert_eq!(memrchr(b'o', haystack), Some(17));
43 /// ```
44 #[inline]
45 pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
46     ::sys::memchr::memrchr(needle, haystack)
47 }
48
49 #[cfg(test)]
50 mod tests {
51     // test the implementations for the current platform
52     use super::{memchr, memrchr};
53
54     #[test]
55     fn matches_one() {
56         assert_eq!(Some(0), memchr(b'a', b"a"));
57     }
58
59     #[test]
60     fn matches_begin() {
61         assert_eq!(Some(0), memchr(b'a', b"aaaa"));
62     }
63
64     #[test]
65     fn matches_end() {
66         assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
67     }
68
69     #[test]
70     fn matches_nul() {
71         assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
72     }
73
74     #[test]
75     fn matches_past_nul() {
76         assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
77     }
78
79     #[test]
80     fn no_match_empty() {
81         assert_eq!(None, memchr(b'a', b""));
82     }
83
84     #[test]
85     fn no_match() {
86         assert_eq!(None, memchr(b'a', b"xyz"));
87     }
88
89     #[test]
90     fn matches_one_reversed() {
91         assert_eq!(Some(0), memrchr(b'a', b"a"));
92     }
93
94     #[test]
95     fn matches_begin_reversed() {
96         assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
97     }
98
99     #[test]
100     fn matches_end_reversed() {
101         assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
102     }
103
104     #[test]
105     fn matches_nul_reversed() {
106         assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
107     }
108
109     #[test]
110     fn matches_past_nul_reversed() {
111         assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
112     }
113
114     #[test]
115     fn no_match_empty_reversed() {
116         assert_eq!(None, memrchr(b'a', b""));
117     }
118
119     #[test]
120     fn no_match_reversed() {
121         assert_eq!(None, memrchr(b'a', b"xyz"));
122     }
123
124     #[test]
125     fn each_alignment() {
126         let mut data = [1u8; 64];
127         let needle = 2;
128         let pos = 40;
129         data[pos] = needle;
130         for start in 0..16 {
131             assert_eq!(Some(pos - start), memchr(needle, &data[start..]));
132         }
133     }
134 }