glob/glob.go

43 lines
965 B
Go
Raw Normal View History

2015-11-30 17:58:20 +03:00
package glob
2016-01-08 20:14:31 +03:00
import "strings"
2015-12-24 17:54:54 +03:00
2015-11-30 18:33:50 +03:00
// Glob represents compiled glob pattern.
2015-11-30 18:24:20 +03:00
type Glob interface {
2015-11-30 17:58:20 +03:00
Match(string) bool
}
2015-11-30 19:01:49 +03:00
// New creates Glob for given pattern and uses other given (if any) strings as separators.
// The pattern syntax is:
//
// pattern:
// { term }
// term:
// `*` matches any sequence of non-separator characters
// `**` matches any sequence of characters
// `?` matches any single non-separator character
// c matches character c (c != `*`, `**`, `?`, `\`)
// `\` c matches character c
2016-01-08 20:14:31 +03:00
func Compile(pattern string, separators ...string) (Glob, error) {
ast, err := parse(newLexer(pattern))
2015-12-24 17:54:54 +03:00
if err != nil {
return nil, err
}
2015-11-30 17:58:20 +03:00
2016-01-08 20:14:31 +03:00
matcher, err := compile(ast, strings.Join(separators, ""))
if err != nil {
return nil, err
2015-12-01 17:22:17 +03:00
}
2016-01-08 20:14:31 +03:00
return matcher, nil
2015-11-30 17:58:20 +03:00
}
2016-01-08 20:14:31 +03:00
func MustCompile(pattern string, separators ...string) Glob {
g, err := Compile(pattern, separators...)
if err != nil {
panic(err)
2015-11-30 18:24:20 +03:00
}
2016-01-08 20:14:31 +03:00
return g
2015-12-25 19:40:36 +03:00
}