Documentation
¶
Overview ¶
Package glob compiles path-aware glob patterns for matching strings.
Patterns match the entire input. By default, slash separates path components on every operating system. WithSeparator selects another separator when compiling a pattern. The package does not access the filesystem or clean, normalize, or otherwise modify paths.
The pattern language provides:
- * to match zero or more runes within one component
- ? to match one rune within one component
- [...] and [!...] character classes within one component
- ** written together as a complete component to match zero or more components
- {...,...} alternatives, including nested and empty alternatives
- backslash to quote the next pattern rune, except that an escaped configured separator outside a class remains structural
A compiled Pattern is immutable and safe for concurrent use. Matching performs no heap allocations. For an input of N runes and source pattern of M runes, matching takes O(N*M) time and O(M) working memory; for a fixed pattern, time is linear in the input length. Compile patterns once and reuse them when matching multiple inputs.
Example ¶
package main
import (
"fmt"
"github.com/greatliontech/glob"
)
func main() {
pattern := glob.MustCompile("{cmd,internal}/**/*.{go,mod}")
for _, path := range []string{
"cmd/main.go",
"internal/net/http.go",
"cmd/main.sum",
"main.go",
} {
fmt.Printf("%s: %t\n", path, pattern.Match(path))
}
}
Output: cmd/main.go: true internal/net/http.go: true cmd/main.sum: false main.go: false
Index ¶
Examples ¶
Constants ¶
const MaxPatternBytes = 4096
MaxPatternBytes is the largest pattern accepted by Compile.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompileError ¶
CompileError describes a pattern compilation failure. Offset is a byte offset into the pattern, or -1 when the error concerns configuration.
func (*CompileError) Error ¶
func (e *CompileError) Error() string
type Option ¶
type Option func(*config)
Option configures pattern compilation. Passing a nil Option to Compile or MustCompile is invalid.
func WithSeparator ¶
WithSeparator configures separator as the path separator. The default is '/'. Separator must be a Unicode scalar value. If multiple separator options are supplied, the last one takes effect.
Example ¶
package main
import (
"fmt"
"github.com/greatliontech/glob"
)
func main() {
pattern := glob.MustCompile("**.example.com", glob.WithSeparator('.'))
fmt.Println(pattern.Match("example.com"))
fmt.Println(pattern.Match("api.eu.example.com"))
fmt.Println(pattern.Match("example.net"))
}
Output: true true false
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
Pattern is an immutable compiled glob pattern. A Pattern is safe for concurrent use by multiple goroutines. Its zero value is invalid; methods must be called on a non-nil Pattern returned by successful compilation.
func Compile ¶
Compile compiles pattern into an immutable matcher. Pattern must be valid UTF-8 and no longer than MaxPatternBytes. Options are applied in order. Compile returns a CompileError for invalid syntax or configuration.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/greatliontech/glob"
)
func main() {
_, err := glob.Compile("[z-a]")
var compileErr *glob.CompileError
if errors.As(err, &compileErr) {
fmt.Printf("invalid pattern at byte %d\n", compileErr.Offset)
}
}
Output: invalid pattern at byte 2
func MustCompile ¶
MustCompile is like Compile but panics if pattern cannot be compiled.