diff --git a/doublestar_test.go b/doublestar_test.go index aecd670..26ff894 100644 --- a/doublestar_test.go +++ b/doublestar_test.go @@ -273,6 +273,33 @@ func BenchmarkGoMatch(b *testing.B) { } } +func TestMatchUnvalidated(t *testing.T) { + for idx, tt := range matchTests { + testMatchUnvalidatedWith(t, idx, tt) + } +} + +func testMatchUnvalidatedWith(t *testing.T, idx int, tt MatchTest) { + defer func() { + if r := recover(); r != nil { + t.Errorf("#%v. MatchUnvalidated(%#q, %#q) panicked: %#v", idx, tt.pattern, tt.testPath, r) + } + }() + + // MatchUnvalidated() always uses "/" as the separator + ok := MatchUnvalidated(tt.pattern, tt.testPath) + if ok != tt.shouldMatch { + t.Errorf("#%v. MatchUnvalidated(%#q, %#q) = %v want %v", idx, tt.pattern, tt.testPath, ok, tt.shouldMatch) + } + + if tt.isStandard { + stdOk, _ := path.Match(tt.pattern, tt.testPath) + if ok != stdOk { + t.Errorf("#%v. MatchUnvalidated(%#q, %#q) != path.Match(...). Got %v want %v", idx, tt.pattern, tt.testPath, ok, stdOk) + } + } +} + func TestPathMatch(t *testing.T) { for idx, tt := range matchTests { // Even though we aren't actually matching paths on disk, we are using diff --git a/match.go b/match.go index 4232c79..c0f20af 100644 --- a/match.go +++ b/match.go @@ -53,6 +53,17 @@ func Match(pattern, name string) (bool, error) { return matchWithSeparator(pattern, name, '/', true) } +// MatchUnvalidated can provide a small performance improvement if you don't +// care about whether or not the pattern is valid (perhaps because you already +// ran `ValidatePattern`). Note that there's really only one case where this +// performance improvement is realized: when pattern matching reaches the end +// of `name` before reaching the end of `pattern`, such as `Match("a/b/c", +// "a")`. +func MatchUnvalidated(pattern, name string) bool { + matched, _ := matchWithSeparator(pattern, name, '/', false) + return matched +} + // PathMatch returns true if `name` matches the file name `pattern`. The // difference between Match and PathMatch is that PathMatch will automatically // use your system's path separator to split `name` and `pattern`. On systems @@ -67,6 +78,17 @@ func PathMatch(pattern, name string) (bool, error) { return matchWithSeparator(pattern, name, filepath.Separator, true) } +// PathMatchUnvalidated can provide a small performance improvement if you +// don't care about whether or not the pattern is valid (perhaps because you +// already ran `ValidatePattern`). Note that there's really only one case where +// this performance improvement is realized: when pattern matching reaches the +// end of `name` before reaching the end of `pattern`, such as `Match("a/b/c", +// "a")`. +func PathMatchUnvalidated(pattern, name string) bool { + matched, _ := matchWithSeparator(pattern, name, filepath.Separator, false) + return matched +} + func matchWithSeparator(pattern, name string, separator rune, validate bool) (matched bool, err error) { return doMatchWithSeparator(pattern, name, separator, validate, -1, -1, -1, -1, 0, 0) }