Skip to content

Commit 04ca834

Browse files
committed
frame for wildmatch function and its tests (#301)
1 parent 683233e commit 04ca834

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

Diff for: git-glob/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ pub struct Pattern {
2222

2323
pub mod pattern;
2424

25+
pub mod wildmatch;
26+
pub use wildmatch::function::wildmatch;
27+
2528
mod parse;
2629
pub use parse::pattern as parse;

Diff for: git-glob/src/pattern.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{pattern, Pattern};
1+
use crate::{pattern, wildmatch, Pattern};
22
use bitflags::bitflags;
33
use bstr::{BStr, BString, ByteSlice};
44

@@ -17,15 +17,6 @@ bitflags! {
1717
const ABSOLUTE = 1 << 4;
1818
}
1919
}
20-
bitflags! {
21-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
22-
pub struct MatchOptions: u8 {
23-
/// Let globs not match the slash `/` literal.
24-
const SLASH_IS_LITERAL = 1 << 0;
25-
/// Match case insensitively for ascii characters only.
26-
const IGNORE_CASE = 1 << 1;
27-
}
28-
}
2920

3021
pub enum Case {
3122
/// The case affects the match
@@ -80,10 +71,10 @@ impl Pattern {
8071
return false;
8172
}
8273

83-
let flags = MatchOptions::SLASH_IS_LITERAL
74+
let flags = wildmatch::Mode::SLASH_IS_LITERAL
8475
| match case {
85-
Case::Fold => MatchOptions::IGNORE_CASE,
86-
Case::Sensitive => MatchOptions::empty(),
76+
Case::Fold => wildmatch::Mode::IGNORE_CASE,
77+
Case::Sensitive => wildmatch::Mode::empty(),
8778
};
8879
let path = path.into();
8980
debug_assert_eq!(
@@ -121,12 +112,12 @@ impl Pattern {
121112
}
122113
}
123114

124-
fn matches(&self, value: &BStr, options: MatchOptions) -> bool {
115+
fn matches(&self, value: &BStr, mode: wildmatch::Mode) -> bool {
125116
match self.first_wildcard_pos {
126117
// "*literal" case, overrides starts-with
127118
Some(pos) if self.mode.contains(pattern::Mode::ENDS_WITH) && !value.contains(&b'/') => {
128119
let text = &self.text[pos + 1..];
129-
if options.contains(MatchOptions::IGNORE_CASE) {
120+
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
130121
value
131122
.len()
132123
.checked_sub(text.len())
@@ -137,7 +128,7 @@ impl Pattern {
137128
}
138129
}
139130
Some(pos) => {
140-
if options.contains(MatchOptions::IGNORE_CASE) {
131+
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
141132
if !value
142133
.get(..pos)
143134
.map_or(false, |value| value.eq_ignore_ascii_case(&self.text[..pos]))
@@ -147,10 +138,10 @@ impl Pattern {
147138
} else if !value.starts_with(&self.text[..pos]) {
148139
return false;
149140
}
150-
todo!("actual wildcard match for '{}' ~= '{}'", self.text, value)
141+
crate::wildmatch(self.text.as_bstr(), value, mode)
151142
}
152143
None => {
153-
if options.contains(MatchOptions::IGNORE_CASE) {
144+
if mode.contains(wildmatch::Mode::IGNORE_CASE) {
154145
self.text.eq_ignore_ascii_case(value)
155146
} else {
156147
self.text == value

Diff for: git-glob/src/wildmatch.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use bitflags::bitflags;
2+
bitflags! {
3+
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
4+
pub struct Mode: u8 {
5+
/// Let globs not match the slash `/` literal.
6+
const SLASH_IS_LITERAL = 1 << 0;
7+
/// Match case insensitively for ascii characters only.
8+
const IGNORE_CASE = 1 << 1;
9+
}
10+
}
11+
12+
pub(crate) mod function {
13+
use crate::wildmatch::Mode;
14+
use bstr::BStr;
15+
16+
pub fn wildmatch(pattern: &BStr, value: &BStr, _mode: Mode) -> bool {
17+
todo!("actual wildcard match for '{}' ~= '{}'", pattern, value)
18+
}
19+
}

Diff for: git-glob/tests/glob.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod matching;
22
mod parse;
3+
mod wildmatch;

Diff for: git-glob/tests/wildmatch/mod.rs

Whitespace-only changes.

0 commit comments

Comments
 (0)