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