/
match.go
147 lines (109 loc) · 3.15 KB
/
match.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
package strings
import (
"regexp"
"strings"
"github.com/tobgu/qframe/qerrors"
)
type Matcher interface {
Matches(s string) bool
}
type CIStringMatcher struct {
matchString string //nolint:structcheck
buf []byte //nolint:structcheck
}
type CIPrefixMatcher CIStringMatcher
func (m *CIPrefixMatcher) Matches(s string) bool {
return strings.HasPrefix(ToUpper(&m.buf, s), m.matchString)
}
type CISuffixMatcher CIStringMatcher
func (m *CISuffixMatcher) Matches(s string) bool {
return strings.HasSuffix(ToUpper(&m.buf, s), m.matchString)
}
type CIContainsMatcher CIStringMatcher
func (m *CIContainsMatcher) Matches(s string) bool {
return strings.Contains(ToUpper(&m.buf, s), m.matchString)
}
type CIExactMatcher CIStringMatcher
func (m *CIExactMatcher) Matches(s string) bool {
return ToUpper(&m.buf, s) == m.matchString
}
type StringMatcher struct {
matchString string //nolint:structcheck
}
type PrefixMatcher StringMatcher
func (m *PrefixMatcher) Matches(s string) bool {
return strings.HasPrefix(s, m.matchString)
}
type SuffixMatcher StringMatcher
func (m *SuffixMatcher) Matches(s string) bool {
return strings.HasSuffix(s, m.matchString)
}
type ContainsMatcher StringMatcher
func (m *ContainsMatcher) Matches(s string) bool {
return strings.Contains(s, m.matchString)
}
type ExactMatcher StringMatcher
func (m *ExactMatcher) Matches(s string) bool {
return s == m.matchString
}
type RegexpMatcher struct {
r *regexp.Regexp
}
func (m *RegexpMatcher) Matches(s string) bool {
return m.r.MatchString(s)
}
func trimPercent(s string) string {
s = strings.TrimPrefix(s, "%")
s = strings.TrimSuffix(s, "%")
return s
}
func NewMatcher(comparatee string, caseSensitive bool) (Matcher, error) {
fuzzyStart := strings.HasPrefix(comparatee, "%")
fuzzyEnd := strings.HasSuffix(comparatee, "%")
if regexp.QuoteMeta(comparatee) != comparatee {
// There are regex characters in the match string
if !fuzzyStart {
comparatee = "^" + comparatee
} else {
comparatee = comparatee[1:]
}
if !fuzzyEnd {
comparatee = comparatee + "$"
} else {
comparatee = comparatee[:len(comparatee)-1]
}
if !caseSensitive {
comparatee = "(?i)" + comparatee
}
r, err := regexp.Compile(comparatee)
if err != nil {
return nil, qerrors.Propagate("string like", err)
}
return &RegexpMatcher{r: r}, nil
}
if !caseSensitive {
comparatee = strings.ToUpper(comparatee)
// Initial size, this will grow if needed
buf := make([]byte, 10)
if fuzzyStart && fuzzyEnd {
return &CIContainsMatcher{matchString: trimPercent(comparatee), buf: buf}, nil
}
if fuzzyStart {
return &CISuffixMatcher{matchString: trimPercent(comparatee), buf: buf}, nil
}
if fuzzyEnd {
return &CIPrefixMatcher{matchString: trimPercent(comparatee), buf: buf}, nil
}
return &CIExactMatcher{matchString: comparatee, buf: buf}, nil
}
if fuzzyStart && fuzzyEnd {
return &ContainsMatcher{matchString: trimPercent(comparatee)}, nil
}
if fuzzyStart {
return &SuffixMatcher{matchString: trimPercent(comparatee)}, nil
}
if fuzzyEnd {
return &PrefixMatcher{matchString: trimPercent(comparatee)}, nil
}
return &ExactMatcher{matchString: comparatee}, nil
}