use std::sync::Arc;
use globset::GlobBuilder;
use super::{Built, LazyGlob};
const BACKSLASH: &str = "\u{1}";
const GLOB_CHARS: [char; 9] = ['*', '?', '[', ']', '{', '}', '\\', ',', '!'];
pub(super) fn cannot_fail(glob: &str) -> bool {
!glob.contains(['[', '{', '}', '\\'])
}
#[derive(Debug, Clone)]
pub(super) enum WordGlob {
Literal(String),
Glob(Arc<LazyGlob>),
}
impl WordGlob {
pub(super) fn new(word: &str) -> Result<Self, globset::Error> {
Self::new_in(word, &mut Built::default())
}
pub(super) fn new_in(word: &str, built: &mut Built) -> Result<Self, globset::Error> {
if !word.contains(GLOB_CHARS) {
return Ok(Self::Literal(word.to_owned()));
}
built
.word(word, || {
let mut glob = String::with_capacity(word.len());
let mut chars = word.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\' && chars.peek() == Some(&'\\') {
chars.next();
glob.push_str(BACKSLASH);
} else {
glob.push(c);
}
}
GlobBuilder::new(&glob)
.literal_separator(false)
.backslash_escape(true)
.build()
.map(|g| LazyGlob::new(g, false))
})
.map(Self::Glob)
}
pub(super) fn is_match(&self, word: &str) -> bool {
match self {
Self::Literal(literal) => literal == word,
Self::Glob(glob) if word.contains('\\') => {
glob.is_match(&word.replace('\\', BACKSLASH))
}
Self::Glob(glob) => glob.is_match(word),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn literal_words_match_exactly_what_their_glob_matches() {
let words = [
"git", "--force", "-rf", "", "ünï", "a/b", "C:", "x.y", "$", "|", "=", "@",
];
let candidates = [
"git",
"Git",
"git ",
"gitx",
"xgit",
"--force",
"--force-with-lease",
"-rf",
"-fr",
"",
"ünï",
"ÜNÏ",
"a/b",
"a\\b",
"a//b",
"C:",
"c:",
"x.y",
"xzy",
"$",
"|",
"=",
"@",
];
for word in words {
let literal = WordGlob::new(word).unwrap();
assert!(matches!(literal, WordGlob::Literal(_)), "{word}");
let glob = WordGlob::Glob(Arc::new(LazyGlob::new(
GlobBuilder::new(word)
.literal_separator(false)
.backslash_escape(true)
.build()
.unwrap(),
false,
)));
for candidate in candidates {
assert_eq!(
literal.is_match(candidate),
glob.is_match(candidate),
"{word:?} vs {candidate:?}"
);
}
}
}
#[test]
fn what_cannot_fail_always_parses() {
let alphabet = ['a', '/', '*', '?', ',', '!', '.', '-', 'é', ']'];
let mut strings = vec![String::new()];
let mut last = vec![String::new()];
for _ in 0..5 {
last = last
.iter()
.flat_map(|s| alphabet.iter().map(move |c| format!("{s}{c}")))
.collect();
strings.extend(last.iter().cloned());
}
for s in &strings {
assert!(cannot_fail(s));
assert!(WordGlob::new(s).is_ok(), "{s:?}");
for separator in [false, true] {
let built = GlobBuilder::new(s).literal_separator(separator).build();
assert!(built.is_ok(), "{s:?}");
}
}
}
#[test]
fn glob_syntax_still_compiles_to_a_glob() {
for word in [
"*sh*", "--force*", "x?", "[ab]", "{a,b}", r"\*", "a]", "!x", "a,b",
] {
assert!(
matches!(WordGlob::new(word), Ok(WordGlob::Glob(_))),
"{word}"
);
}
for malformed in ["{a", "[a", "b}", "a\\", "[z-a]", "{a,{b}"] {
assert!(!cannot_fail(malformed), "{malformed}");
}
for malformed in ["{a", "[a", "b}"] {
assert!(
WordGlob::new(malformed).is_err(),
"{malformed} is still rejected"
);
}
}
}