use memchr::{memchr, memchr2};
pub(crate) fn find_ci(haystack: &[u8], needle: &[u8], from: usize) -> Option<usize> {
let (&first, rest) = needle.split_first()?;
let last = haystack.len().checked_sub(needle.len())?;
let (lower, upper) = (first.to_ascii_lowercase(), first.to_ascii_uppercase());
let mut at = from;
while at <= last {
let window = &haystack[at..=last];
let found = if lower == upper { memchr(lower, window) } else { memchr2(lower, upper, window) };
at += found?;
if haystack[at + 1..at + needle.len()].eq_ignore_ascii_case(rest) {
return Some(at);
}
at += 1;
}
None
}
pub(crate) fn contains_ci(haystack: &[u8], needle: &str) -> bool {
find_ci(haystack, needle.as_bytes(), 0).is_some()
}
pub(crate) fn contains_any_ci(haystack: &[u8], needles: &[&str]) -> bool {
needles.iter().any(|needle| contains_ci(haystack, needle))
}
pub(crate) fn starts_with_ci(haystack: &[u8], at: usize, needle: &str) -> bool {
haystack.get(at..at + needle.len()).is_some_and(|s| s.eq_ignore_ascii_case(needle.as_bytes()))
}
pub(crate) fn is_word(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'_'
}
fn word_start(haystack: &[u8], at: usize) -> bool {
at == 0 || !is_word(haystack[at - 1])
}
pub(crate) fn word_end(haystack: &[u8], end: usize) -> bool {
haystack.get(end).is_none_or(|&b| !is_word(b))
}
pub(crate) fn dotted(b: u8) -> bool {
b.is_ascii_digit() || b == b'.'
}
pub(crate) fn dotted_underscored(b: u8) -> bool {
dotted(b) || b == b'_'
}
pub(crate) fn underscored(b: u8) -> bool {
b.is_ascii_digit() || b == b'_'
}
pub(crate) fn digit(b: u8) -> bool {
b.is_ascii_digit()
}
pub(crate) fn contains_word_ci(haystack: &[u8], word: &str) -> bool {
let mut from = 0;
while let Some(at) = find_ci(haystack, word.as_bytes(), from) {
if word_start(haystack, at) && word_end(haystack, at + word.len()) {
return true;
}
from = at + 1;
}
false
}
pub(crate) fn prefixed_word_ci<'a>(haystack: &'a [u8], prefix: &str, min: usize) -> Option<&'a [u8]> {
let mut from = 0;
while let Some(at) = find_ci(haystack, prefix.as_bytes(), from) {
from = at + 1;
if !word_start(haystack, at) {
continue;
}
let run = run(haystack, at + prefix.len(), |b| b.is_ascii_alphanumeric());
let end = at + prefix.len() + run;
if run >= min && word_end(haystack, end) {
return Some(&haystack[at..end]);
}
}
None
}
pub(crate) fn run(haystack: &[u8], at: usize, class: impl Fn(u8) -> bool) -> usize {
haystack.get(at..).map_or(0, |rest| rest.iter().position(|&b| !class(b)).unwrap_or(rest.len()))
}
pub(crate) fn version_after<'a>(
haystack: &'a [u8],
tokens: &[&str],
separators: &[u8],
first: fn(u8) -> bool,
rest: fn(u8) -> bool,
) -> Option<&'a [u8]> {
let mut best: Option<(usize, &'a [u8])> = None;
for token in tokens {
let mut from = 0;
while let Some(at) = find_ci(haystack, token.as_bytes(), from) {
if best.is_some_and(|(start, _)| at >= start) {
break;
}
from = at + 1;
let separator = at + token.len();
let start = separator + 1;
if !haystack.get(separator).is_some_and(|b| separators.contains(b))
|| !haystack.get(start).is_some_and(|&b| first(b))
{
continue;
}
let end = start + 1 + run(haystack, start + 1, rest);
best = Some((at, &haystack[start..end]));
break;
}
}
best.map(|(_, version)| version)
}
pub(crate) fn slash_version<'a>(haystack: &'a [u8], token: &str) -> Option<&'a [u8]> {
version_after(haystack, &[token], b"/", dotted, dotted)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn finds_ignoring_case() {
assert_eq!(find_ci(b"Mozilla/5.0 CHROME/1", b"chrome", 0), Some(12));
assert_eq!(find_ci(b"abc", b"abcd", 0), None);
assert_eq!(find_ci(b"aXbxB", b"xb", 2), Some(3));
assert_eq!(find_ci(b"5.0 / 5.0", b"/ 5", 0), Some(4));
assert_eq!(find_ci(b"\xffA", b"a", 0), Some(1));
assert!(!contains_ci(b"\xc3\xa9", "\u{c9}"));
}
#[test]
fn captures_versions() {
let ua = b"Edg/x Edge/18.1 Edg/2";
assert_eq!(version_after(ua, &["EdgA", "EdgiOS", "Edg", "Edge"], b"/", dotted, dotted), Some(&b"18.1"[..]));
assert_eq!(slash_version(b"Chrome/ Chrome/.5a", "chrome"), Some(&b".5"[..]));
assert_eq!(prefixed_word_ci(b"x KFMAWI)", "KF", 2), Some(&b"KFMAWI"[..]));
assert_eq!(prefixed_word_ci(b"x KFMAWI_", "KF", 2), None);
assert_eq!(prefixed_word_ci(b"xKFMA KFA KFAB", "kf", 2), Some(&b"KFAB"[..]));
assert!(contains_word_ci(b"(Red Hat)", "Red Hat"));
assert!(!contains_word_ci(b"Ubuntus", "Ubuntu"));
}
}