/
glob.go
220 lines (202 loc) · 5.58 KB
/
glob.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package glob
import (
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/dlclark/regexp2"
"github.com/pachyderm/ohmyglob/compiler"
"github.com/pachyderm/ohmyglob/syntax"
)
// Glob represents compiled glob pattern.
type Glob struct {
r *regexp2.Regexp
}
// Compile creates Glob for given pattern and strings (if any present after pattern) as separators.
// The pattern syntax is:
//
// pattern:
// { term }
//
// term:
// `*` matches any sequence of non-separator characters
// `**` matches any sequence of characters
// `?` matches any single non-separator character
// `[` [ `!` ] { character-range } `]`
// character class (must be non-empty)
// `{` pattern-list `}`
// pattern alternatives
// c matches character c (c != `*`, `**`, `?`, `\`, `[`, `{`, `}`)
// `\` c matches character c
//
// character-range:
// c matches character c (c != `\\`, `-`, `]`)
// `\` c matches character c
// lo `-` hi matches character c for lo <= c <= hi
//
// pattern-list:
// pattern { `,` pattern }
// comma-separated (without spaces) patterns
//
// extended-glob:
// `(` { `|` pattern } `)`
// `@(` { `|` pattern } `)`
// match and capture one of pipe-separated subpatterns
// `*(` { `|` pattern } `)`
// match and capture any number of the pipe-separated subpatterns
// `+(` { `|` pattern } `)`
// match and capture one or more of the pipe-separated subpatterns
// `?(` { `|` pattern } `)`
// match and capture zero or one of the pipe-separated subpatterns
// `!(` { `|` pattern } `)`
// match and capture anything except one of the pipe-separated subpatterns
//
func Compile(pattern string, separators ...rune) (*Glob, error) {
tree, err := syntax.Parse(pattern)
if err != nil {
return nil, err
}
regex, err := compiler.Compile(tree, separators)
if err != nil {
return nil, err
}
r, err := regexp2.Compile(regex, 0)
if err != nil {
return nil, err
}
r.MatchTimeout = time.Minute * 5 // if it takes more than 5minutes to match a glob, something is very wrong
return &Glob{r: r}, nil
}
// MustCompile is the same as Compile, except that if Compile returns error, this will panic
func MustCompile(pattern string, separators ...rune) *Glob {
g, err := Compile(pattern, separators...)
if err != nil {
panic(err)
}
return g
}
// Match tests the fixture against the compiled pattern, and return true for a match
func (g *Glob) Match(fixture string) bool {
m, err := g.r.MatchString(fixture)
if err != nil {
// this is taking longer than 5 minutes, so something is seriously wrong
panic(err)
}
return m
}
// Capture returns the list of subexpressions captured while testing the fixture against the compiled pattern
func (g *Glob) Capture(fixture string) []string {
m, err := g.r.FindStringMatch(fixture)
if err != nil {
// this is taking longer than 5 minutes, so something is seriously wrong
panic(err)
}
if m == nil {
return nil
}
groups := m.Groups()
captures := make([]string, 0, len(groups))
for _, gp := range groups {
captures = append(captures, gp.Capture.String())
}
return captures
}
// The code for the extract function is based on the extract function from https://golang.org/src/regexp/regexp.go
// Additionally, the Replace function is based on the expand function from https://golang.org/src/regexp/regexp.go
func extract(str string) (int, string, bool) {
var name, rest string
var num int
if len(str) < 2 || str[0] != '$' {
return num, rest, false
}
brace := false
if str[1] == '{' {
brace = true
str = str[2:]
} else {
str = str[1:]
}
i := 0
for i < len(str) {
rune, size := utf8.DecodeRuneInString(str[i:])
if !unicode.IsDigit(rune) {
break
}
i += size
}
if i == 0 {
// empty name is not okay
return num, rest, false
}
name = str[:i]
if brace {
if i >= len(str) || str[i] != '}' {
// missing closing brace
return num, rest, false
}
i++
}
// Parse number.
num = 0
for i := 0; i < len(name); i++ {
if name[i] < '0' || '9' < name[i] || num >= 1e8 {
num = -1
break
}
num = num*10 + int(name[i]) - '0'
}
// Disallow leading zeros.
if name[0] == '0' && len(name) > 1 {
num = -1
}
rest = str[i:]
return num, rest, true
}
// Replace runs the compiled pattern on the given fixture, and then replaces any instance of $n (or ${n}) in the template with the nth capture group
func (g *Glob) Replace(fixture, template string) string {
result := ""
match := g.Capture(fixture)
for len(template) > 0 {
i := strings.Index(template, "$")
if i < 0 {
break
}
result += template[:i]
template = template[i:]
if len(template) > 1 && template[1] == '$' {
// Treat $$ as $.
result += "$"
template = template[2:]
continue
}
num, rest, ok := extract(template)
if !ok {
// Malformed; treat $ as raw text.
result += "$"
template = template[1:]
continue
}
template = rest
if num < len(match) {
result += match[num]
}
}
result += template
return result
}
// QuoteMeta returns a string that quotes all glob pattern meta characters
// inside the argument text; For example, QuoteMeta(`*(foo*)`) returns `\*\(foo\*\)`.
func QuoteMeta(s string) string {
b := make([]byte, 2*len(s))
// a byte loop is correct because all meta characters are ASCII
j := 0
for i := 0; i < len(s); i++ {
if syntax.Special(s[i]) {
b[j] = '\\'
j++
}
b[j] = s[i]
j++
}
return string(b[0:j])
}