]> git.proxmox.com Git - rustc.git/blame - library/core/src/slice/memchr.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / core / src / slice / memchr.rs
CommitLineData
9fa01778 1// Original implementation taken from rust-memchr.
ff7c6d11
XL
2// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
3
60c5eb7d
XL
4// ignore-tidy-undocumented-unsafe
5
48663c56
XL
6use crate::cmp;
7use crate::mem;
ff7c6d11
XL
8
9const LO_U64: u64 = 0x0101010101010101;
10const HI_U64: u64 = 0x8080808080808080;
11
9fa01778 12// Use truncation.
ff7c6d11
XL
13const LO_USIZE: usize = LO_U64 as usize;
14const HI_USIZE: usize = HI_U64 as usize;
1b1a35ee 15const USIZE_BYTES: usize = mem::size_of::<usize>();
ff7c6d11 16
9fa01778 17/// Returns `true` if `x` contains any zero byte.
ff7c6d11 18///
9fa01778 19/// From *Matters Computational*, J. Arndt:
ff7c6d11
XL
20///
21/// "The idea is to subtract one from each of the bytes and then look for
22/// bytes where the borrow propagated all the way to the most significant
23/// bit."
24#[inline]
25fn contains_zero_byte(x: usize) -> bool {
26 x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
27}
28
29#[cfg(target_pointer_width = "16")]
30#[inline]
31fn repeat_byte(b: u8) -> usize {
32 (b as usize) << 8 | b as usize
33}
34
83c7162d 35#[cfg(not(target_pointer_width = "16"))]
ff7c6d11
XL
36#[inline]
37fn repeat_byte(b: u8) -> usize {
f9f354fc 38 (b as usize) * (usize::MAX / 255)
ff7c6d11
XL
39}
40
9fa01778 41/// Returns the first index matching the byte `x` in `text`.
1b1a35ee 42#[inline]
ff7c6d11 43pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
1b1a35ee
XL
44 // Fast path for small slices
45 if text.len() < 2 * USIZE_BYTES {
46 return text.iter().position(|elt| *elt == x);
47 }
48
49 memchr_general_case(x, text)
50}
51
52fn memchr_general_case(x: u8, text: &[u8]) -> Option<usize> {
ff7c6d11
XL
53 // Scan for a single byte value by reading two `usize` words at a time.
54 //
55 // Split `text` in three parts
56 // - unaligned initial part, before the first word aligned address in text
57 // - body, scan by 2 words at a time
58 // - the last remaining part, < 2 word size
1b1a35ee
XL
59
60 // search up to an aligned boundary
ff7c6d11
XL
61 let len = text.len();
62 let ptr = text.as_ptr();
1b1a35ee 63 let mut offset = ptr.align_offset(USIZE_BYTES);
ff7c6d11 64
ff7c6d11
XL
65 if offset > 0 {
66 offset = cmp::min(offset, len);
67 if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
68 return Some(index);
69 }
70 }
71
72 // search the body of the text
73 let repeated_x = repeat_byte(x);
1b1a35ee
XL
74 while offset <= len - 2 * USIZE_BYTES {
75 unsafe {
76 let u = *(ptr.add(offset) as *const usize);
77 let v = *(ptr.add(offset + USIZE_BYTES) as *const usize);
78
79 // break if there is a matching byte
80 let zu = contains_zero_byte(u ^ repeated_x);
81 let zv = contains_zero_byte(v ^ repeated_x);
82 if zu || zv {
83 break;
ff7c6d11 84 }
ff7c6d11 85 }
1b1a35ee 86 offset += USIZE_BYTES * 2;
ff7c6d11
XL
87 }
88
9fa01778 89 // Find the byte after the point the body loop stopped.
ff7c6d11
XL
90 text[offset..].iter().position(|elt| *elt == x).map(|i| offset + i)
91}
92
9fa01778 93/// Returns the last index matching the byte `x` in `text`.
ff7c6d11
XL
94pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
95 // Scan for a single byte value by reading two `usize` words at a time.
96 //
9fa01778
XL
97 // Split `text` in three parts:
98 // - unaligned tail, after the last word aligned address in text,
99 // - body, scanned by 2 words at a time,
100 // - the first remaining bytes, < 2 word size.
ff7c6d11
XL
101 let len = text.len();
102 let ptr = text.as_ptr();
b7449926 103 type Chunk = usize;
ff7c6d11 104
b7449926
XL
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())
8faf50e0 110 };
b7449926
XL
111
112 let mut offset = max_aligned_offset;
8faf50e0
XL
113 if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
114 return Some(offset + index);
ff7c6d11
XL
115 }
116
9fa01778 117 // Search the body of the text, make sure we don't cross min_aligned_offset.
b7449926
XL
118 // offset is always aligned, so just testing `>` is sufficient and avoids possible
119 // overflow.
ff7c6d11 120 let repeated_x = repeat_byte(x);
b7449926 121 let chunk_bytes = mem::size_of::<Chunk>();
ff7c6d11 122
b7449926 123 while offset > min_aligned_offset {
ff7c6d11 124 unsafe {
b7449926
XL
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);
ff7c6d11 127
9fa01778 128 // Break if there is a matching byte.
ff7c6d11
XL
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 }
b7449926 135 offset -= 2 * chunk_bytes;
ff7c6d11
XL
136 }
137
9fa01778 138 // Find the byte before the point the body loop stopped.
ff7c6d11
XL
139 text[..offset].iter().rposition(|elt| *elt == x)
140}