forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
matchers.go
247 lines (199 loc) · 4.78 KB
/
matchers.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
package match
import (
"bytes"
"fmt"
"reflect"
"strings"
"unsafe"
)
type equalsMatcher struct {
s string
bs []byte
}
type substringMatcher struct {
s string
bs []byte
}
type altSubstringMatcher struct {
literals [][]byte
}
type oneOfMatcher struct {
literals [][]byte
}
type prefixMatcher struct {
s []byte
}
type altPrefixMatcher struct {
literals [][]byte
}
type prefixNumDate struct {
minLen int
prefix []byte
digits []int
seps [][]byte
suffix []byte
}
type emptyStringMatcher struct{}
type emptyWhiteStringMatcher struct{}
type matchAny struct{}
func (m *equalsMatcher) MatchString(s string) bool {
return m.s == s
}
func (m *equalsMatcher) Match(bs []byte) bool {
return bytes.Equal(bs, m.bs)
}
func (m *equalsMatcher) String() string {
return fmt.Sprintf("<string '%v'>", m.s)
}
func (m *substringMatcher) MatchString(s string) bool {
return strings.Contains(s, m.s)
}
func (m *substringMatcher) Match(bs []byte) bool {
return bytes.Contains(bs, m.bs)
}
func (m *substringMatcher) String() string {
return fmt.Sprintf("<substring '%v'>", m.s)
}
func (m *altSubstringMatcher) MatchString(s string) bool {
return m.Match(stringToBytes(s))
}
func (m *altSubstringMatcher) Match(in []byte) bool {
for _, literal := range m.literals {
if bytes.Contains(in, literal) {
return true
}
}
return false
}
func (m *altSubstringMatcher) String() string {
return fmt.Sprintf("<alt substring '%s'>", bytes.Join(m.literals, []byte(",")))
}
func (m *oneOfMatcher) MatchString(s string) bool {
return m.Match(stringToBytes(s))
}
func (m *oneOfMatcher) Match(in []byte) bool {
for _, literal := range m.literals {
if bytes.Equal(in, literal) {
return true
}
}
return false
}
func (m *oneOfMatcher) String() string {
return fmt.Sprintf("<one of '%s'>", bytes.Join(m.literals, []byte(",")))
}
func (m *prefixMatcher) MatchString(s string) bool {
return len(s) >= len(m.s) && s[0:len(m.s)] == string(m.s)
}
func (m *prefixMatcher) Match(bs []byte) bool {
return len(bs) >= len(m.s) && bytes.Equal(bs[0:len(m.s)], m.s)
}
func (m *prefixMatcher) String() string {
return fmt.Sprintf("<prefix string '%v'>", string(m.s))
}
func (m *altPrefixMatcher) MatchString(in string) bool {
for _, s := range m.literals {
if len(in) >= len(s) && in[0:len(s)] == string(s) {
return true
}
}
return false
}
func (m *altPrefixMatcher) Match(bs []byte) bool {
for _, s := range m.literals {
if len(bs) >= len(s) && bytes.Equal(bs[0:len(s)], s) {
return true
}
}
return false
}
func (m *altPrefixMatcher) String() string {
return fmt.Sprintf("<alt prefix string '%s'>", bytes.Join(m.literals, []byte(",")))
}
func (m *prefixNumDate) MatchString(in string) bool {
return m.Match(stringToBytes(in))
}
func (m *prefixNumDate) Match(in []byte) bool {
if len(in) < m.minLen {
return false
}
pos := 0
if m.prefix != nil {
end := len(m.prefix)
if !bytes.Equal(in[0:end], m.prefix) {
return false
}
pos += end
}
for cnt := m.digits[0]; cnt > 0; cnt-- {
v := in[pos]
pos++
if !('0' <= v && v <= '9') {
return false
}
}
for i := 1; i < len(m.digits); i++ {
sep := m.seps[i-1]
if !bytes.Equal(in[pos:pos+len(sep)], sep) {
return false
}
pos += len(sep)
for cnt := m.digits[i]; cnt > 0; cnt-- {
v := in[pos]
pos++
if !('0' <= v && v <= '9') {
return false
}
}
}
if sfx := m.suffix; len(sfx) > 0 {
if !bytes.HasPrefix(in[pos:], sfx) {
return false
}
}
return true
}
func (m *prefixNumDate) String() string {
return "<prefix num date>"
}
func (m *emptyStringMatcher) MatchString(s string) bool {
return len(s) == 0
}
func (m *emptyStringMatcher) Match(bs []byte) bool {
return len(bs) == 0
}
func (m *emptyStringMatcher) String() string {
return "<empty>"
}
func (m *emptyWhiteStringMatcher) MatchString(s string) bool {
for _, r := range s {
if !(r == 0x9 || r == 0xa || r == 0xc || r == 0xd || r == 0x20 || r == '\t') {
return false
}
}
return true
}
func (m *emptyWhiteStringMatcher) Match(bs []byte) bool {
for _, r := range bytesToString(bs) {
if !(r == 0x9 || r == 0xa || r == 0xc || r == 0xd || r == 0x20 || r == '\t') {
return false
}
}
return true
}
func (m *emptyWhiteStringMatcher) String() string {
return "<empty whitespace>"
}
func (m *matchAny) Match(_ []byte) bool { return true }
func (m *matchAny) MatchString(_ string) bool { return true }
func (m *matchAny) String() string { return "<any>" }
func bytesToString(b []byte) string {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := reflect.StringHeader{Data: bh.Data, Len: bh.Len}
return *(*string)(unsafe.Pointer(&sh))
}
func stringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len}
return *(*[]byte)(unsafe.Pointer(&bh))
}