use super::*;
impl Hasher for FastHasher {
#[inline(always)]
fn finish(&self) -> u64 {
let Self { state } = self;
*state
}
#[inline(always)]
fn write(&mut self, bytes: &[u8]) {
let Self { state } = self;
let mut h: u64 = *state;
for &b in bytes {
h ^= u64::from(b);
h = h.wrapping_mul(FNV_PRIME);
}
*state = h;
}
}
impl BuildHasher for FastBuildHasher {
type Hasher = FastHasher;
#[inline(always)]
fn build_hasher(&self) -> Self::Hasher {
FastHasher {
state: FNV_OFFSET_BASIS,
}
}
}
impl Default for GlobSet {
#[inline(always)]
fn default() -> Self {
Self::new()
}
}
impl GlobSet {
pub fn new() -> Self {
Self {
exact: HashSet::with_hasher(FastBuildHasher),
min_exact_len: usize::MAX,
max_exact_len: 0,
wildcard_count: 0,
branches: Vec::new(),
}
}
pub fn compile<I, S>(patterns: I) -> Result<Self, GlobError>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut set: Self = Self::new();
for pat in patterns {
set.add_pattern(GlobPattern::compile(pat.as_ref())?);
}
Ok(set)
}
pub fn add_pattern(&mut self, pattern: GlobPattern) {
let Self {
exact,
min_exact_len,
max_exact_len,
wildcard_count,
branches,
} = self;
if let Some(lit) = pattern.exact_literal() {
let len: usize = lit.len();
if len < *min_exact_len {
*min_exact_len = len;
}
if len > *max_exact_len {
*max_exact_len = len;
}
exact.insert(lit.to_owned());
} else {
*wildcard_count += 1;
let (pat_branches, path_aware): (Vec<CompiledBranch>, bool) =
pattern.into_compiled_parts();
branches.reserve(pat_branches.len());
for branch in pat_branches {
branches.push((branch, path_aware));
}
}
}
#[inline(always)]
pub fn len(&self) -> usize {
let Self {
exact,
wildcard_count,
..
} = self;
exact.len() + *wildcard_count
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn is_match(&self, candidate: &str) -> bool {
let Self {
exact,
min_exact_len,
max_exact_len,
branches,
..
} = self;
let len: usize = candidate.len();
if len >= *min_exact_len && len <= *max_exact_len && exact.contains(candidate) {
return true;
}
for (branch, path_aware) in branches {
if match_branch(branch, candidate, *path_aware) {
return true;
}
}
false
}
}