Skip to main content

find_any_of

Function find_any_of 

Source
pub fn find_any_of(haystack: &[u8], needles: &[u8]) -> Option<usize>
Expand description

Find the byte offset of the first occurrence of any byte in needles within haystack.

Returns None when no needle byte is present.

Performance:

  • 0 needles: returns None in O(1).
  • 1, 2, 3 needles: routes through [memchr] for SSE2 / NEON acceleration where the platform supports it.
  • 4+ needles: uses SWAR (8-byte-stride packed comparison).
  • The scalar tail handles the last < 8 bytes byte-by-byte.

All paths produce identical results — exhaustively tested by the equivalence suite in tests/simd_equivalence.rs.

§Examples

use noyalib::simd::find_any_of;

// 1-needle path (memchr).
assert_eq!(find_any_of(b"abc:def", &[b':']), Some(3));

// 2-needle path (memchr2).
assert_eq!(find_any_of(b"abc\ndef", &[b':', b'\n']), Some(3));

// 3-needle path (memchr3).
assert_eq!(find_any_of(b"abc#def", &[b':', b'\n', b'#']), Some(3));

// 4+ needle path (SWAR).
assert_eq!(
    find_any_of(b"name=value", &[b':', b',', b'=', b'\n']),
    Some(4),
);