Skip to content

Commit 2832f67

Browse files
committed
closes #92 adds MatchUnvalidated
1 parent 424062b commit 2832f67

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

doublestar_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,33 @@ func BenchmarkGoMatch(b *testing.B) {
273273
}
274274
}
275275

276+
func TestMatchUnvalidated(t *testing.T) {
277+
for idx, tt := range matchTests {
278+
testMatchUnvalidatedWith(t, idx, tt)
279+
}
280+
}
281+
282+
func testMatchUnvalidatedWith(t *testing.T, idx int, tt MatchTest) {
283+
defer func() {
284+
if r := recover(); r != nil {
285+
t.Errorf("#%v. MatchUnvalidated(%#q, %#q) panicked: %#v", idx, tt.pattern, tt.testPath, r)
286+
}
287+
}()
288+
289+
// MatchUnvalidated() always uses "/" as the separator
290+
ok := MatchUnvalidated(tt.pattern, tt.testPath)
291+
if ok != tt.shouldMatch {
292+
t.Errorf("#%v. MatchUnvalidated(%#q, %#q) = %v want %v", idx, tt.pattern, tt.testPath, ok, tt.shouldMatch)
293+
}
294+
295+
if tt.isStandard {
296+
stdOk, _ := path.Match(tt.pattern, tt.testPath)
297+
if ok != stdOk {
298+
t.Errorf("#%v. MatchUnvalidated(%#q, %#q) != path.Match(...). Got %v want %v", idx, tt.pattern, tt.testPath, ok, stdOk)
299+
}
300+
}
301+
}
302+
276303
func TestPathMatch(t *testing.T) {
277304
for idx, tt := range matchTests {
278305
// Even though we aren't actually matching paths on disk, we are using

match.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ func Match(pattern, name string) (bool, error) {
5353
return matchWithSeparator(pattern, name, '/', true)
5454
}
5555

56+
// MatchUnvalidated can provide a small performance improvement if you don't
57+
// care about whether or not the pattern is valid (perhaps because you already
58+
// ran `ValidatePattern`). Note that there's really only one case where this
59+
// performance improvement is realized: when pattern matching reaches the end
60+
// of `name` before reaching the end of `pattern`, such as `Match("a/b/c",
61+
// "a")`.
62+
func MatchUnvalidated(pattern, name string) bool {
63+
matched, _ := matchWithSeparator(pattern, name, '/', false)
64+
return matched
65+
}
66+
5667
// PathMatch returns true if `name` matches the file name `pattern`. The
5768
// difference between Match and PathMatch is that PathMatch will automatically
5869
// use your system's path separator to split `name` and `pattern`. On systems
@@ -67,6 +78,17 @@ func PathMatch(pattern, name string) (bool, error) {
6778
return matchWithSeparator(pattern, name, filepath.Separator, true)
6879
}
6980

81+
// PathMatchUnvalidated can provide a small performance improvement if you
82+
// don't care about whether or not the pattern is valid (perhaps because you
83+
// already ran `ValidatePattern`). Note that there's really only one case where
84+
// this performance improvement is realized: when pattern matching reaches the
85+
// end of `name` before reaching the end of `pattern`, such as `Match("a/b/c",
86+
// "a")`.
87+
func PathMatchUnvalidated(pattern, name string) bool {
88+
matched, _ := matchWithSeparator(pattern, name, filepath.Separator, false)
89+
return matched
90+
}
91+
7092
func matchWithSeparator(pattern, name string, separator rune, validate bool) (matched bool, err error) {
7193
return doMatchWithSeparator(pattern, name, separator, validate, -1, -1, -1, -1, 0, 0)
7294
}

0 commit comments

Comments
 (0)