-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
wildcard.go
555 lines (487 loc) · 12.6 KB
/
wildcard.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// The wildcard matching code in Vitess uses two different implementations for wildcard algorithms,
// as seen on https://en.wikipedia.org/wiki/Matching_wildcards
//
// The main implementation is based on the logic in INN (https://inn.eyrie.org/trac/browser/trunk/lib/uwildmat.c),
// and is originally MIT licensed. This is a recursive matching algorithm with important optimizations, as explained
// on the Wikipedia page: it is a traditional recursion algorithm with 3 return values for match, no match, and
// impossible match, which greatly stops the depth of the recursion tree. It also only tries to target the ending
// codepoint at the end of a 'star' match, which again cuts the recursion depth.
//
// In practice, this results in a very efficient algorithm which performs great in real world cases, however,
// as just explained, it DOES recurse, which may be an issue when the input pattern is complex enough to cause
// deep recursion.
//
// To prevent Vitess instances from crashing because of stack overflows, we've added a stack guard to the algorithm,
// controlled by the wildcardRecursionDepth constant. If the recursion limit is reached, the match will fail --
// potentially leading to wrong results for the algorithm.
//
// If accuracy is of upmost importance, the wildcardRecursionDepth constant can be set to 0, in which case Vitess
// will use an alternative iterative algorithm, based on a public domain algorithm by Alessandro Cantatore
// (seen in http://xoomer.virgilio.it/acantato/dev/wildcard/wildmatch.html). This algorithm is much simpler and does
// not recurse, however it is significantly slower than our recursive implementation (~25% slower in our benchmarks).
//
// Because of this, we intend to enable the recursive algorithm by default.
package collations
import (
"unicode/utf8"
"vitess.io/vitess/go/mysql/collations/charset"
)
type match byte
const (
matchOK match = iota
matchFail
matchOver
)
// wildcardRecursionDepth is the maximum amount of recursive calls that can be performed when
// matching a wildcard. If set to 0, the default wildcard matcher will use an alternative algorithm
// that does not use recursion.
const wildcardRecursionDepth = 32
// patternMatchOne is a special value for compiled patterns which matches a single char (it usually replaces '_' or '?')
const patternMatchOne = -128
// patternMatchMany is a special value for compiled pattern that matches any amount of chars (it usually replaces '%' or '*')
const patternMatchMany = -256
// nopMatcher is an implementation of WildcardPattern that never matches anything.
// It is returned when we detect that a provided wildcard pattern cannot match anything
type nopMatcher struct{}
func (nopMatcher) Match(_ []byte) bool {
return false
}
// emptyMatcher is an implementation of WildcardPattern that only matches the empty string
type emptyMatcher struct{}
func (emptyMatcher) Match(in []byte) bool {
return len(in) == 0
}
// fastMatcher is an implementation of WildcardPattern that uses a collation's Collate method
// to perform wildcard matching.
// It is returned:
// - when the wildcard pattern has no wildcard characters at all
// - when the wildcard pattern has a single '%' (patternMatchMany) and it is the very last
// character of the pattern (in this case, we set isPrefix to true to use prefix-match collation)
type fastMatcher struct {
collate func(left, right []byte, isPrefix bool) int
pattern []byte
isPrefix bool
}
func (cm *fastMatcher) Match(in []byte) bool {
return cm.collate(in, cm.pattern, cm.isPrefix) == 0
}
// unicodeWildcard is an implementation of WildcardPattern for multibyte charsets;
// it is used for all UCA collations, multibyte collations and all Unicode-based collations
type unicodeWildcard struct {
equals func(a, b rune) bool
charset charset.Charset
pattern []rune
}
func newUnicodeWildcardMatcher(
cs charset.Charset,
equals func(a rune, b rune) bool,
collate func(left []byte, right []byte, isPrefix bool) int,
pat []byte, chOne, chMany, chEsc rune,
) WildcardPattern {
var escape bool
var chOneCount, chManyCount, chEscCount int
var parsedPattern = make([]rune, 0, len(pat))
var patOriginal = pat
if chOne == 0 {
chOne = '_'
}
if chMany == 0 {
chMany = '%'
}
if chEsc == 0 {
chEsc = '\\'
}
for len(pat) > 0 {
cp, width := cs.DecodeRune(pat)
if cp == charset.RuneError && width < 3 {
return nopMatcher{}
}
pat = pat[width:]
if escape {
parsedPattern = append(parsedPattern, cp)
escape = false
continue
}
switch cp {
case chOne:
chOneCount++
parsedPattern = append(parsedPattern, patternMatchOne)
case chMany:
if len(parsedPattern) > 0 && parsedPattern[len(parsedPattern)-1] == patternMatchMany {
continue
}
chManyCount++
parsedPattern = append(parsedPattern, patternMatchMany)
case chEsc:
chEscCount++
escape = true
default:
parsedPattern = append(parsedPattern, cp)
}
}
if escape {
parsedPattern = append(parsedPattern, chEsc)
}
// if we have a collation callback, we can detect some common cases for patterns
// here and optimize them away without having to return a full WildcardPattern
if collate != nil {
if len(parsedPattern) == 0 {
return emptyMatcher{}
}
if chOneCount == 0 && chEscCount == 0 {
if chManyCount == 0 {
return &fastMatcher{
collate: collate,
pattern: patOriginal,
isPrefix: false,
}
}
if chManyCount == 1 && chMany < utf8.RuneSelf && parsedPattern[len(parsedPattern)-1] == chMany {
return &fastMatcher{
collate: collate,
pattern: patOriginal[:len(patOriginal)-1],
isPrefix: true,
}
}
}
}
return &unicodeWildcard{
equals: equals,
charset: cs,
pattern: parsedPattern,
}
}
func (wc *unicodeWildcard) matchIter(str []byte, pat []rune) bool {
var s []byte
var p []rune
var star = false
var cs = wc.charset
retry:
s = str
p = pat
for len(s) > 0 {
var p0 rune
if len(p) > 0 {
p0 = p[0]
}
switch p0 {
case patternMatchOne:
c0, width := cs.DecodeRune(s)
if c0 == charset.RuneError && width < 3 {
return false
}
s = s[width:]
case patternMatchMany:
star = true
str = s
pat = p[1:]
if len(pat) == 0 {
return true
}
goto retry
default:
c0, width := cs.DecodeRune(s)
if c0 == charset.RuneError && width < 3 {
return false
}
if !wc.equals(c0, p0) {
goto starCheck
}
s = s[width:]
}
p = p[1:]
}
return len(p) == 0 || (len(p) == 1 && p[0] == patternMatchMany)
starCheck:
if !star {
return false
}
if len(str) > 0 {
c0, width := cs.DecodeRune(str)
if c0 == charset.RuneError && width < 3 {
return false
}
str = str[width:]
}
goto retry
}
func (wc *unicodeWildcard) Match(in []byte) bool {
if wildcardRecursionDepth == 0 {
return wc.matchIter(in, wc.pattern)
}
return wc.matchRecursive(in, wc.pattern, 0) == matchOK
}
func (wc *unicodeWildcard) matchMany(in []byte, pat []rune, depth int) match {
var cs = wc.charset
var p0 rune
many:
if len(pat) == 0 {
return matchOK
}
p0 = pat[0]
pat = pat[1:]
switch p0 {
case patternMatchMany:
goto many
case patternMatchOne:
cpIn, width := cs.DecodeRune(in)
if cpIn == charset.RuneError && width < 3 {
return matchFail
}
in = in[width:]
goto many
}
if len(in) == 0 {
return matchOver
}
retry:
var width int
for len(in) > 0 {
var cpIn rune
cpIn, width = cs.DecodeRune(in)
if cpIn == charset.RuneError && width < 3 {
return matchFail
}
if wc.equals(cpIn, p0) {
break
}
in = in[width:]
}
if len(in) == 0 {
return matchOver
}
in = in[width:]
m := wc.matchRecursive(in, pat, depth+1)
if m == matchFail {
goto retry
}
return m
}
func (wc *unicodeWildcard) matchRecursive(in []byte, pat []rune, depth int) match {
if depth >= wildcardRecursionDepth {
return matchFail
}
var cs = wc.charset
for len(pat) > 0 {
if pat[0] == patternMatchMany {
return wc.matchMany(in, pat[1:], depth)
}
cpIn, width := cs.DecodeRune(in)
if cpIn == charset.RuneError && width < 3 {
return matchFail
}
switch {
case pat[0] == patternMatchOne:
case wc.equals(pat[0], cpIn):
default:
return matchFail
}
in = in[width:]
pat = pat[1:]
}
if len(in) == 0 {
return matchOK
}
return matchFail
}
// eightbitWildcard is an implementation of WildcardPattern used for 8-bit charsets.
// It is used for all 8-bit encodings.
type eightbitWildcard struct {
sort *[256]byte
pattern []int16
}
func newEightbitWildcardMatcher(
sort *[256]byte,
collate func(left []byte, right []byte, isPrefix bool) int,
pat []byte, chOneRune, chManyRune, chEscRune rune,
) WildcardPattern {
var escape bool
var parsedPattern = make([]int16, 0, len(pat))
var chOne, chMany, chEsc byte = '_', '%', '\\'
var chOneCount, chManyCount, chEscCount int
if chOneRune > 255 || chManyRune > 255 || chEscRune > 255 {
return nopMatcher{}
}
if chOneRune != 0 {
chOne = byte(chOneRune)
}
if chManyRune != 0 {
chMany = byte(chManyRune)
}
if chEscRune != 0 {
chEsc = byte(chEscRune)
}
for _, ch := range pat {
if escape {
parsedPattern = append(parsedPattern, int16(ch))
escape = false
continue
}
switch ch {
case chOne:
chOneCount++
parsedPattern = append(parsedPattern, patternMatchOne)
case chMany:
if len(parsedPattern) > 0 && parsedPattern[len(parsedPattern)-1] == patternMatchMany {
continue
}
chManyCount++
parsedPattern = append(parsedPattern, patternMatchMany)
case chEsc:
chEscCount++
escape = true
default:
parsedPattern = append(parsedPattern, int16(ch))
}
}
if escape {
parsedPattern = append(parsedPattern, int16(chEsc))
}
// if we have a collation callback, we can detect some common cases for patterns
// here and optimize them away without having to return a full WildcardPattern
if collate != nil {
if len(parsedPattern) == 0 {
return emptyMatcher{}
}
if chOneCount == 0 && chEscCount == 0 {
if chManyCount == 0 {
return &fastMatcher{
collate: collate,
pattern: pat,
isPrefix: false,
}
}
if chManyCount == 1 && pat[len(pat)-1] == chMany {
return &fastMatcher{
collate: collate,
pattern: pat[:len(pat)-1],
isPrefix: true,
}
}
}
}
return &eightbitWildcard{
sort: sort,
pattern: parsedPattern,
}
}
func (wc *eightbitWildcard) Match(in []byte) bool {
if wildcardRecursionDepth == 0 {
return wc.matchIter(in, wc.pattern)
}
return wc.matchRecursive(in, wc.pattern, 0) == matchOK
}
func (wc *eightbitWildcard) matchMany(in []byte, pat []int16, depth int) match {
var p0 int16
many:
if len(pat) == 0 {
return matchOK
}
p0 = pat[0]
pat = pat[1:]
switch p0 {
case patternMatchMany:
goto many
case patternMatchOne:
if len(in) == 0 {
return matchFail
}
in = in[1:]
goto many
}
if len(in) == 0 {
return matchOver
}
retry:
for len(in) > 0 {
if wc.sort[in[0]] == wc.sort[byte(p0)] {
break
}
in = in[1:]
}
if len(in) == 0 {
return matchOver
}
in = in[1:]
m := wc.matchRecursive(in, pat, depth+1)
if m == matchFail {
goto retry
}
return m
}
func (wc *eightbitWildcard) matchRecursive(in []byte, pat []int16, depth int) match {
if depth >= wildcardRecursionDepth {
return matchFail
}
for len(pat) > 0 {
if pat[0] == patternMatchMany {
return wc.matchMany(in, pat[1:], depth)
}
if len(in) == 0 {
return matchFail
}
switch {
case pat[0] == patternMatchOne:
case wc.sort[byte(pat[0])] == wc.sort[in[0]]:
default:
return matchFail
}
in = in[1:]
pat = pat[1:]
}
if len(in) == 0 {
return matchOK
}
return matchFail
}
func (wc *eightbitWildcard) matchIter(str []byte, pat []int16) bool {
var s []byte
var p []int16
var star = false
retry:
s = str
p = pat
for len(s) > 0 {
var p0 int16
if len(p) > 0 {
p0 = p[0]
}
switch p0 {
case patternMatchOne:
break
case patternMatchMany:
star = true
str = s
pat = p[1:]
if len(pat) == 0 {
return true
}
goto retry
default:
if wc.sort[byte(p0)] != wc.sort[s[0]] {
goto starCheck
}
}
s = s[1:]
p = p[1:]
}
return len(p) == 0 || (len(p) == 1 && p[0] == patternMatchMany)
starCheck:
if !star {
return false
}
str = str[1:]
goto retry
}