Skip to main content

match_prefix_len

Function match_prefix_len 

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

Length of the leading run of bytes in haystack that ARE in needles. The polarity-flipped sibling of clean_prefix_len:

  • clean_prefix_len = skip while the byte is not a needle (consume the clean run; stop at the first interesting byte).
  • match_prefix_len = skip while the byte is a needle (consume the matched run; stop at the first content byte).

match_prefix_len(rem, b" \t") is the canonical “advance past blanks” primitive; see Scanner::skip_blank.

LLVM at opt-level=3 + lto=fat auto-vectorises the per-byte loop into 16/32-byte block scans, so the explicit SIMD path is not required for short runs (the typical 0-8 byte indent). Larger-arity needle sets fall through the same bitmap a scalar code would build for itself; the cost is a one-time bitmap_for call.

§Examples

use noyalib::simd::match_prefix_len;

// Three leading blanks, then `f`.
assert_eq!(match_prefix_len(b"   foo", b" \t"), 3);

// No leading match — empty prefix.
assert_eq!(match_prefix_len(b"foo", b" \t"), 0);

// Whole input matches.
assert_eq!(match_prefix_len(b"\t \t ", b" \t"), 4);

// Empty needle set — no byte ever matches, returns 0.
assert_eq!(match_prefix_len(b"abc", b""), 0);