1 // Original implementation taken from rust-memchr.
2 // Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
7 const LO_U64: u64 = 0x0101010101010101;
8 const HI_U64: u64 = 0x8080808080808080;
11 const LO_USIZE: usize = LO_U64 as usize;
12 const HI_USIZE: usize = HI_U64 as usize;
14 /// Returns whether `x` contains any zero byte.
16 /// From *Matters Computational*, J. Arndt:
18 /// "The idea is to subtract one from each of the bytes and then look for
19 /// bytes where the borrow propagated all the way to the most significant
22 fn contains_zero_byte(x: usize) -> bool {
23 x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
26 #[cfg(target_pointer_width = "16")]
28 fn repeat_byte(b: u8) -> usize {
29 (b as usize) << 8 | b as usize
32 #[cfg(not(target_pointer_width = "16"))]
34 fn repeat_byte(b: u8) -> usize {
35 (b as usize) * (::usize::MAX / 255)
38 /// Returns the first index matching the byte `x` in `text`.
39 pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
40 // Scan for a single byte value by reading two `usize` words at a time.
42 // Split `text` in three parts
43 // - unaligned initial part, before the first word aligned address in text
44 // - body, scan by 2 words at a time
45 // - the last remaining part, < 2 word size
47 let ptr = text.as_ptr();
48 let usize_bytes = mem::size_of::<usize>();
50 // search up to an aligned boundary
51 let mut offset = ptr.align_offset(usize_bytes);
53 offset = cmp::min(offset, len);
54 if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
59 // search the body of the text
60 let repeated_x = repeat_byte(x);
62 if len >= 2 * usize_bytes {
63 while offset <= len - 2 * usize_bytes {
65 let u = *(ptr.add(offset) as *const usize);
66 let v = *(ptr.add(offset + usize_bytes) as *const usize);
68 // break if there is a matching byte
69 let zu = contains_zero_byte(u ^ repeated_x);
70 let zv = contains_zero_byte(v ^ repeated_x);
75 offset += usize_bytes * 2;
79 // Find the byte after the point the body loop stopped.
80 text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i)
83 /// Returns the last index matching the byte `x` in `text`.
84 pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
85 // Scan for a single byte value by reading two `usize` words at a time.
87 // Split `text` in three parts:
88 // - unaligned tail, after the last word aligned address in text,
89 // - body, scanned by 2 words at a time,
90 // - the first remaining bytes, < 2 word size.
92 let ptr = text.as_ptr();
95 let (min_aligned_offset, max_aligned_offset) = {
96 // We call this just to obtain the length of the prefix and suffix.
97 // In the middle we always process two chunks at once.
98 let (prefix, _, suffix) = unsafe { text.align_to::<(Chunk, Chunk)>() };
99 (prefix.len(), len - suffix.len())
102 let mut offset = max_aligned_offset;
103 if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
104 return Some(offset + index);
107 // Search the body of the text, make sure we don't cross min_aligned_offset.
108 // offset is always aligned, so just testing `>` is sufficient and avoids possible
110 let repeated_x = repeat_byte(x);
111 let chunk_bytes = mem::size_of::<Chunk>();
113 while offset > min_aligned_offset {
115 let u = *(ptr.offset(offset as isize - 2 * chunk_bytes as isize) as *const Chunk);
116 let v = *(ptr.offset(offset as isize - chunk_bytes as isize) as *const Chunk);
118 // Break if there is a matching byte.
119 let zu = contains_zero_byte(u ^ repeated_x);
120 let zv = contains_zero_byte(v ^ repeated_x);
125 offset -= 2 * chunk_bytes;
128 // Find the byte before the point the body loop stopped.
129 text[..offset].iter().rposition(|elt| *elt == x)