pub(crate) fn matches(pattern: &str, name: &str) -> bool {
fn rec(p: &[u8], n: &[u8]) -> bool {
match p.first() {
None => n.is_empty(),
Some(b'*') if p.get(1) == Some(&b'*') => {
let rest = &p[2..];
match rest.first() {
None => true,
Some(b'/') => {
if rec(&rest[1..], n) {
return true;
}
n.iter()
.enumerate()
.any(|(at, &c)| c == b'/' && rec(p, &n[at + 1..]))
}
_ => rec(rest, n) || (!n.is_empty() && rec(p, &n[1..])),
}
}
Some(b'*') => rec(&p[1..], n) || (!n.is_empty() && n[0] != b'/' && rec(p, &n[1..])),
Some(b'?') => !n.is_empty() && n[0] != b'/' && rec(&p[1..], &n[1..]),
Some(&c) => !n.is_empty() && n[0] == c && rec(&p[1..], &n[1..]),
}
}
rec(pattern.as_bytes(), name.as_bytes())
}
#[cfg(test)]
mod tests {
use super::matches;
#[test]
fn a_single_star_stays_inside_one_segment() {
assert!(matches("*.png", "logo.png"));
assert!(!matches("*.png", "sub/logo.png"));
assert!(matches("sub/*.png", "sub/logo.png"));
assert!(!matches("sub/*.png", "sub/deep/logo.png"));
}
#[test]
fn a_question_mark_is_one_character_and_never_a_separator() {
assert!(matches("a?c.txt", "abc.txt"));
assert!(!matches("a?c.txt", "ac.txt"));
assert!(!matches("a?c", "a/c"));
}
#[test]
fn a_double_star_segment_stands_for_any_number_of_segments() {
assert!(matches("**/logo.png", "logo.png"));
assert!(matches("**/logo.png", "sub/logo.png"));
assert!(matches("**/logo.png", "a/b/c/logo.png"));
assert!(matches("**/*.png", "a/b/logo.png"));
assert!(!matches("**/*.png", "a/b/logo.jpg"));
}
#[test]
fn a_double_star_matches_in_the_middle_and_at_the_end() {
assert!(matches("a/**/b.txt", "a/b.txt"));
assert!(matches("a/**/b.txt", "a/x/b.txt"));
assert!(matches("a/**/b.txt", "a/x/y/b.txt"));
assert!(!matches("a/**/b.txt", "z/x/b.txt"));
assert!(matches("a/**", "a/b.txt"));
assert!(matches("a/**", "a/x/y/b.txt"));
assert!(!matches("a/**", "a"));
}
#[test]
fn a_bare_double_star_takes_everything() {
assert!(matches("**", "a.txt"));
assert!(matches("**", "a/b/c.txt"));
}
#[test]
fn a_double_star_not_on_a_segment_boundary_is_a_star_that_crosses() {
assert!(matches("a**z", "abz"));
assert!(matches("a**z", "ab/cz"));
assert!(!matches("a*z", "ab/cz"));
}
#[test]
fn a_pattern_matches_nothing_it_does_not_cover() {
assert!(!matches("", "a"));
assert!(matches("", ""));
assert!(!matches("a.txt", "a.txtx"));
assert!(!matches("a.txt", "xa.txt"));
}
}