const LO: u64 = 0x0101_0101_0101_0101;
const HI: u64 = 0x8080_8080_8080_8080;
const STEP: usize = 8;
#[inline]
const fn ge(w: u64, n: u8) -> u64 {
let high = w & HI;
let low = w & !HI;
let diff = (low | HI).wrapping_sub(LO.wrapping_mul(n as u64));
(diff & HI) | high
}
#[inline]
const fn ne(w: u64, n: u8) -> u64 {
(!ge(w, n) & HI) | ge(w, n + 1)
}
#[inline]
const fn eq_or_just_above(w: u64, n: u8) -> u64 {
let x = w ^ LO.wrapping_mul(n as u64);
x.wrapping_sub(LO) & !x & HI
}
#[inline]
const fn lowest(mask: u64) -> Option<usize> {
if mask == 0 { None } else { Some(mask.trailing_zeros() as usize / STEP) }
}
#[inline]
fn word(bytes: &[u8], at: usize) -> Option<u64> {
let chunk: [u8; STEP] = bytes.get(at..at + STEP)?.try_into().ok()?;
Some(u64::from_le_bytes(chunk))
}
pub(crate) fn first_of(bytes: &[u8], at: usize, needles: &[u8]) -> usize {
let mut i = at;
while let Some(w) = word(bytes, i) {
let mut found = 0;
for &n in needles {
found |= eq_or_just_above(w, n);
}
if let Some(k) = lowest(found) {
return i + k;
}
i += STEP;
}
while i < bytes.len() {
if needles.contains(&bytes[i]) {
return i;
}
i += 1;
}
bytes.len()
}
pub(crate) fn run_of_blanks(bytes: &[u8], at: usize) -> usize {
let mut i = at;
while let Some(w) = word(bytes, i) {
let neither = ne(w, b' ') & ne(w, b'\t');
if let Some(k) = lowest(neither) {
return i + k;
}
i += STEP;
}
while i < bytes.len() && matches!(bytes[i], b' ' | b'\t') {
i += 1;
}
i
}
#[cfg(test)]
mod tests {
use super::*;
fn blanks_scalar(bytes: &[u8], at: usize) -> usize {
let mut i = at;
while i < bytes.len() && matches!(bytes[i], b' ' | b'\t') {
i += 1;
}
i
}
#[test]
fn a_run_of_blanks_ends_where_the_obvious_loop_says_it_does() {
for run in 0..24 {
for after in 0u16..=255 {
let after = after as u8;
if matches!(after, b' ' | b'\t') {
continue;
}
let mut input = vec![b' '; run];
input.push(after);
input.extend_from_slice(b"tail tail tail");
assert_eq!(
run_of_blanks(&input, 0),
blanks_scalar(&input, 0),
"run of {run} then {after:#04x}"
);
}
}
}
#[test]
fn a_byte_one_above_a_space_is_not_a_space() {
assert_eq!(run_of_blanks(b" !x", 0), 8);
assert_eq!(run_of_blanks(b" !", 0), 1);
assert_eq!(run_of_blanks(b"\t\t\t\t\t\t\t\t\nx", 0), 8);
}
#[test]
fn tabs_and_spaces_mix_freely() {
assert_eq!(run_of_blanks(b" \t \t \t \t \t x", 0), 11);
assert_eq!(run_of_blanks(b"x", 0), 0);
assert_eq!(run_of_blanks(b"", 0), 0);
assert_eq!(run_of_blanks(b"abc def", 3), 6);
}
#[test]
fn first_of_never_runs_past_a_needle() {
for len in 0..40 {
for at in [0usize, 1, 7, 8, 9] {
if at > len {
continue;
}
let mut input = vec![b'x'; len];
for planted in 0..len {
input[planted] = b'*';
let got = first_of(&input, at, b"*\n");
let want = (at..len).find(|&i| input[i] == b'*').unwrap_or(len);
assert!(got <= want, "len {len} at {at} planted {planted}: {got} > {want}");
assert!(got <= len);
input[planted] = b'x';
}
assert_eq!(first_of(&input, at, b"*"), len, "no needle, len {len} at {at}");
}
}
}
#[test]
fn first_of_finds_a_needle_in_the_tail_past_the_last_whole_word() {
assert_eq!(first_of(b"xxxxxxxx*", 0, b"*"), 8);
assert_eq!(first_of(b"xxxxxxx*x", 0, b"*"), 7);
assert_eq!(first_of(b"*xxxxxxxx", 0, b"*"), 0);
}
#[test]
fn the_comparison_masks_agree_with_arithmetic_on_every_byte() {
for b in 0u16..=255 {
let b = b as u8;
let w = u64::from_le_bytes([b; STEP]);
for n in [1u8, 9, 0x20, 0x2A, 0x5C, 0x7F, 0x80] {
let want = if b >= n { HI } else { 0 };
assert_eq!(ge(w, n), want, "byte {b:#04x} against {n:#04x}");
}
assert_eq!(ne(w, b' '), if b == b' ' { 0 } else { HI }, "byte {b:#04x}");
}
}
}