Skip to content

Commit

Permalink
very basic beginnings of wildmatch (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 12, 2022
1 parent 1336bc9 commit 334c624
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
52 changes: 50 additions & 2 deletions git-glob/src/wildmatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,55 @@ pub(crate) mod function {

use crate::wildmatch::Mode;

pub fn wildmatch(pattern: &BStr, value: &BStr, _mode: Mode) -> bool {
todo!("actual wildcard match for '{}' ~= '{}'", pattern, value)
#[derive(Eq, PartialEq)]
enum Result {
Match,
NoMatch,
AbortAll,
// AbortToStarStar,
}

const STAR: u8 = b'*';

fn match_recursive(pattern: &BStr, text: &BStr, mode: Mode) -> Result {
use self::Result::*;
let possibly_lowercase = |c: &u8| {
if mode.contains(Mode::IGNORE_CASE) {
c.to_ascii_lowercase()
} else {
*c
}
};
let mut p = pattern.iter().map(possibly_lowercase);
let mut t = text.iter().map(possibly_lowercase);

while let Some(mut p_ch) = p.next() {
let t_ch = match t.next() {
Some(c) => c,
None if p_ch != STAR => return AbortAll,
None => 0,
};

if p_ch == b'\\' {
p_ch = match p.next() {
Some(c) => c,
None => return NoMatch,
};
}
match p_ch {
non_glob_ch => {
if non_glob_ch != t_ch {
return NoMatch;
} else {
continue;
}
}
}
}
t.next().map(|_| NoMatch).unwrap_or(Match)
}

pub fn wildmatch(pattern: &BStr, value: &BStr, mode: Mode) -> bool {
match_recursive(pattern, value, mode) == Result::Match
}
}
2 changes: 2 additions & 0 deletions git-glob/tests/fixtures/make_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ while read -r pattern nomatch; do
done <<EOF >>git-baseline.nmatch
*/\ XXX/\
*/\\ XXX/\
/*foo bar/foo
/*foo bar/bazfoo
foo*bar foo/baz/bar
/*foo.txt hello/foo.txt
bar/foo baz/bar/foo
Expand Down
6 changes: 2 additions & 4 deletions git-glob/tests/matching/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ fn basename_glob_and_literal_is_ends_with() {
}

#[test]
#[ignore]
fn special_cases_from_corpus() {
let pattern = &pat("foo*bar");
assert!(
Expand Down Expand Up @@ -266,12 +265,11 @@ fn absolute_basename_glob_and_literal_is_ends_with_in_basenames() {
}

#[test]
#[ignore]
fn absolute_basename_glob_and_literal_is_glob_in_paths() {
let pattern = &pat("/*foo");

assert!(match_file(pattern, "bar/foo", Case::Sensitive), "* does not match /");
assert!(match_file(pattern, "bar/bazfoo", Case::Sensitive));
assert!(!match_file(pattern, "bar/foo", Case::Sensitive), "* does not match /");
assert!(!match_file(pattern, "bar/bazfoo", Case::Sensitive));
}

#[test]
Expand Down

0 comments on commit 334c624

Please sign in to comment.