This commit is contained in:
s.kamardin 2015-11-30 18:33:50 +03:00
parent 015b706bca
commit 3c6119e9f6
2 changed files with 11 additions and 9 deletions

18
glob.go
View File

@ -6,17 +6,19 @@ import (
) )
const ( const (
Any = "*" any = "*"
SuperAny = "**" superAny = "**"
SingleAny = "?" singleAny = "?"
) )
var Chars = []string{Any, SuperAny, SingleAny} var chars = []string{any, superAny, singleAny}
// Glob represents compiled glob pattern.
type Glob interface { type Glob interface {
Match(string) bool Match(string) bool
} }
// New creates Glob for given pattern and uses other given (if any) strings as delimiters.
func New(pattern string, d ...string) Glob { func New(pattern string, d ...string) Glob {
chunks := parse(pattern, nil, strings.Join(d, "")) chunks := parse(pattern, nil, strings.Join(d, ""))
@ -32,7 +34,7 @@ func parse(p string, m []Glob, d string) []Glob {
return m return m
} }
i, c := firstIndexOfChars(p, Chars) i, c := firstIndexOfChars(p, chars)
if i == -1 { if i == -1 {
return append(m, raw{p}) return append(m, raw{p})
} }
@ -42,11 +44,11 @@ func parse(p string, m []Glob, d string) []Glob {
} }
switch c { switch c {
case SuperAny: case superAny:
m = append(m, multiple{}) m = append(m, multiple{})
case Any: case any:
m = append(m, multiple{d}) m = append(m, multiple{d})
case SingleAny: case singleAny:
m = append(m, single{d}) m = append(m, single{d})
} }

View File

@ -52,7 +52,7 @@ func main() {
## Performance ## Performance
In comparison with [go-glob](https://github.com/ryanuber/go-glob), it is ~2.7x faster (on my personal Mac), In comparison with [go-glob](https://github.com/ryanuber/go-glob), it is ~2.7x faster (on my personal Mac),
because my glob is compiling patterns for multiple usages. If you will not use compiled `glob.Glob` object, because my impl compiles patterns for future usage. If you will not use compiled `glob.Glob` object,
and do `g := glob.New(pattern); g.Match(...)` every time, then your code will be about ~3x slower. and do `g := glob.New(pattern); g.Match(...)` every time, then your code will be about ~3x slower.
Run `go test bench=.` from source root to see the benchmarks: Run `go test bench=.` from source root to see the benchmarks: