use std::sync::OnceLock;
static HAS_AVX2: OnceLock<bool> = OnceLock::new();
#[inline]
pub fn avx2() -> bool {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
*HAS_AVX2.get_or_init(|| is_x86_feature_detected!("avx2"))
}
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
{
false
}
}
#[inline]
pub fn find(hay: &[u8], from: usize, b: u8) -> Option<usize> {
if from < hay.len() && hay[from] == b {
return Some(from);
}
memchr::memchr(b, &hay[from..]).map(|p| from + p)
}
#[inline]
pub fn find2(hay: &[u8], from: usize, a: u8, b: u8) -> Option<(usize, u8)> {
if from < hay.len() {
if hay[from] == a {
return Some((from, a));
}
if hay[from] == b {
return Some((from, b));
}
}
memchr::memchr2(a, b, &hay[from..]).map(|p| (from + p, hay[from + p]))
}
#[inline]
pub fn starts_with<const N: usize>(hay: &[u8], at: usize, lit: &[u8; N]) -> bool {
at + N <= hay.len() && hay[at..at + N] == *lit
}
#[inline]
pub fn find_literal(hay: &[u8], at: usize, finder: &memchr::memmem::Finder) -> Option<usize> {
finder.find(&hay[at..]).map(|p| at + p)
}
#[inline]
pub unsafe fn utf8_after_chunk_validation(b: &[u8]) -> &str {
debug_assert!(
simdutf8::basic::from_utf8(b).is_ok(),
"utf8_after_chunk_validation: bytes failed SIMD validation"
);
std::str::from_utf8_unchecked(b)
}
#[inline]
pub fn find_delimiter_or_quote(
hay: &[u8],
from: usize,
delimiter: u8,
quote: u8,
) -> Option<(usize, u8)> {
find2(hay, from, delimiter, quote)
}
pub fn scan_delimited_field(bytes: &[u8], start: usize, delimiter: u8, quote: u8) -> usize {
if start >= bytes.len() {
return bytes.len();
}
match find2(bytes, start, delimiter, quote) {
Some((rel, b)) if b == quote => {
let mut pos = rel + 1;
while let Some(q_rel) = memchr::memchr(quote, &bytes[pos..]) {
if pos + q_rel + 1 < bytes.len() && bytes[pos + q_rel + 1] == quote {
pos += q_rel + 2; } else {
return pos + q_rel + 1; }
}
bytes.len() }
Some((rel, _)) => rel + 1, None => bytes.len(), }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_find() {
let data = b"hello world";
assert_eq!(find(data, 0, b'w'), Some(6));
assert_eq!(find(data, 6, b'w'), Some(6));
assert_eq!(find(data, 7, b'w'), None);
assert_eq!(find(data, 0, b'x'), None);
}
#[test]
fn test_find_fast_path() {
let data = b"xxxxx";
assert_eq!(find(data, 3, b'x'), Some(3));
}
#[test]
fn test_find2() {
let data = b"a,b,c";
assert_eq!(find2(data, 0, b'a', b'b'), Some((0, b'a')));
assert_eq!(find2(data, 0, b'b', b'a'), Some((0, b'a')));
assert_eq!(find2(data, 1, b'b', b'c'), Some((2, b'b')));
}
#[test]
fn test_starts_with() {
let data = b"<Field>";
assert!(starts_with(data, 0, b"<Field>"));
assert!(!starts_with(data, 0, b"<Text>"));
assert!(!starts_with(data, 10, b"<Fiel"));
}
#[test]
fn test_find_literal() {
let finder = memchr::memmem::Finder::new(b"</Field>");
let data = b"<Field><Value>1</Value></Field>";
assert_eq!(find_literal(data, 0, &finder), Some(23));
}
#[test]
fn test_utf8_after_chunk_validation() {
let valid = "hello world";
unsafe {
assert_eq!(utf8_after_chunk_validation(valid.as_bytes()), valid);
}
}
}