Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions library/core/src/slice/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ const fn contains_zero_byte(x: usize) -> bool {
#[must_use]
pub const fn memchr(x: u8, text: &[u8]) -> Option<usize> {
// Fast path for small slices.
if text.len() < 2 * USIZE_BYTES {
return memchr_naive(x, text);
let result =
if text.len() < 2 * USIZE_BYTES { memchr_naive(x, text) } else { memchr_aligned(x, text) };
if let Some(index) = result {
// SAFETY: Both implementations only return an index from within `text`.
unsafe { crate::hint::assert_unchecked(index < text.len()) };
}

memchr_aligned(x, text)
result
}

#[inline]
Expand Down Expand Up @@ -107,8 +109,18 @@ const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> {
}

/// Returns the last index matching the byte `x` in `text`.
#[inline]
#[must_use]
pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
let result = memrchr_aligned(x, text);
if let Some(index) = result {
// SAFETY: `memrchr_aligned` only returns the index of a matching byte in `text`.
unsafe { crate::hint::assert_unchecked(index < text.len()) };
}
result
}

fn memrchr_aligned(x: u8, text: &[u8]) -> Option<usize> {
// Scan for a single byte value by reading two `usize` words at a time.
//
// Split `text` in three parts:
Expand Down
11 changes: 11 additions & 0 deletions library/coretests/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,17 @@ pub mod memchr {
assert_eq!(None, memchr(b'a', b"xyz"));
}

#[test]
fn each_alignment() {
let mut data = [1u8; 64];
let needle = 2;
let pos = 40;
data[pos] = needle;
for start in 0..16 {
assert_eq!(Some(pos - start), memchr(needle, &data[start..]));
}
}

#[test]
fn matches_one_reversed() {
assert_eq!(Some(0), memrchr(b'a', b"a"));
Expand Down
38 changes: 38 additions & 0 deletions tests/codegen-llvm/lib-optimizations/memchr-result.rs

@Darksonn Darksonn Aug 6, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should take this test out. It's too fragile.

View changes since the review

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Ensure `memchr` communicates that a returned index is in bounds.
//@ compile-flags: -Copt-level=3 -Zinline-mir=false
//@ only-x86_64
//@ revisions: llvm-old llvm-new
//@ [llvm-old] max-llvm-major-version: 21
//@ [llvm-new] min-llvm-version: 22

#![crate_type = "lib"]
#![feature(slice_internals)]

extern crate core;

use core::slice::memchr::{memchr, memrchr};

// CHECK-LABEL: @find_char
#[no_mangle]
pub fn find_char(haystack: &str, needle: char) -> Option<usize> {
// llvm-old: call void @llvm.assume
// llvm-new-NOT: phi { i64, i64 }
// CHECK: ret { i64, i64 }
haystack.find(needle)
}

// CHECK-LABEL: @find_byte
#[no_mangle]
pub fn find_byte(haystack: &[u8], needle: u8) -> Option<u8> {
// llvm-new-NOT: panic_bounds_check
// CHECK: ret { i1, i8 }
memchr(needle, haystack).map(|index| haystack[index])
}

// CHECK-LABEL: @rfind_byte
#[no_mangle]
pub fn rfind_byte(haystack: &[u8], needle: u8) -> Option<u8> {
// CHECK-NOT: panic_bounds_check
// CHECK: ret { i1, i8 }
memrchr(needle, haystack).map(|index| haystack[index])
}
Loading