core/glob/glob.go
Ingo Oppermann 129058e633
Improve FS.List and FS.RemoveList functions, improve CleanupFS
The FS.List and FS.RemoveList are up to 42x faster by precompiling the
globbing pattern.

The CleanupFS from restreamer/fs is up to 32x faster in the benchmarks
and cleanup is now only every 5 seconds instead of every second.
2023-06-12 11:36:07 +02:00

37 lines
752 B
Go

package glob
import "github.com/gobwas/glob"
type Glob interface {
Match(name string) bool
}
type globber struct {
glob glob.Glob
}
func Compile(pattern string, separators ...rune) (Glob, error) {
g, err := glob.Compile(pattern, separators...)
if err != nil {
return nil, err
}
return &globber{glob: g}, nil
}
func (g *globber) Match(name string) bool {
return g.glob.Match(name)
}
// Match returns whether the name matches the glob pattern, also considering
// one or several optionnal separator. An error is only returned if the pattern
// is invalid.
func Match(pattern, name string, separators ...rune) (bool, error) {
g, err := Compile(pattern, separators...)
if err != nil {
return false, err
}
return g.Match(name), nil
}