#[inline(always)]
pub(crate) fn broadcast(byte: u8) -> u64 {
byte as u64 * 0x0101_0101_0101_0101
}
#[inline(always)]
fn haszero(v: u64) -> bool {
v.wrapping_sub(0x0101_0101_0101_0101) & !v & 0x8080_8080_8080_8080 != 0
}
#[inline(always)]
pub(crate) fn hasbyte(word: u64, needle_mask: u64) -> bool {
haszero(word ^ needle_mask)
}
#[inline(always)]
pub(crate) fn hasless(word: u64, limit_mask: u64) -> bool {
word.wrapping_sub(limit_mask) & !word & 0x8080_8080_8080_8080 != 0
}
#[inline(always)]
pub(crate) fn has_non_ascii(word: u64) -> bool {
word & 0x8080_8080_8080_8080 != 0
}
#[inline(always)]
pub(crate) fn load_word(bytes: &[u8], i: usize) -> u64 {
let mut chunk = [0u8; 8];
chunk.copy_from_slice(&bytes[i..i + 8]);
u64::from_ne_bytes(chunk)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn detects_each_byte_value() {
let word = load_word(b"a\"b\\c\x01de", 0); assert!(hasbyte(word, broadcast(b'"')));
assert!(hasbyte(word, broadcast(b'\\')));
assert!(!hasbyte(word, broadcast(b'x')));
}
#[test]
fn detects_controls_and_non_ascii() {
let word = load_word(b"abcdefg\x01", 0);
assert!(hasless(word, broadcast(0x20)));
let word = load_word("日本語です!".as_bytes(), 0);
assert!(has_non_ascii(word));
let word = load_word(b"abcdefgh", 0);
assert!(!hasless(word, broadcast(0x20)));
assert!(!has_non_ascii(word));
assert!(!hasbyte(word, broadcast(b'"')));
}
#[test]
fn space_is_not_a_control() {
let word = load_word(b"abc defg", 0);
assert!(!hasless(word, broadcast(0x20)));
}
}