glob/glob.go

57 lines
1.5 KiB
Go
Raw Normal View History

2015-11-30 17:58:20 +03:00
package glob
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
}
2016-01-18 13:44:31 +03:00
// Compile creates Glob for given pattern and strings (if any present after pattern) as separators.
2015-11-30 19:01:49 +03:00
// The pattern syntax is:
//
2016-01-18 13:42:51 +03:00
// pattern:
// { term }
2016-01-18 13:38:19 +03:00
//
2016-01-18 13:42:51 +03:00
// term:
// `*` matches any sequence of non-separator characters
// `**` matches any sequence of characters
// `?` matches any single non-separator character
// `[` [ `!` ] { character-range } `]`
// character class (must be non-empty)
// `{` pattern-list `}`
// pattern alternatives
// c matches character c (c != `*`, `**`, `?`, `\`, `[`, `{`, `}`)
// `\` c matches character c
2016-01-18 13:38:19 +03:00
//
2016-01-18 13:42:51 +03:00
// character-range:
// c matches character c (c != `\\`, `-`, `]`)
// `\` c matches character c
// lo `-` hi matches character c for lo <= c <= hi
//
// pattern-list:
// pattern { `,` pattern }
// comma-separated (without spaces) patterns
2016-01-18 13:38:19 +03:00
//
2016-02-02 14:57:42 +03:00
func Compile(pattern string, separators ...rune) (Glob, error) {
2016-01-08 20:14:31 +03:00
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-02-02 14:57:42 +03:00
matcher, err := compile(ast, separators)
2016-01-08 20:14:31 +03:00
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-18 13:38:19 +03:00
// MustCompile is the same as Compile, except that if Compile returns error, this will panic
2016-02-02 22:03:37 +03:00
func MustCompile(pattern string, separators ...rune) Glob {
2016-01-08 20:14:31 +03:00
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
}