-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpattern.go
205 lines (191 loc) · 3.73 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
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
package logparser
import (
"bytes"
"crypto/md5"
"fmt"
"regexp"
"strings"
"sync"
)
const (
patternMaxWords = 100
patterMinWordLen = 2
patternMaxDiff = 1
)
var (
buffers = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
)
var (
squote = '\''
dquote = '"'
bslash = '\\'
lsbrack = '['
rsbrack = ']'
lpar = '('
rpar = ')'
lcur = '{'
rcur = '}'
hexWithPrefix = regexp.MustCompile(`^0x[a-fA-F0-9]+$`)
hex = regexp.MustCompile(`^[a-fA-F0-9]{4,}$`)
uuid = regexp.MustCompile(`^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$`)
)
type Pattern struct {
words []string
str *string
hash *string
}
func (p *Pattern) String() string {
if p.str == nil {
buf := buffers.Get().(*bytes.Buffer)
buf.Reset()
for _, w := range p.words {
if buf.Len() > 0 {
buf.WriteByte(' ')
}
buf.WriteString(w)
}
s := buf.String()
p.str = &s
buffers.Put(buf)
}
return *p.str
}
func (p *Pattern) Hash() string {
if p.hash == nil {
h := fmt.Sprintf("%x", md5.Sum([]byte(p.String())))
p.hash = &h
}
return *p.hash
}
func (p *Pattern) WeakEqual(other *Pattern) bool {
if len(p.words) != len(other.words) {
return false
}
var diffs int
for i := range other.words {
if p.words[i] != other.words[i] {
diffs++
if diffs > patternMaxDiff {
return false
}
}
}
return true
}
func NewPattern(input string) *Pattern {
pattern := &Pattern{}
buf := buffers.Get().(*bytes.Buffer)
buf.Reset()
for _, p := range strings.Fields(removeQuotedAndBrackets(input, buf)) {
p = strings.TrimRight(p, "=:],;")
if len(p) < patterMinWordLen {
continue
}
if hexWithPrefix.MatchString(p) || hex.MatchString(p) || uuid.MatchString(p) {
continue
}
p = removeDigits(p, buf)
if !isWord(p) {
continue
}
pattern.words = append(pattern.words, p)
if len(pattern.words) >= patternMaxWords {
break
}
}
buffers.Put(buf)
return pattern
}
func NewPatternFromWords(input string) *Pattern {
return &Pattern{words: strings.Split(input, " ")}
}
// like regexp match to `^[a-zA-Z][a-zA-Z._-]*[a-zA-Z]$`, but much faster
func isWord(s string) bool {
l := len(s) - 1
var firstLast int
for i, r := range s {
switch i {
case 0, l:
switch {
case r >= 'A' && r <= 'Z':
firstLast++
case r >= 'a' && r <= 'z':
firstLast++
default:
return false
}
default:
switch {
case r >= 'A' && r <= 'Z':
case r >= 'a' && r <= 'z':
case r == '.':
case r == '_':
case r == '-':
default:
return false
}
}
}
return firstLast == 2
}
func removeDigits(s string, buf *bytes.Buffer) string {
buf.Reset()
for _, r := range s {
if r >= '0' && r <= '9' {
continue
}
buf.WriteRune(r)
}
return buf.String()
}
func removeQuotedAndBrackets(s string, buf *bytes.Buffer) string {
buf.Reset()
var quote, prev rune
var seenBrackets []rune
var l int
for i, r := range s {
switch r {
case lsbrack, lpar, lcur:
if quote == 0 {
seenBrackets = append(seenBrackets, r)
}
case rsbrack:
if l = len(seenBrackets); l > 0 && seenBrackets[l-1] == lsbrack {
seenBrackets = seenBrackets[:l-1]
continue
}
case rpar:
if l = len(seenBrackets); l > 0 && seenBrackets[l-1] == lpar {
seenBrackets = seenBrackets[:l-1]
continue
}
case rcur:
if l = len(seenBrackets); l > 0 && seenBrackets[l-1] == lcur {
seenBrackets = seenBrackets[:l-1]
continue
}
case dquote, squote:
prev = 0
if i > 0 {
prev = rune(s[i-1])
}
if prev != bslash && len(seenBrackets) == 0 {
if quote == 0 {
quote = r
} else if quote == r {
quote = 0
continue
}
}
}
if quote != 0 || len(seenBrackets) > 0 {
continue
}
buf.WriteRune(r)
}
return buf.String()
}