]> git.proxmox.com Git - rustc.git/blame - src/libstd/memchr.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / libstd / memchr.rs
CommitLineData
9cc50fc6
SL
1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10//
11// Original implementation taken from rust-memchr
12// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
13
9cc50fc6
SL
14/// A safe interface to `memchr`.
15///
16/// Returns the index corresponding to the first occurrence of `needle` in
17/// `haystack`, or `None` if one is not found.
18///
19/// memchr reduces to super-optimized machine code at around an order of
20/// magnitude faster than `haystack.iter().position(|&b| b == needle)`.
21/// (See benchmarks.)
22///
23/// # Example
24///
25/// This shows how to find the first position of a byte in a byte string.
26///
27/// ```rust,ignore
28/// use memchr::memchr;
29///
30/// let haystack = b"the quick brown fox";
31/// assert_eq!(memchr(b'k', haystack), Some(8));
32/// ```
c30ab7b3 33#[inline]
9cc50fc6 34pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
c30ab7b3 35 ::sys::memchr::memchr(needle, haystack)
9cc50fc6
SL
36}
37
38/// A safe interface to `memrchr`.
39///
40/// Returns the index corresponding to the last occurrence of `needle` in
41/// `haystack`, or `None` if one is not found.
42///
43/// # Example
44///
45/// This shows how to find the last position of a byte in a byte string.
46///
47/// ```rust,ignore
48/// use memchr::memrchr;
49///
50/// let haystack = b"the quick brown fox";
51/// assert_eq!(memrchr(b'o', haystack), Some(17));
52/// ```
c30ab7b3 53#[inline]
9cc50fc6 54pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
c30ab7b3 55 ::sys::memchr::memrchr(needle, haystack)
9cc50fc6
SL
56}
57
58#[cfg(test)]
59mod tests {
60 // test the implementations for the current plattform
61 use super::{memchr, memrchr};
62
63 #[test]
64 fn matches_one() {
65 assert_eq!(Some(0), memchr(b'a', b"a"));
66 }
67
68 #[test]
69 fn matches_begin() {
70 assert_eq!(Some(0), memchr(b'a', b"aaaa"));
71 }
72
73 #[test]
74 fn matches_end() {
75 assert_eq!(Some(4), memchr(b'z', b"aaaaz"));
76 }
77
78 #[test]
79 fn matches_nul() {
80 assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00"));
81 }
82
83 #[test]
84 fn matches_past_nul() {
85 assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z"));
86 }
87
88 #[test]
89 fn no_match_empty() {
90 assert_eq!(None, memchr(b'a', b""));
91 }
92
93 #[test]
94 fn no_match() {
95 assert_eq!(None, memchr(b'a', b"xyz"));
96 }
97
98 #[test]
99 fn matches_one_reversed() {
100 assert_eq!(Some(0), memrchr(b'a', b"a"));
101 }
102
103 #[test]
104 fn matches_begin_reversed() {
105 assert_eq!(Some(3), memrchr(b'a', b"aaaa"));
106 }
107
108 #[test]
109 fn matches_end_reversed() {
110 assert_eq!(Some(0), memrchr(b'z', b"zaaaa"));
111 }
112
113 #[test]
114 fn matches_nul_reversed() {
115 assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00"));
116 }
117
118 #[test]
119 fn matches_past_nul_reversed() {
120 assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa"));
121 }
122
123 #[test]
124 fn no_match_empty_reversed() {
125 assert_eq!(None, memrchr(b'a', b""));
126 }
127
128 #[test]
129 fn no_match_reversed() {
130 assert_eq!(None, memrchr(b'a', b"xyz"));
131 }
5bcae85e
SL
132
133 #[test]
134 fn each_alignment() {
135 let mut data = [1u8; 64];
136 let needle = 2;
137 let pos = 40;
138 data[pos] = needle;
139 for start in 0..16 {
140 assert_eq!(Some(pos - start), memchr(needle, &data[start..]));
141 }
142 }
9cc50fc6 143}