]> git.lizzy.rs Git - rust.git/blob - src/libcore/slice/memchr.rs
Auto merge of #52712 - oli-obk:const_eval_cleanups, r=RalfJung
[rust.git] / src / libcore / slice / memchr.rs
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
14 use cmp;
15 use mem;
16
17 const LO_U64: u64 = 0x0101010101010101;
18 const HI_U64: u64 = 0x8080808080808080;
19
20 // use truncation
21 const LO_USIZE: usize = LO_U64 as usize;
22 const HI_USIZE: usize = HI_U64 as usize;
23
24 /// Return `true` if `x` contains any zero byte.
25 ///
26 /// From *Matters Computational*, J. Arndt
27 ///
28 /// "The idea is to subtract one from each of the bytes and then look for
29 /// bytes where the borrow propagated all the way to the most significant
30 /// bit."
31 #[inline]
32 fn contains_zero_byte(x: usize) -> bool {
33     x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
34 }
35
36 #[cfg(target_pointer_width = "16")]
37 #[inline]
38 fn repeat_byte(b: u8) -> usize {
39     (b as usize) << 8 | b as usize
40 }
41
42 #[cfg(not(target_pointer_width = "16"))]
43 #[inline]
44 fn repeat_byte(b: u8) -> usize {
45     (b as usize) * (::usize::MAX / 255)
46 }
47
48 /// Return the first index matching the byte `x` in `text`.
49 pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
50     // Scan for a single byte value by reading two `usize` words at a time.
51     //
52     // Split `text` in three parts
53     // - unaligned initial part, before the first word aligned address in text
54     // - body, scan by 2 words at a time
55     // - the last remaining part, < 2 word size
56     let len = text.len();
57     let ptr = text.as_ptr();
58     let usize_bytes = mem::size_of::<usize>();
59
60     // search up to an aligned boundary
61     let mut offset = ptr.align_offset(usize_bytes);
62     if offset > 0 {
63         offset = cmp::min(offset, len);
64         if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
65             return Some(index);
66         }
67     }
68
69     // search the body of the text
70     let repeated_x = repeat_byte(x);
71
72     if len >= 2 * usize_bytes {
73         while offset <= len - 2 * usize_bytes {
74             unsafe {
75                 let u = *(ptr.offset(offset as isize) as *const usize);
76                 let v = *(ptr.offset((offset + usize_bytes) as isize) as *const usize);
77
78                 // break if there is a matching byte
79                 let zu = contains_zero_byte(u ^ repeated_x);
80                 let zv = contains_zero_byte(v ^ repeated_x);
81                 if zu || zv {
82                     break;
83                 }
84             }
85             offset += usize_bytes * 2;
86         }
87     }
88
89     // find the byte after the point the body loop stopped
90     text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i)
91 }
92
93 /// Return the last index matching the byte `x` in `text`.
94 pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
95     // Scan for a single byte value by reading two `usize` words at a time.
96     //
97     // Split `text` in three parts
98     // - unaligned tail, after the last word aligned address in text
99     // - body, scan by 2 words at a time
100     // - the first remaining bytes, < 2 word size
101     let len = text.len();
102     let ptr = text.as_ptr();
103     type Chunk = usize;
104
105     let (min_aligned_offset, max_aligned_offset) = {
106         // We call this just to obtain the length of the prefix and suffix.
107         // In the middle we always process two chunks at once.
108         let (prefix, _, suffix) = unsafe { text.align_to::<(Chunk, Chunk)>() };
109         (prefix.len(), len - suffix.len())
110     };
111
112     let mut offset = max_aligned_offset;
113     if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
114         return Some(offset + index);
115     }
116
117     // search the body of the text, make sure we don't cross min_aligned_offset.
118     // offset is always aligned, so just testing `>` is sufficient and avoids possible
119     // overflow.
120     let repeated_x = repeat_byte(x);
121     let chunk_bytes = mem::size_of::<Chunk>();
122
123     while offset > min_aligned_offset {
124         unsafe {
125             let u = *(ptr.offset(offset as isize - 2 * chunk_bytes as isize) as *const Chunk);
126             let v = *(ptr.offset(offset as isize - chunk_bytes as isize) as *const Chunk);
127
128             // break if there is a matching byte
129             let zu = contains_zero_byte(u ^ repeated_x);
130             let zv = contains_zero_byte(v ^ repeated_x);
131             if zu || zv {
132                 break;
133             }
134         }
135         offset -= 2 * chunk_bytes;
136     }
137
138     // find the byte before the point the body loop stopped
139     text[..offset].iter().rposition(|elt| *elt == x)
140 }