forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize.go
258 lines (220 loc) · 5.23 KB
/
optimize.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
248
249
250
251
252
253
254
255
256
257
258
package match
import "regexp/syntax"
type trans func(*syntax.Regexp) (bool, *syntax.Regexp)
var transformations = []trans{
simplify,
uncapture,
trimLeft,
trimRight,
unconcat,
concatRepetition,
flattenRepetition,
}
// optimize runs minimal regular expression optimizations
// until fix-point.
func optimize(r *syntax.Regexp) *syntax.Regexp {
for {
changed := false
for _, t := range transformations {
var upd bool
upd, r = t(r)
changed = changed || upd
}
if changed == false {
return r
}
}
}
// Simplify regular expression by stdlib.
func simplify(r *syntax.Regexp) (bool, *syntax.Regexp) {
return false, r.Simplify()
}
// uncapture optimizes regular expression by removing capture groups from
// regular expression potentially allocating memory when executed.
func uncapture(r *syntax.Regexp) (bool, *syntax.Regexp) {
if r.Op == syntax.OpCapture {
// try to uncapture
if len(r.Sub) == 1 {
_, sub := uncapture(r.Sub[0])
return true, sub
}
tmp := *r
tmp.Op = syntax.OpConcat
r = &tmp
}
sub := make([]*syntax.Regexp, len(r.Sub))
modified := false
for i := range r.Sub {
var m bool
m, sub[i] = uncapture(r.Sub[i])
modified = modified || m
}
if !modified {
return false, r
}
tmp := *r
tmp.Sub = sub
return true, &tmp
}
// trimLeft removes not required '.*' from beginning of regular expressions.
func trimLeft(r *syntax.Regexp) (bool, *syntax.Regexp) {
if eqPrefixAnyRegex(r, patDotStar, patNullBeginDotStar) {
tmp := *r
tmp.Sub = tmp.Sub[1:]
return true, &tmp
}
return false, r
}
// trimRight removes not required '.*' from end of regular expressions.
func trimRight(r *syntax.Regexp) (bool, *syntax.Regexp) {
if eqSuffixAnyRegex(r, patDotStar, patNullEndDotStar) {
i := len(r.Sub) - 1
tmp := *r
tmp.Sub = tmp.Sub[0:i]
return true, &tmp
}
return false, r
}
// unconcat removes intermediate regular expression concatenations generated by
// parser if concatenation contains only 1 element. Removal of object from
// parse-tree can enable other optimization to fire.
func unconcat(r *syntax.Regexp) (bool, *syntax.Regexp) {
switch {
case r.Op == syntax.OpConcat && len(r.Sub) <= 1:
if len(r.Sub) == 1 {
return true, r.Sub[0]
}
return true, &syntax.Regexp{
Op: syntax.OpEmptyMatch,
Flags: r.Flags,
}
case r.Op == syntax.OpRepeat && r.Min == r.Max && r.Min == 1:
return true, r.Sub[0]
}
return false, r
}
// concatRepetition concatenates 2 consecutive repeated sub-patterns into a
// repetition of length 2.
func concatRepetition(r *syntax.Regexp) (bool, *syntax.Regexp) {
if r.Op != syntax.OpConcat {
// don't iterate sub-expressions if top-level is no OpConcat
return false, r
}
// check if concatenated op is already a repetition
if isConcatRepetition(r) {
return false, r
}
// concatenate repetitions in sub-expressions first
var subs []*syntax.Regexp
changed := false
for _, sub := range r.Sub {
changedSub, tmp := concatRepetition(sub)
changed = changed || changedSub
subs = append(subs, tmp)
}
var concat []*syntax.Regexp
lastMerged := -1
for i, j := 0, 1; j < len(subs); i, j = j, j+1 {
if subs[i].Op == syntax.OpRepeat && eqRegex(subs[i].Sub[0], subs[j]) {
r := subs[i]
concat = append(concat,
&syntax.Regexp{
Op: syntax.OpRepeat,
Sub: r.Sub,
Min: r.Min + 1,
Max: r.Max + 1,
Flags: r.Flags,
},
)
lastMerged = j
changed = true
j++
continue
}
if isConcatRepetition(subs[i]) && eqRegex(subs[i].Sub[0], subs[j]) {
r := subs[i]
concat = append(concat,
&syntax.Regexp{
Op: syntax.OpConcat,
Sub: append(r.Sub, r.Sub[0]),
Flags: r.Flags,
},
)
lastMerged = j
changed = true
j++
continue
}
if eqRegex(subs[i], subs[j]) {
r := subs[i]
concat = append(concat,
&syntax.Regexp{
Op: syntax.OpRepeat,
Sub: []*syntax.Regexp{r},
Min: 2,
Max: 2,
Flags: r.Flags,
},
)
lastMerged = j
changed = true
j++
continue
}
concat = append(concat, subs[i])
}
if lastMerged+1 != len(subs) {
concat = append(concat, subs[len(subs)-1])
}
r = &syntax.Regexp{
Op: syntax.OpConcat,
Sub: concat,
Flags: r.Flags,
}
return changed, r
}
// flattenRepetition flattens nested repetitions
func flattenRepetition(r *syntax.Regexp) (bool, *syntax.Regexp) {
if r.Op != syntax.OpConcat {
// don't iterate sub-expressions if top-level is no OpConcat
return false, r
}
sub := r.Sub
inRepetition := false
if isConcatRepetition(r) {
sub = sub[:1]
inRepetition = true
// create flattened regex repetition mulitplying count
// if nexted expression is also a repetition
if s := sub[0]; isConcatRepetition(s) {
count := len(s.Sub) * len(r.Sub)
return true, &syntax.Regexp{
Op: syntax.OpRepeat,
Sub: s.Sub[:1],
Min: count,
Max: count,
Flags: r.Flags | s.Flags,
}
}
}
// recursively check if we can flatten sub-expressions
changed := false
for i, s := range sub {
upd, tmp := flattenRepetition(s)
changed = changed || upd
sub[i] = tmp
}
if !changed {
return false, r
}
// fix up top-level repetition with modified one
tmp := *r
if inRepetition {
for i := range r.Sub {
tmp.Sub[i] = sub[0]
}
} else {
tmp.Sub = sub
}
return changed, &tmp
}