Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added MatchWithSeparator and ValidateWithSeparator #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func PathMatch(pattern, name string) (bool, error) {
return matchWithSeparator(pattern, name, filepath.Separator, true)
}

// MatchWithSeparator returns true if `name` matches the file name `pattern`
// using the specified rune as separator.
func MatchWithSeparator(pattern, name string, separator rune) (bool, error) {
return matchWithSeparator(pattern, name, separator, true)
}

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 Expand Up @@ -283,7 +289,7 @@ MATCH:
}
}

if validate && patIdx < patLen && !doValidatePattern(pattern[patIdx:], separator) {
if validate && patIdx < patLen && !ValidateWithSeparator(pattern[patIdx:], separator) {
return false, ErrBadPattern
}
return false, nil
Expand Down Expand Up @@ -334,7 +340,7 @@ func isZeroLengthPattern(pattern string, separator rune) (ret bool, err error) {
}

// no luck - validate the rest of the pattern
if !doValidatePattern(pattern, separator) {
if !ValidateWithSeparator(pattern, separator) {
return false, ErrBadPattern
}
return false, nil
Expand Down
7 changes: 4 additions & 3 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "path/filepath"
// ValidatePattern assumes your pattern uses '/' as the path separator.
//
func ValidatePattern(s string) bool {
return doValidatePattern(s, '/')
return ValidateWithSeparator(s, '/')
}

// Like ValidatePattern, only uses your OS path separator. In other words, use
Expand All @@ -20,10 +20,11 @@ func ValidatePattern(s string) bool {
// Glob() requires '/' separators, even if your OS uses something else.
//
func ValidatePathPattern(s string) bool {
return doValidatePattern(s, filepath.Separator)
return ValidateWithSeparator(s, filepath.Separator)
}

func doValidatePattern(s string, separator rune) bool {
// ValidateWithSeparator is like ValidatePattern using the specified separator.
func ValidateWithSeparator(s string, separator rune) bool {
altDepth := 0
l := len(s)
VALIDATE:
Expand Down