Skip to content

Commit abe34e9

Browse files
committed
Auto merge of rust-lang#118315 - WaffleLapkin:don't-repeat_byte, r=m-ou-se
Use `usize::repeat_u8` instead of implementing `repeat_byte` in `memchr.rs` It's simpler that way and the tricks don't actually make a difference: https://godbolt.org/z/zrvYY1dGx
2 parents ce4727f + 234e950 commit abe34e9

File tree

1 file changed

+2
-16
lines changed

1 file changed

+2
-16
lines changed

library/core/src/slice/memchr.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,6 @@ const fn contains_zero_byte(x: usize) -> bool {
2020
x.wrapping_sub(LO_USIZE) & !x & HI_USIZE != 0
2121
}
2222

23-
#[inline]
24-
#[cfg(target_pointer_width = "16")]
25-
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
26-
const fn repeat_byte(b: u8) -> usize {
27-
(b as usize) << 8 | b as usize
28-
}
29-
30-
#[inline]
31-
#[cfg(not(target_pointer_width = "16"))]
32-
#[rustc_const_stable(feature = "const_memchr", since = "1.65.0")]
33-
const fn repeat_byte(b: u8) -> usize {
34-
(b as usize) * (usize::MAX / 255)
35-
}
36-
3723
/// Returns the first index matching the byte `x` in `text`.
3824
#[inline]
3925
#[must_use]
@@ -93,7 +79,7 @@ const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> {
9379
}
9480

9581
// search the body of the text
96-
let repeated_x = repeat_byte(x);
82+
let repeated_x = usize::repeat_u8(x);
9783
while offset <= len - 2 * USIZE_BYTES {
9884
// SAFETY: the while's predicate guarantees a distance of at least 2 * usize_bytes
9985
// between the offset and the end of the slice.
@@ -149,7 +135,7 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
149135
// Search the body of the text, make sure we don't cross min_aligned_offset.
150136
// offset is always aligned, so just testing `>` is sufficient and avoids possible
151137
// overflow.
152-
let repeated_x = repeat_byte(x);
138+
let repeated_x = usize::repeat_u8(x);
153139
let chunk_bytes = mem::size_of::<Chunk>();
154140

155141
while offset > min_aligned_offset {

0 commit comments

Comments
 (0)