-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.go
More file actions
719 lines (588 loc) · 16.8 KB
/
pattern.go
File metadata and controls
719 lines (588 loc) · 16.8 KB
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WoozyMasta
// Source: github.com/woozymasta/pathrules
package pathrules
import (
"fmt"
"regexp"
"strings"
)
// compiledRule is matcher-internal compiled representation of one rule.
type compiledRule struct {
// componentRE matches basename/component patterns without slash in source.
componentRE *regexp.Regexp
// componentExact matches basename/component patterns without glob meta.
componentExact string
// componentGlob matches component patterns with "*" and "?" without regexp.
componentGlob segmentPattern
// pathExact matches full path patterns without glob meta.
pathExact string
// pathSegments matches slash patterns without "**" and char-classes.
pathSegments []segmentPattern
// pathPrefixSegments matches slash patterns with trailing "/**".
pathPrefixSegments []segmentPattern
// pathRE matches full path patterns.
pathRE *regexp.Regexp
// pathDirRE matches full path patterns targeting a directory subtree.
pathDirRE *regexp.Regexp
// source is original source rule.
source Rule
// anchored means source pattern starts with "/".
anchored bool
// dirOnly means source pattern ends with "/".
dirOnly bool
// hasSlash means source pattern contains "/" after normalization.
hasSlash bool
}
// segmentPattern is precompiled component/path segment matcher.
type segmentPattern struct {
// text is raw segment pattern source.
text string
// wildcard reports whether text contains "*" or "?".
wildcard bool
}
// compileRule compiles one source rule into the cheapest matching strategy
// that preserves expected gitignore-like semantics.
func compileRule(rule Rule, caseInsensitive bool) (*compiledRule, error) {
if !rule.Action.valid() {
return nil, fmt.Errorf("%w: unsupported action %d", ErrInvalidRule, rule.Action)
}
pattern := normalizePattern(rule.Pattern)
if caseInsensitive {
pattern = asciiLower(pattern)
}
if pattern == "" {
return nil, fmt.Errorf("%w: empty", ErrInvalidPattern)
}
cr := &compiledRule{
source: rule,
anchored: strings.HasPrefix(pattern, "/"),
dirOnly: strings.HasSuffix(pattern, "/"),
}
pattern = strings.TrimPrefix(pattern, "/")
pattern = strings.TrimSuffix(pattern, "/")
pattern = strings.Trim(pattern, "/")
if pattern == "" {
return nil, fmt.Errorf("%w: empty after normalization (%q)", ErrInvalidPattern, rule.Pattern)
}
// Anchored patterns ("/name") must be matched against full path from root
// even when they do not contain an explicit slash after normalization.
cr.hasSlash = strings.Contains(pattern, "/") || cr.anchored
hasMeta := patternHasGlobMeta(pattern)
hasCharClass := patternHasCharClass(pattern)
if !cr.hasSlash {
// Component-only rules can avoid regexp completely for exact and simple wildcard cases.
if !hasMeta {
cr.componentExact = pattern
return cr, nil
}
if !hasCharClass {
cr.componentGlob = newSegmentPattern(pattern)
return cr, nil
}
re, err := regexp.Compile("^" + globToRegexComponent(pattern) + "$")
if err != nil {
return nil, fmt.Errorf("%w: compile component %q: %v", ErrInvalidPattern, rule.Pattern, err)
}
cr.componentRE = re
return cr, nil
}
// Path rules get similar fast paths first: exact match, then segmented wildcard matching.
if !hasMeta {
cr.pathExact = pattern
return cr, nil
}
if prefix, ok := strings.CutSuffix(pattern, "/**"); ok {
// Trailing "/**" is common and can be matched as "prefix directory + any descendants".
if prefix != "" && canUseSimplePathSegments(prefix) {
cr.pathPrefixSegments = compilePathSegments(prefix)
return cr, nil
}
}
if canUseSimplePathSegments(pattern) {
cr.pathSegments = compilePathSegments(pattern)
return cr, nil
}
// Fallback for patterns with char classes or complex "**" combinations.
body := globToRegexPath(pattern)
prefix := `(?:^|.*/)`
if cr.anchored {
prefix = `^`
}
if cr.dirOnly {
re, err := regexp.Compile(prefix + body + `(?:/.*)?$`)
if err != nil {
return nil, fmt.Errorf("%w: compile dir pattern %q: %v", ErrInvalidPattern, rule.Pattern, err)
}
cr.pathDirRE = re
return cr, nil
}
re, err := regexp.Compile(prefix + body + `$`)
if err != nil {
return nil, fmt.Errorf("%w: compile path pattern %q: %v", ErrInvalidPattern, rule.Pattern, err)
}
cr.pathRE = re
return cr, nil
}
// matches reports whether compiled rule matches normalized candidate path.
func (r *compiledRule) matches(candidate string, isDir bool) bool {
if candidate == "" {
return false
}
if r.hasSlash {
// Path strategy priority mirrors compile-time selection: exact -> fast segmented -> regexp.
if r.pathExact != "" {
return matchExactPathRule(r.pathExact, candidate, isDir, r.anchored, r.dirOnly)
}
if len(r.pathPrefixSegments) > 0 {
return matchPathPrefixDoubleStar(r.pathPrefixSegments, candidate, r.anchored)
}
if len(r.pathSegments) > 0 {
return matchPathSegments(r.pathSegments, candidate, r.anchored, r.dirOnly)
}
if r.dirOnly {
return r.pathDirRE != nil && r.pathDirRE.MatchString(candidate)
}
return r.pathRE != nil && r.pathRE.MatchString(candidate)
}
// Component strategy priority mirrors compile-time selection too.
if r.componentExact != "" {
if !r.dirOnly {
return pathBase(candidate) == r.componentExact
}
return matchDirOnlyComponentExact(r.componentExact, candidate, isDir)
}
if r.componentGlob.text != "" {
if !r.dirOnly {
return matchSegmentPattern(r.componentGlob, pathBase(candidate))
}
return matchDirOnlyComponentPattern(r.componentGlob, candidate, isDir)
}
if r.componentRE == nil {
return false
}
if !r.dirOnly {
return r.componentRE.MatchString(pathBase(candidate))
}
return matchDirOnlyComponent(r.componentRE, candidate, isDir)
}
// patternHasGlobMeta reports whether pattern contains supported glob meta.
func patternHasGlobMeta(pattern string) bool {
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '*', '?':
return true
case '[':
if findCharClassEnd(pattern, i) >= 0 {
return true
}
}
}
return false
}
// patternHasCharClass reports whether pattern contains at least one valid "[...]" class.
func patternHasCharClass(pattern string) bool {
for i := 0; i < len(pattern); i++ {
if pattern[i] != '[' {
continue
}
if findCharClassEnd(pattern, i) >= 0 {
return true
}
}
return false
}
// canUseSimplePathSegments reports whether slash pattern can use lightweight segment matching.
func canUseSimplePathSegments(pattern string) bool {
if pattern == "" {
return false
}
if strings.Contains(pattern, "**") {
return false
}
return !patternHasCharClass(pattern)
}
// newSegmentPattern precompiles one segment pattern.
func newSegmentPattern(pattern string) segmentPattern {
return segmentPattern{
text: pattern,
wildcard: strings.ContainsAny(pattern, "*?"),
}
}
// compilePathSegments precompiles slash-separated path pattern segments.
func compilePathSegments(pattern string) []segmentPattern {
segments := make([]segmentPattern, 0, strings.Count(pattern, "/")+1)
start := 0
for i := 0; i <= len(pattern); i++ {
if i != len(pattern) && pattern[i] != '/' {
continue
}
segments = append(segments, newSegmentPattern(pattern[start:i]))
start = i + 1
}
return segments
}
// matchSegmentPattern matches one precompiled segment pattern.
func matchSegmentPattern(pattern segmentPattern, segment string) bool {
if !pattern.wildcard {
return segment == pattern.text
}
return matchSimpleWildcard(pattern.text, segment)
}
// matchSimpleWildcard matches "*" and "?" wildcard pattern against one segment.
func matchSimpleWildcard(pattern string, input string) bool {
pIdx := 0
sIdx := 0
starPattern := -1
starInput := 0
for sIdx < len(input) {
if pIdx < len(pattern) && (pattern[pIdx] == '?' || pattern[pIdx] == input[sIdx]) {
pIdx++
sIdx++
continue
}
if pIdx < len(pattern) && pattern[pIdx] == '*' {
// Remember star position and continue greedily from current input index.
starPattern = pIdx
pIdx++
starInput = sIdx
continue
}
if starPattern >= 0 {
// Mismatch after a previous star: backtrack pattern to token after '*'
// and let '*' consume one more input byte.
pIdx = starPattern + 1
starInput++
sIdx = starInput
continue
}
return false
}
for pIdx < len(pattern) && pattern[pIdx] == '*' {
pIdx++
}
return pIdx == len(pattern)
}
// matchPathSegments matches slash patterns without "**" and char-classes.
func matchPathSegments(pattern []segmentPattern, candidate string, anchored bool, dirOnly bool) bool {
if len(pattern) == 0 || candidate == "" {
return false
}
if anchored {
end, ok := matchPathSegmentsAt(pattern, candidate, 0)
if !ok {
return false
}
if dirOnly {
return end == len(candidate) || (end < len(candidate) && candidate[end] == '/')
}
return end == len(candidate)
}
return matchPathSegmentsUnanchored(pattern, candidate, dirOnly)
}
// matchPathSegmentsUnanchored matches unanchored path segments from any segment boundary.
func matchPathSegmentsUnanchored(pattern []segmentPattern, candidate string, dirOnly bool) bool {
for start := 0; ; {
end, ok := matchPathSegmentsAt(pattern, candidate, start)
if ok {
if dirOnly {
if end == len(candidate) || (end < len(candidate) && candidate[end] == '/') {
return true
}
} else if end == len(candidate) {
return true
}
}
nextSlash := strings.IndexByte(candidate[start:], '/')
if nextSlash < 0 {
return false
}
// Shift to next segment boundary and retry, emulating "(^|.*/)" prefix.
start += nextSlash + 1
}
}
// matchPathSegmentsAt matches precompiled path segments starting at candidate boundary index.
func matchPathSegmentsAt(pattern []segmentPattern, candidate string, start int) (int, bool) {
if start < 0 || start >= len(candidate) {
return 0, false
}
index := start
for seg := range pattern {
end := index
for end < len(candidate) && candidate[end] != '/' {
end++
}
if end == index {
return 0, false
}
if !matchSegmentPattern(pattern[seg], candidate[index:end]) {
return 0, false
}
index = end
if seg == len(pattern)-1 {
// Return end position to let caller validate terminal constraints
// (full match vs directory-subtree match).
return index, true
}
if index >= len(candidate) || candidate[index] != '/' {
return 0, false
}
index++
}
return index, true
}
// matchPathPrefixDoubleStar matches path pattern with trailing "/**".
func matchPathPrefixDoubleStar(prefix []segmentPattern, candidate string, anchored bool) bool {
if len(prefix) == 0 || candidate == "" {
return false
}
if anchored {
end, ok := matchPathSegmentsAt(prefix, candidate, 0)
// "/prefix/**" should match descendants only; exact directory alone does not match.
return ok && end < len(candidate) && candidate[end] == '/'
}
for start := 0; ; {
end, ok := matchPathSegmentsAt(prefix, candidate, start)
if ok && end < len(candidate) && candidate[end] == '/' {
return true
}
nextSlash := strings.IndexByte(candidate[start:], '/')
if nextSlash < 0 {
return false
}
start += nextSlash + 1
}
}
// matchExactPathRule matches slash-containing literal pattern without regexp.
func matchExactPathRule(pattern string, candidate string, isDir bool, anchored bool, dirOnly bool) bool {
if pattern == "" || candidate == "" {
return false
}
if anchored {
if !dirOnly {
return candidate == pattern
}
return candidate == pattern || strings.HasPrefix(candidate, pattern+"/")
}
if !dirOnly {
return candidate == pattern || strings.HasSuffix(candidate, "/"+pattern)
}
return containsDirPath(pattern, candidate, isDir)
}
// containsDirPath reports whether candidate contains pattern as directory path segment.
func containsDirPath(pattern string, candidate string, isDir bool) bool {
for start := 0; start < len(candidate); {
idx := strings.Index(candidate[start:], pattern)
if idx < 0 {
return false
}
idx += start
beforeOK := idx == 0 || candidate[idx-1] == '/'
after := idx + len(pattern)
afterOK := after == len(candidate) || candidate[after] == '/'
if beforeOK && afterOK {
if after < len(candidate) {
return true
}
if isDir {
return true
}
}
start = idx + 1
}
return false
}
// matchDirOnlyComponentExact matches dir-only component literal without regexp.
func matchDirOnlyComponentExact(component string, candidate string, isDir bool) bool {
if component == "" || candidate == "" {
return false
}
start := 0
for i := 0; i <= len(candidate); i++ {
if i != len(candidate) && candidate[i] != '/' {
continue
}
if i > start {
// For file paths, skip the last component (basename).
if i == len(candidate) && !isDir {
return false
}
if candidate[start:i] == component {
return true
}
}
start = i + 1
}
return false
}
// matchDirOnlyComponentPattern matches dir-only component wildcard pattern without regexp.
func matchDirOnlyComponentPattern(pattern segmentPattern, candidate string, isDir bool) bool {
if pattern.text == "" || candidate == "" {
return false
}
start := 0
for i := 0; i <= len(candidate); i++ {
if i != len(candidate) && candidate[i] != '/' {
continue
}
if i > start {
// For file paths, skip the last component (basename).
if i == len(candidate) && !isDir {
return false
}
if matchSegmentPattern(pattern, candidate[start:i]) {
return true
}
}
start = i + 1
}
return false
}
// globToRegexComponent converts a gitignore-like component pattern to regex body.
func globToRegexComponent(pat string) string {
var b strings.Builder
for i := 0; i < len(pat); i++ {
if next, ok := appendCharClassRegex(pat, i, &b); ok {
i = next
continue
}
c := pat[i]
switch c {
case '*':
// Treat ** as * when matching a single path component.
if i+1 < len(pat) && pat[i+1] == '*' {
i++
}
b.WriteString(`[^/]*`)
case '?':
b.WriteString(`[^/]`)
default:
b.WriteString(regexEscapeByte(c))
}
}
return b.String()
}
// globToRegexPath converts a gitignore-like path pattern to regex body.
func globToRegexPath(pat string) string {
var b strings.Builder
for i := 0; i < len(pat); i++ {
// Handle "**/" so it can match zero or more directories.
if pat[i] == '*' && i+2 < len(pat) && pat[i+1] == '*' && pat[i+2] == '/' {
b.WriteString(`(?:.*/)?`)
i += 2
continue
}
if next, ok := appendCharClassRegex(pat, i, &b); ok {
i = next
continue
}
c := pat[i]
switch c {
case '*':
if i+1 < len(pat) && pat[i+1] == '*' {
b.WriteString(`.*`)
i++
continue
}
b.WriteString(`[^/]*`)
case '?':
b.WriteString(`[^/]`)
default:
b.WriteString(regexEscapeByte(c))
}
}
return b.String()
}
// appendCharClassRegex appends a parsed glob char class (`[...]`) as regex class.
func appendCharClassRegex(pat string, start int, b *strings.Builder) (int, bool) {
if start < 0 || start >= len(pat) || pat[start] != '[' {
return start, false
}
end := findCharClassEnd(pat, start)
if end < 0 {
return start, false
}
b.WriteByte('[')
idx := start + 1
if idx < end && pat[idx] == '!' {
// gitignore-style class negation "[!x]" maps to regex "[^x]".
b.WriteByte('^')
idx++
} else if idx < end && pat[idx] == '^' {
// Literal leading '^' must be escaped in regex char class.
b.WriteString(`\^`)
idx++
}
if idx < end && pat[idx] == ']' {
// Leading ']' is treated as literal in both glob and regex classes.
b.WriteByte(']')
idx++
}
for ; idx < end; idx++ {
if pat[idx] == '\\' {
b.WriteString(`\\`)
continue
}
b.WriteByte(pat[idx])
}
b.WriteByte(']')
return end, true
}
// findCharClassEnd locates closing bracket for a glob char class.
func findCharClassEnd(pat string, start int) int {
if start < 0 || start >= len(pat) || pat[start] != '[' {
return -1
}
idx := start + 1
if idx < len(pat) && (pat[idx] == '!' || pat[idx] == '^') {
idx++
}
if idx < len(pat) && pat[idx] == ']' {
idx++
}
for ; idx < len(pat); idx++ {
if pat[idx] == ']' {
return idx
}
}
return -1
}
// regexEscapeByte escapes one byte for regexp source.
func regexEscapeByte(c byte) string {
switch c {
case '.', '+', '(', ')', '|', '{', '}', '[', ']', '^', '$', '\\':
return `\` + string(c)
default:
return string(c)
}
}
// pathBase returns final path component using slash separator.
func pathBase(path string) string {
if i := strings.LastIndexByte(path, '/'); i >= 0 {
return path[i+1:]
}
return path
}
// matchDirOnlyComponent matches component-based dir-only rule without allocating split slices.
func matchDirOnlyComponent(re *regexp.Regexp, candidate string, isDir bool) bool {
if re == nil || candidate == "" {
return false
}
start := 0
for i := 0; i <= len(candidate); i++ {
if i != len(candidate) && candidate[i] != '/' {
continue
}
if i > start {
// For file paths, skip the last component (basename).
if i == len(candidate) && !isDir {
return false
}
if re.MatchString(candidate[start:i]) {
return true
}
}
start = i + 1
}
return false
}