-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.go
98 lines (88 loc) · 2.32 KB
/
pattern.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
package main
import (
"errors"
"fmt"
"regexp"
"strings"
)
// Pattern is a file matching pattern than optionally can also specify
// substitutions.
type Pattern struct {
Expression *regexp.Regexp
Subtitution string
}
// UnmarshalText unmarshals the given text as a patter in p.
func (p *Pattern) UnmarshalText(text []byte) error {
re := string(text)
// Interpret special values as no-ops
if re == "" || re == "_" {
p.Expression = nil
p.Subtitution = ""
return nil
}
// If the expression contains a forward slash, interpret it as a delimiter
// between the expression and the substitution
sub := ""
if pair := strings.SplitN(re, "/", 2); len(pair) == 2 {
re = pair[0]
sub = pair[1]
if sub == "" {
return errors.New("empty substitution provided in pattern")
}
}
exp, err := compileRegex(re)
if err != nil {
return err
}
p.Expression = exp
p.Subtitution = sub
return nil
}
// String returns a string representation of the pattern.
func (p Pattern) String() string {
if p.Expression == nil {
return "*"
}
if p.Subtitution == "" {
return fmt.Sprintf("%s", p.Expression)
}
return fmt.Sprintf("%s / %s", p.Expression, p.Subtitution)
}
// ApplyPattern selects an appropriate pattern for the given traversal depth,
// applies it to value, returned the result of the match.
//
// If the selected pattern supplies a substitution, it is applied and the
// substituted value is returned. Otherwise, the original value is returned.
func ApplyPattern(patterns []Pattern, depth int, value string) (result Match, newValue string) {
if depth >= len(patterns) {
return NoPattern, value
}
pattern := patterns[depth]
if pattern.Expression == nil {
if pattern.Subtitution != "" {
panic("unexpected substitution with nil pattern expression")
}
return Matched, value
}
if pattern.Expression.MatchString(value) {
if pattern.Subtitution == "" {
return Matched, value
}
return Matched, pattern.Expression.ReplaceAllString(value, pattern.Subtitution)
}
return NotMatched, value
}
func compileRegex(re string) (*regexp.Regexp, error) {
if re == "" {
return nil, nil
}
// Force case-insensitive matching
if !strings.HasPrefix(re, "(?i)") {
re = "(?i)" + re
}
c, err := regexp.Compile(re)
if err != nil {
return nil, fmt.Errorf("unable to compile regular expression \"%s\": %v", re, err)
}
return c, nil
}