-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfnmatch.go
245 lines (203 loc) · 4.59 KB
/
fnmatch.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package fnmatch
import (
"strings"
"unicode"
"unicode/utf8"
)
// Constants ...
const (
NoEscape = 1 << iota
Pathname
Period
LeadingDir
CaseFold
)
// Searches a string for an ASCII char and returns its offset or -1
func strchr(s string, c rune) int {
for i, sc := range s {
if sc == c {
return i
}
}
return -1
}
// Unpacks a rune from string and advances the string pointer
func unpackRune(s *string) rune {
r, size := utf8.DecodeRuneInString(*s)
*s = (*s)[size:]
return r
}
// Match ...
func Match(pattern string, str string, flags ...int) bool {
combinedFlags := 0
for _, flag := range flags {
combinedFlags ^= flag
}
// Flags
flagNoEscape := combinedFlags&NoEscape != 0
flagPathname := combinedFlags&Pathname != 0
flagPeriod := combinedFlags&Period != 0
flagLeadingDir := combinedFlags&LeadingDir != 0
flagCaseFold := combinedFlags&CaseFold != 0
// Unpacks a rune from `str` and keeps some info about previous rune and string position
strPrevAtStart := true
strCurrAtStart := true
strPrevUnpacked := rune(0)
strCurrUnpacked := rune(0)
unpackStr := func() rune {
strPrevAtStart = strCurrAtStart
strCurrAtStart = false
strPrevUnpacked = strCurrUnpacked
strCurrUnpacked = unpackRune(&str)
return strCurrUnpacked
}
for len(pattern) > 0 {
patternChar := unpackRune(&pattern)
switch patternChar {
case '?':
// Any char
if len(str) == 0 {
return false
}
strChar := unpackStr()
if strChar == '/' && flagPathname {
return false
}
if strChar == '.' && flagPeriod && (strPrevAtStart || (flagPathname && strPrevUnpacked == '/')) {
return false
}
case '*':
// Collapse multiple "*"
for len(pattern) > 0 && pattern[0] == '*' {
pattern = pattern[1:]
}
if len(str) > 0 && str[0] == '.' && flagPeriod &&
(strCurrAtStart || (flagPathname && strCurrUnpacked == '/')) {
return false
}
// "*" at the end
if len(pattern) == 0 {
if flagPathname {
if flagLeadingDir || !strings.ContainsRune(str, '/') {
return true
}
return false
}
return true
}
// "*/"
if pattern[0] == '/' && flagPathname {
offset := strchr(str, '/')
if offset == -1 {
return false
}
str = str[offset:]
unpackStr()
pattern = pattern[1:]
break
}
// General case - recurse
for test := str; len(test) > 0; unpackRune(&test) {
if Match(pattern, test, (combinedFlags & ^Period)) {
return true
}
if flagPathname && test[0] == '/' {
break
}
}
return false
case '[':
// Range
if len(str) == 0 {
return false
}
if flagPathname && str[0] == '/' {
return false
}
strChar := unpackStr()
if !matchRange(&pattern, strChar, combinedFlags) {
return false
}
case '\\':
// Escape char
if !flagNoEscape {
if len(pattern) > 0 {
patternChar = unpackRune(&pattern)
}
}
fallthrough
default:
if len(str) == 0 {
return false
}
strChar := unpackStr()
switch {
case strChar == patternChar:
case flagCaseFold && unicode.ToLower(strChar) == unicode.ToLower(patternChar):
default:
return false
}
}
}
return len(str) == 0 || (flagLeadingDir && str[0] == '/')
}
func matchRange(pattern *string, test rune, flags int) bool {
if len(*pattern) == 0 {
return false
}
flagNoEscape := flags&NoEscape != 0
flagCaseFold := flags&CaseFold != 0
if flagCaseFold {
test = unicode.ToLower(test)
}
var negate, matched bool
if (*pattern)[0] == '^' || (*pattern)[0] == '!' {
negate = true
(*pattern) = (*pattern)[1:]
}
for !matched && len(*pattern) > 1 && (*pattern)[0] != ']' {
c := unpackRune(pattern)
if !flagNoEscape && c == '\\' {
if len(*pattern) > 1 {
c = unpackRune(pattern)
} else {
return false
}
}
if flagCaseFold {
c = unicode.ToLower(c)
}
if (*pattern)[0] == '-' && len(*pattern) > 1 && (*pattern)[1] != ']' {
unpackRune(pattern) // skip the -
c2 := unpackRune(pattern)
if !flagNoEscape && c2 == '\\' {
if len(*pattern) > 0 {
c2 = unpackRune(pattern)
} else {
return false
}
}
if flagCaseFold {
c2 = unicode.ToLower(c2)
}
// This really should be more intelligent, but it looks like
// fnmatch.c does simple int comparisons, therefore we will as well
if c <= test && test <= c2 {
matched = true
}
} else if c == test {
matched = true
}
}
// Skip past the rest of the pattern
ok := false
for !ok && len(*pattern) > 0 {
c := unpackRune(pattern)
if c == '\\' && len(*pattern) > 0 {
unpackRune(pattern)
} else if c == ']' {
ok = true
}
}
return ok && matched != negate
}