-
Notifications
You must be signed in to change notification settings - Fork 3
/
globset.go
112 lines (97 loc) · 2.8 KB
/
globset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ohmyglob
import (
"io"
"strings"
)
// GlobSet represents an ordered set of Globs, and has the same matching capabilities as a Glob. Globbing is done
// in order, with later globs taking precedence over earlier globs in the set. A GlobSet is immutable.
type GlobSet interface {
GlobMatcher
// Globs returns the ordered Glob objects contained within the set
Globs() []Glob
// String returns the patterns used to create the GlobSet
String() string
// MatchingGlob returns the Glob that matches the specified pattern (or does not match, in the case of a negative
// glob)
MatchingGlob(b []byte) Glob
// AllMatchingGlobs returns all Globs that match the specified pattern (or do not match, in the case of a negative
// glob)
AllMatchingGlobs(b []byte) []Glob
}
type globSetImpl []Glob
func (g globSetImpl) String() string {
strs := make([]string, len(g))
for i, glob := range g {
strs[i] = glob.String()
}
return strings.Join(strs, ", ")
}
func (g globSetImpl) Globs() []Glob {
globs := make([]Glob, 0, len(g))
globs = append(globs, g...)
return globs
}
func (g globSetImpl) MatchingGlob(b []byte) Glob {
// By iterating in reverse order, we can bail early if we get a match further down. If we iterated in normal order,
// we would HAVE to check every glob
for i := len(g) - 1; i >= 0; i-- {
glob := g[i]
matches := glob.Match(b)
if matches {
Logger.Tracef("[ohmyglob:GlobSet] %s matched to %s", string(b), glob.String())
return glob
}
}
return nil
}
func (g globSetImpl) AllMatchingGlobs(b []byte) []Glob {
result := []Glob(nil)
for _, glob := range g {
if glob.Match(b) {
Logger.Tracef("[ohmyglob:GlobSet] %s matched to %s", string(b), glob.String())
result = append(result, glob)
}
}
return result
}
func (g globSetImpl) Match(b []byte) bool {
glob := g.MatchingGlob(b)
return glob != nil && !glob.IsNegative()
}
func (g globSetImpl) MatchReader(r io.RuneReader) bool {
// Drain the reader and convert to a byte array
b := make([]byte, 0, 10)
for {
rn, _, err := r.ReadRune()
if err == io.EOF {
break
} else if err != nil {
return false
}
b = append(b, byte(rn))
}
return g.Match(b)
}
func (g globSetImpl) MatchString(s string) bool {
return g.Match([]byte(s))
}
// NewGlobSet constructs a GlobSet from a slice of Globs.
func NewGlobSet(globs []Glob) (GlobSet, error) {
set := make(globSetImpl, len(globs))
for i, glob := range globs {
set[i] = glob
}
return set, nil
}
// CompileGlobSet constructs a GlobSet from a slice of strings, which will be compiled individually to Globs.
func CompileGlobSet(patterns []string, options *Options) (GlobSet, error) {
globs := make(globSetImpl, len(patterns))
for i, pattern := range patterns {
glob, err := Compile(pattern, options)
if err != nil {
return nil, err
}
globs[i] = glob
}
return globs, nil
}