-
Notifications
You must be signed in to change notification settings - Fork 32
/
fnmatch.go
213 lines (173 loc) · 4.47 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
package editorconfig
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
// findLeftBrackets matches the opening left bracket {.
findLeftBrackets = regexp.MustCompile(`(^|[^\\])\{`)
// findDoubleLeftBrackets matches the duplicated opening left bracket {{.
findDoubleLeftBrackets = regexp.MustCompile(`(^|[^\\])\{\{`)
// findLeftBrackets matches the closing right bracket {.
findRightBrackets = regexp.MustCompile(`(^|[^\\])\}`)
// findDoubleRightBrackets matches the duplicated opening left bracket {{.
findDoubleRightBrackets = regexp.MustCompile(`(^|[^\\])\}\}`)
// findNumericRange matches a range of number, e.g. -2..5.
findNumericRange = regexp.MustCompile(`^([+-]?\d+)\.\.([+-]?\d+)$`)
)
// FnmatchCase tests whether the name matches the given pattern case included.
func FnmatchCase(pattern, name string) (bool, error) {
p := translate(pattern)
r, err := regexp.Compile(fmt.Sprintf("^%s$", p))
if err != nil {
return false, fmt.Errorf("error compiling %q: %w", pattern, err)
}
return r.MatchString(name), nil
}
func translate(pattern string) string { //nolint:funlen,gocognit,gocyclo,cyclop,maintidx
index := 0
pat := []rune(pattern)
length := len(pat)
result := strings.Builder{}
braceLevel := 0
isEscaped := false
inBrackets := false
// Double left and right is a hack to pass the core-test suite.
left := len(findLeftBrackets.FindAllString(pattern, -1))
doubleLeft := len(findDoubleLeftBrackets.FindAllString(pattern, -1))
right := len(findRightBrackets.FindAllString(pattern, -1))
doubleRight := len(findDoubleRightBrackets.FindAllString(pattern, -1))
matchesBraces := left+doubleLeft == right+doubleRight
pathSeparator := "/"
for index < length {
r := pat[index]
index++
switch r {
case '*':
p := index
if p < length && pat[p] == '*' {
result.WriteString(".*")
index++
} else {
result.WriteString(fmt.Sprintf("[^%s]*", pathSeparator))
}
case '/':
p := index
if p+2 < length && pat[p] == '*' && pat[p+1] == '*' && pat[p+2] == '/' {
result.WriteString(fmt.Sprintf("(?:%s|%s.*%s)", pathSeparator, pathSeparator, pathSeparator))
index += 3
} else {
result.WriteRune(r)
}
case '?':
result.WriteString(fmt.Sprintf("[^%s]", pathSeparator))
case '[':
if inBrackets { //nolint:nestif
result.WriteString("\\[")
} else {
hasSlash := false
res := strings.Builder{}
p := index
for p < length {
if pat[p] == ']' && pat[p-1] != '\\' {
break
}
res.WriteRune(pat[p])
if pat[p] == '/' && pat[p-1] != '\\' {
hasSlash = true
break
}
p++
}
if hasSlash {
result.WriteString("\\[" + res.String())
index = p + 1
} else {
if index < length && pat[index] == '!' || pat[index] == '^' {
result.WriteString("[^")
index++
} else {
result.WriteRune('[')
}
inBrackets = true
}
}
case ']':
if inBrackets && pat[index-2] == '\\' {
result.WriteString("\\]")
} else {
result.WriteRune(r)
inBrackets = false
}
case '{':
hasComma := false
p := index
res := strings.Builder{}
for p < length {
if pat[p] == '}' && pat[p-1] != '\\' {
break
}
res.WriteRune(pat[p])
if pat[p] == ',' && pat[p-1] != '\\' {
hasComma = true
break
}
p++
}
switch {
case !hasComma && p < length:
inner := res.String()
sub := findNumericRange.FindStringSubmatch(inner)
if len(sub) == 3 {
from, _ := strconv.Atoi(sub[1])
to, _ := strconv.Atoi(sub[2])
result.WriteString("(?:")
// XXX does not scale well
for i := from; i < to; i++ {
result.WriteString(strconv.Itoa(i))
result.WriteRune('|')
}
result.WriteString(strconv.Itoa(to))
result.WriteRune(')')
} else {
r := translate(inner)
result.WriteString(fmt.Sprintf("\\{%s\\}", r))
}
index = p + 1
case matchesBraces:
result.WriteString("(?:")
braceLevel++
default:
result.WriteString("\\{")
}
case '}':
if braceLevel > 0 {
if isEscaped {
result.WriteRune('}')
isEscaped = false
} else {
result.WriteRune(')')
braceLevel--
}
} else {
result.WriteString("\\}")
}
case ',':
if braceLevel == 0 || isEscaped {
result.WriteRune(r)
} else {
result.WriteRune('|')
}
default:
if r != '\\' || isEscaped {
result.WriteString(regexp.QuoteMeta(string(r)))
isEscaped = false
} else {
isEscaped = true
}
}
}
return result.String()
}