-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.go
314 lines (258 loc) · 8 KB
/
regex.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package regex
import (
"bytes"
"regexp"
"sort"
"strconv"
"time"
"github.com/GRbit/go-pcre"
"github.com/tkdeng/goregex/common"
)
type PCRE pcre.Regexp
type RE2 *regexp.Regexp
type Regexp struct {
RE pcre.Regexp
len int64
}
type RegexpRE2 struct {
RE *regexp.Regexp
len int64
}
type bgPart struct {
ref []byte
b []byte
}
var regCompCommentAndChars *regexp.Regexp = regexp.MustCompile(`(\\|)\(\?#.*?\)|%!|!%|\\[\\']`)
var regCompParam *regexp.Regexp = regexp.MustCompile(`(\\|)%(\{[0-9]+\}|[0-9])`)
var regCompBG *regexp.Regexp = regexp.MustCompile(`\[^?(\\[\\\]]|[^\]])+\]`)
var regCompBGRef *regexp.Regexp = regexp.MustCompile(`%!([0-9]+|o|c)!%`)
var regComplexSel *Regexp
var regEscape *Regexp
var cache common.CacheMap[*Regexp] = common.NewCache[*Regexp]()
var cacheRE2 common.CacheMap[*RegexpRE2] = common.NewCache[*RegexpRE2]()
var compCache common.CacheMap[[]byte] = common.NewCache[[]byte]()
func init() {
regComplexSel = Comp(`(\\|)\$([0-9]|\{[0-9]+\})`)
regEscape = Comp(`[\\\^\$\.\|\?\*\+\(\)\[\]\{\}\%]`)
go func() {
for {
time.Sleep(10 * time.Minute)
// default: remove cache items have not been accessed in over 2 hours
cacheTime := 2 * time.Hour
// SysFreeMemory returns the total free system memory in megabytes
mb := common.SysFreeMemory()
if mb < 200 && mb != 0 {
// low memory: remove cache items have not been accessed in over 10 minutes
cacheTime = 10 * time.Minute
} else if mb < 500 && mb != 0 {
// low memory: remove cache items have not been accessed in over 30 minutes
cacheTime = 30 * time.Minute
} else if mb < 2000 && mb != 0 {
// low memory: remove cache items have not been accessed in over 1 hour
cacheTime = 1 * time.Hour
} else if mb > 64000 {
// high memory: remove cache items have not been accessed in over 12 hour
cacheTime = 12 * time.Hour
} else if mb > 32000 {
// high memory: remove cache items have not been accessed in over 6 hour
cacheTime = 6 * time.Hour
} else if mb > 16000 {
// high memory: remove cache items have not been accessed in over 3 hour
cacheTime = 3 * time.Hour
}
cache.DelOld(cacheTime)
cacheRE2.DelOld(cacheTime)
compCache.DelOld(cacheTime)
time.Sleep(10 * time.Second)
// clear cache if were still critically low on available memory
if mb := common.SysFreeMemory(); mb < 10 && mb != 0 {
cache.DelOld(0)
cacheRE2.DelOld(0)
compCache.DelOld(0)
}
}
}()
}
// this method compiles the RE string to add more functionality to it
func compRE(re string, params []string) string {
if val, err := compCache.Get(re); val != nil || err != nil {
if err != nil {
return ""
}
return string(regCompParam.ReplaceAllFunc(val, func(b []byte) []byte {
if b[1] == '{' && b[len(b)-1] == '}' {
b = b[2 : len(b)-1]
} else {
b = b[1:]
}
if n, e := strconv.Atoi(string(b)); e == nil && n > 0 && n <= len(params) {
return []byte(Escape(params[n-1]))
}
return []byte{}
}))
}
reB := []byte(re)
reB = regCompCommentAndChars.ReplaceAllFunc(reB, func(b []byte) []byte {
if bytes.Equal(b, []byte("%!")) {
return []byte("%!o!%")
} else if bytes.Equal(b, []byte("!%")) {
return []byte("%!c!%")
} else if b[0] == '\\' {
if b[1] == '\'' {
return []byte{'`'}
}
return b
}
return []byte{}
})
bgList := [][]byte{}
reB = regCompBG.ReplaceAllFunc(reB, func(b []byte) []byte {
bgList = append(bgList, b)
return common.JoinBytes('%', '!', len(bgList)-1, '!', '%')
})
for ind, bgItem := range bgList {
charS := []byte{'['}
if bgItem[1] == '^' {
bgItem = bgItem[2 : len(bgItem)-1]
charS = append(charS, '^')
} else {
bgItem = bgItem[1 : len(bgItem)-1]
}
newBG := []bgPart{}
for i := 0; i < len(bgItem); i++ {
if i+1 < len(bgItem) {
if bgItem[i] == '\\' {
newBG = append(newBG, bgPart{ref: []byte{bgItem[i+1]}, b: []byte{bgItem[i], bgItem[i+1]}})
i++
continue
} else if bgItem[i+1] == '-' && i+2 < len(bgItem) {
newBG = append(newBG, bgPart{ref: []byte{bgItem[i], bgItem[i+2]}, b: []byte{bgItem[i], bgItem[i+1], bgItem[i+2]}})
i += 2
continue
}
}
newBG = append(newBG, bgPart{ref: []byte{bgItem[i]}, b: []byte{bgItem[i]}})
}
sort.Slice(newBG, func(i, j int) bool {
if len(newBG[i].ref) > len(newBG[j].ref) {
return true
} else if len(newBG[i].ref) < len(newBG[j].ref) {
return false
}
for k := 0; k < len(newBG[i].ref); k++ {
if newBG[i].ref[k] < newBG[j].ref[k] {
return true
} else if newBG[i].ref[k] > newBG[j].ref[k] {
return false
}
}
return false
})
bgItem = charS
for i := 0; i < len(newBG); i++ {
bgItem = append(bgItem, newBG[i].b...)
}
bgItem = append(bgItem, ']')
bgList[ind] = bgItem
}
reB = regCompBGRef.ReplaceAllFunc(reB, func(b []byte) []byte {
b = b[2 : len(b)-2]
if b[0] == 'o' {
return []byte(`%!`)
} else if b[0] == 'c' {
return []byte(`!%`)
}
if n, e := strconv.Atoi(string(b)); e == nil && n < len(bgList) {
return bgList[n]
}
return []byte{}
})
compCache.Set(re, reB, nil)
return string(regCompParam.ReplaceAllFunc(reB, func(b []byte) []byte {
if b[1] == '{' && b[len(b)-1] == '}' {
b = b[2 : len(b)-1]
} else {
b = b[1:]
}
if n, e := strconv.Atoi(string(b)); e == nil && n > 0 && n <= len(params) {
return []byte(Escape(params[n-1]))
}
return []byte{}
}))
}
//* regex compile methods
// Comp compiles a regular expression and store it in the cache
func Comp(re string, params ...string) *Regexp {
re = compRE(re, params)
if val, err := cache.Get(re); val != nil || err != nil {
if err != nil {
panic(err)
}
return val
}
reg := pcre.MustCompile(re, pcre.UTF8)
// commented below methods compiled 10000 times in 0.1s (above method being used finished in half of that time)
// reg := pcre.MustCompileParse(re)
// reg := pcre.MustCompileJIT(re, pcre.UTF8, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileJIT(re, pcre.EXTRA, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)
compRe := Regexp{RE: reg, len: int64(len(re))}
cache.Set(re, &compRe, nil)
return &compRe
}
// CompTry tries to compile or returns an error
func CompTry(re string, params ...string) (*Regexp, error) {
re = compRE(re, params)
if val, err := cache.Get(re); val != nil || err != nil {
if err != nil {
return &Regexp{}, err
}
return val, nil
}
reg, err := pcre.Compile(re, pcre.UTF8)
if err != nil {
cache.Set(re, nil, err)
return &Regexp{}, err
}
// commented below methods compiled 10000 times in 0.1s (above method being used finished in half of that time)
// reg := pcre.MustCompileParse(re)
// reg := pcre.MustCompileJIT(re, pcre.UTF8, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileJIT(re, pcre.EXTRA, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileJIT(re, pcre.JAVASCRIPT_COMPAT, pcre.STUDY_JIT_COMPILE)
// reg := pcre.MustCompileParseJIT(re, pcre.STUDY_JIT_COMPILE)
compRe := Regexp{RE: reg, len: int64(len(re))}
cache.Set(re, &compRe, nil)
return &compRe, nil
}
//* other regex methods
// Escape will escape regex special chars
func Escape(re string) string {
return string(regEscape.RepStr([]byte(re), []byte(`\$1`)))
}
// IsValid will return true if a regex is valid and can be compiled by this module
func IsValid(re string) bool {
re = compRE(re, []string{})
if _, err := pcre.Compile(re, pcre.UTF8); err == nil {
return true
}
return false
}
// IsValidPCRE will return true if a regex is valid and can be compiled by the PCRE module
func IsValidPCRE(re string) bool {
if _, err := pcre.Compile(re, pcre.UTF8); err == nil {
return true
}
return false
}
// IsValidRE2 will return true if a regex is valid and can be compiled by the builtin RE2 module
func IsValidRE2(re string) bool {
if _, err := regexp.Compile(re); err == nil {
return true
}
return false
}
// JoinBytes is an easy way to join multiple values into a single []byte
func JoinBytes(bytes ...interface{}) []byte {
return common.JoinBytes(bytes...)
}