const WORD_BYTES: usize = core::mem::size_of::<u64>();
const ONE_BYTES: u64 = u64::MAX / 255;
const HIGH_BYTES: u64 = ONE_BYTES << 7;
#[inline]
pub(crate) fn find_string_special(bytes: &[u8]) -> Option<usize> {
if bytes.len() < WORD_BYTES {
return scalar_scan(bytes);
}
let mut offset = 0;
let mut chunks = bytes.chunks_exact(WORD_BYTES);
for chunk in &mut chunks {
let word = u64::from_le_bytes(chunk.try_into().expect("u64-sized chunk"));
let contains_control = word.wrapping_sub(ONE_BYTES * 0x20) & !word;
let quote = word ^ (ONE_BYTES * u64::from(b'"'));
let contains_quote = quote.wrapping_sub(ONE_BYTES) & !quote;
let backslash = word ^ (ONE_BYTES * u64::from(b'\\'));
let contains_backslash = backslash.wrapping_sub(ONE_BYTES) & !backslash;
let special = (contains_control | contains_quote | contains_backslash) & HIGH_BYTES;
if special != 0 {
return Some(offset + special.trailing_zeros() as usize / 8);
}
offset += WORD_BYTES;
}
scalar_scan(chunks.remainder()).map(|relative| offset + relative)
}
#[inline]
fn scalar_scan(bytes: &[u8]) -> Option<usize> {
bytes
.iter()
.position(|&byte| byte < 0x20 || matches!(byte, b'"' | b'\\'))
}
#[cfg(test)]
mod tests {
use super::find_string_special;
fn scalar(bytes: &[u8]) -> Option<usize> {
bytes
.iter()
.position(|&byte| byte < 0x20 || matches!(byte, b'"' | b'\\'))
}
#[test]
fn it_agrees_with_the_scalar_scan_at_every_length_and_position() {
for length in 0..40_usize {
for position in 0..length {
for special in [0x00_u8, 0x01, 0x1f, b'"', b'\\'] {
let mut bytes = vec![b'a'; length];
bytes[position] = special;
assert_eq!(
find_string_special(&bytes),
scalar(&bytes),
"length {length}, position {position}, byte {special:#04x}"
);
}
}
assert_eq!(find_string_special(&vec![b'a'; length]), None);
}
}
#[test]
fn a_high_byte_is_not_special_and_does_not_disturb_its_neighbours() {
for high in [0x80_u8, 0xc3, 0xff] {
for position in 0..16_usize {
let mut bytes = vec![b'a'; 16];
bytes[position] = high;
assert_eq!(
find_string_special(&bytes),
None,
"{high:#04x} @ {position}"
);
let mut bytes = vec![b'a'; 16];
bytes[position] = high;
bytes[15] = b'"';
assert_eq!(find_string_special(&bytes), Some(15));
}
}
}
#[test]
fn every_byte_value_classifies_the_same_as_the_scalar_scan() {
for byte in 0..=u8::MAX {
let bytes = [byte];
assert_eq!(find_string_special(&bytes), scalar(&bytes), "{byte:#04x}");
}
}
}