-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.go
More file actions
176 lines (139 loc) · 3.97 KB
/
regex.go
File metadata and controls
176 lines (139 loc) · 3.97 KB
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
package regex
import (
"regexp"
"strconv"
"time"
"github.com/tkdeng/regex/common"
)
type Regexp struct {
RE *regexp.Regexp
len int64
}
var reg_comment = regexp.MustCompile(`\(\?#.*?\)`)
var reg_param_quote = regexp.MustCompile(`\\?%([0-9]|\{[0-9]+\})|\\[\\']`)
var reg_escapeParam = regexp.MustCompile(`[\\%]`)
var cache common.CacheMap[*Regexp] = common.NewCache[*Regexp]()
func init() {
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)
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)
}
}
}()
}
//* regex compile methods
// this method compiles the RE string to add more functionality to it
func compRE(re string, params []string) string {
reB := []byte(re)
reB = reg_comment.ReplaceAllLiteral(reB, []byte{})
reB = reg_param_quote.ReplaceAllFunc(reB, func(b []byte) []byte {
if b[0] == '\\' {
if b[1] == '\'' {
return []byte{'`'}
} else if b[1] == '%' {
return b[1:]
}
return b
}
if b[0] == '%' {
b = b[1:]
if len(b) > 1 {
b = b[1 : len(b)-1]
}
if i, err := strconv.Atoi(string(b)); err == nil {
if i < 1 || i > len(params) {
return []byte{}
}
return []byte(regexp.QuoteMeta(params[i-1]))
}
}
return []byte{}
})
return string(reB)
}
// 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 := regexp.MustCompile(re)
compReg := Regexp{RE: reg, len: int64(len(re))}
cache.Set(re, &compReg, nil)
return &compReg
}
// 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 := regexp.Compile(re)
if err != nil {
cache.Set(re, nil, err)
return &Regexp{}, err
}
compReg := Regexp{RE: reg, len: int64(len(re))}
cache.Set(re, &compReg, nil)
return &compReg, nil
}
//* other regex methods
// Escape will escape regex special chars
func Escape(re string) string {
re = regexp.QuoteMeta(re)
re = reg_escapeParam.ReplaceAllString(re, `\$0`)
return re
}
// 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 := regexp.Compile(re); 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 ...any) []byte {
return common.JoinBytes(bytes...)
}