-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
206 lines (159 loc) · 4 KB
/
common.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
package easyregexp
import (
"io"
"io/ioutil"
"os"
"regexp"
"sort"
"sync"
)
//max number of cached *EasyRegexp
const MaxCompiledRegex = 1024
var mutex sync.Mutex
var compiledRegex map[string]*EasyRegexp
func init() {
compiledRegex = map[string]*EasyRegexp{}
}
//contain pattern string and count
type EasyRegexp struct {
pattern string
r *regexp.Regexp
count uint
}
func NewEasyRegexp(pattern string) *EasyRegexp {
er, ok := compiledRegex[pattern]
if !ok {
// 检查是否超过 MaxCompiledRegex,如果超过,就把调用次数最少的清理掉
mutex.Lock()
defer mutex.Unlock()
if len(compiledRegex) > MaxCompiledRegex {
ers := make([]*EasyRegexp, len(compiledRegex))
i := 0
for _, v := range compiledRegex {
ers[i] = v
i++
}
sort.Slice(ers, func(i int, j int) bool {
return ers[i].count < ers[j].count
})
delete(compiledRegex, ers[0].pattern)
}
er = &EasyRegexp{
pattern, regexp.MustCompile(pattern), 0,
}
compiledRegex[pattern] = er
}
return er
}
func FMatch(pattern string, filepath string) bool {
return NewEasyRegexp(pattern).FMatch(filepath)
}
func (er *EasyRegexp) FMatch(filepath string) bool {
return er.Match(openfile(filepath))
}
func Match(pattern string, i interface{}) bool {
return NewEasyRegexp(pattern).Match(i)
}
func (er *EasyRegexp) Match(i interface{}) bool {
er.count++
s := toString(i)
return er.r.MatchString(s)
}
func FCatch(pattern string, filepath string) []string {
return NewEasyRegexp(pattern).FCatch(filepath)
}
func (er *EasyRegexp) FCatch(filepath string) []string {
return er.Catch(openfile(filepath))
}
func Catch(pattern string, i interface{}) []string {
return NewEasyRegexp(pattern).Catch(i)
}
func (er *EasyRegexp) Catch(i interface{}) []string {
er.count++
s := toString(i)
var result []string
catchedString := er.r.FindStringSubmatch(s)
if len(catchedString) == 0 {
result = catchedString
} else {
result = catchedString[1:]
}
return result
}
func FCatchAll(pattern string, filepath string) [][]string {
return NewEasyRegexp(pattern).FCatchAll(filepath)
}
func (er *EasyRegexp) FCatchAll(filepath string) [][]string {
return er.CatchAll(openfile(filepath))
}
func CatchAll(pattern string, i interface{}) [][]string {
return NewEasyRegexp(pattern).CatchAll(i)
}
func (er *EasyRegexp) CatchAll(i interface{}) [][]string {
er.count++
s := toString(i)
var result [][]string
allCatchedString := er.r.FindAllStringSubmatch(s, -1)
if len(allCatchedString) == 0 {
result = allCatchedString
} else {
result = make([][]string, len(allCatchedString))
for i, v := range allCatchedString {
result[i] = v[1:]
}
}
return result
}
func FReplaceAll(pattern string, filepath string, repl string) string {
return NewEasyRegexp(pattern).FReplaceAll(filepath, repl)
}
func (er *EasyRegexp) FReplaceAll(filepath string, repl string) string {
return er.ReplaceAll(openfile(filepath), repl)
}
func ReplaceAll(pattern string, i interface{}, repl string) string {
return NewEasyRegexp(pattern).ReplaceAll(i, repl)
}
func (er *EasyRegexp) ReplaceAll(i interface{}, repl string) string {
er.count++
s := toString(i)
return er.r.ReplaceAllString(s, repl)
}
func FSplit(pattern string, filepath string) []string {
return NewEasyRegexp(pattern).FSplit(filepath)
}
func (er *EasyRegexp) FSplit(filepath string) []string {
return er.Split(openfile(filepath))
}
func Split(pattern string, i interface{}) []string {
return NewEasyRegexp(pattern).Split(i)
}
func (er *EasyRegexp) Split(i interface{}) []string {
er.count++
s := toString(i)
return er.r.Split(s, -1)
}
func toString(i interface{}) string {
var result string
switch a := i.(type) {
case string:
result = a
case []byte:
result = string(a)
case io.Reader:
tmp, err := ioutil.ReadAll(a)
if err != nil {
panic(err)
}
result = string(tmp)
default:
panic("only support string/[]byte/io.Reader")
}
return result
}
func openfile(filepath string) io.Reader {
r, err := os.Open(filepath)
if err != nil {
panic(err)
}
return r
}