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 (
Any = "*"
SuperAny = "**"
SingleAny = "?"
any = "*"
superAny = "**"
singleAny = "?"
)
var Chars = []string{Any, SuperAny, SingleAny}
var chars = []string{any, superAny, singleAny}
// Glob represents compiled glob pattern.
type Glob interface {
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 {
chunks := parse(pattern, nil, strings.Join(d, ""))
@ -32,7 +34,7 @@ func parse(p string, m []Glob, d string) []Glob {
return m
}
i, c := firstIndexOfChars(p, Chars)
i, c := firstIndexOfChars(p, chars)
if i == -1 {
return append(m, raw{p})
}
@ -42,11 +44,11 @@ func parse(p string, m []Glob, d string) []Glob {
}
switch c {
case SuperAny:
case superAny:
m = append(m, multiple{})
case Any:
case any:
m = append(m, multiple{d})
case SingleAny:
case singleAny:
m = append(m, single{d})
}

View File

@ -52,7 +52,7 @@ func main() {
## Performance
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.
Run `go test bench=.` from source root to see the benchmarks: