Skip to content

Commit

Permalink
closes #92 adds MatchUnvalidated
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatcuk committed Oct 11, 2024
1 parent 424062b commit 2832f67
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
27 changes: 27 additions & 0 deletions doublestar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down

0 comments on commit 2832f67

Please sign in to comment.