wildcard-glob 11.2.0

Zero-dependency, zero-allocation compiled wildcard and path glob pattern matcher supporting *, **, ?, character classes, brace alternation, and multi-pattern GlobSet.
Documentation
use super::*;

/// FNV-1a byte hashing implementation for [`FastHasher`].
impl Hasher for FastHasher {
    /// Returns the accumulated 64-bit hash digest.
    ///
    /// # Returns
    ///
    /// - `u64` - Finalized hash value.
    #[inline(always)]
    fn finish(&self) -> u64 {
        let Self { state } = self;
        *state
    }

    /// Mixes a byte slice into the FNV-1a state.
    ///
    /// # Arguments
    ///
    /// - `&[u8]` - Input bytes to hash.
    #[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;
    }
}

/// Constructs [`FastHasher`] instances for [`HashSet`] exact-literal lookups.
impl BuildHasher for FastBuildHasher {
    type Hasher = FastHasher;

    /// Returns a fresh [`FastHasher`] initialized to the FNV-1a offset basis.
    ///
    /// # Returns
    ///
    /// - `Self::Hasher` - Initialized hasher.
    #[inline(always)]
    fn build_hasher(&self) -> Self::Hasher {
        FastHasher {
            state: FNV_OFFSET_BASIS,
        }
    }
}

/// Default empty construction for [`GlobSet`].
impl Default for GlobSet {
    /// Creates an empty [`GlobSet`].
    ///
    /// # Returns
    ///
    /// - `Self` - Empty glob set.
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

/// Construction and lookup operations for [`GlobSet`].
impl GlobSet {
    /// Creates an empty [`GlobSet`].
    ///
    /// # Returns
    ///
    /// - `Self` - Empty glob set.
    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(),
        }
    }

    /// Compiles an iterator of path-aware glob patterns into a [`GlobSet`].
    ///
    /// # Arguments
    ///
    /// - `I` - Iterator yielding pattern string references.
    ///
    /// # Returns
    ///
    /// - `Result<Self, GlobError>` - Compiled glob set or first pattern compile error.
    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)
    }

    /// Adds a compiled [`GlobPattern`] into the set.
    ///
    /// # Arguments
    ///
    /// - `GlobPattern` - Compiled pattern to insert.
    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));
            }
        }
    }

    /// Returns the total number of patterns stored in the set.
    ///
    /// # Returns
    ///
    /// - `usize` - Combined count of exact and wildcard patterns.
    #[inline(always)]
    pub fn len(&self) -> usize {
        let Self {
            exact,
            wildcard_count,
            ..
        } = self;
        exact.len() + *wildcard_count
    }

    /// Returns whether the set contains zero patterns.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when empty.
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Tests whether `candidate` matches at least one pattern in the set.
    ///
    /// # Arguments
    ///
    /// - `&str` - Input string to match.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when any member pattern matches.
    #[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
    }
}