-
Notifications
You must be signed in to change notification settings - Fork 0
/
regext.go
292 lines (262 loc) · 6.64 KB
/
regext.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
package regext
import (
"bytes"
"fmt"
"regexp"
)
//Regext is the main struct
type Regext struct {
Dataset [][]byte
source []byte
stack [][]byte
}
//NewRegext gets the instance of Regext.
func NewRegext(data []byte) *Regext {
var newSet [][]byte
newSet = append(newSet, data)
return &Regext{Dataset: newSet, source: data, stack: newSet}
}
/*FindLast TODO: Doesn't work just yet....*/
func (r *Regext) FindLast(regex string) *Regext {
return r.Find(regex, -1, 1)
}
//FindFirst is a wrapper of Find to shorthand the skip and count
func (r *Regext) FindFirst(regex string) *Regext {
return r.Find(regex, 0, 1)
}
//FindAll is a wrapper of Find to shorthand the skip and count
func (r *Regext) FindAll(regex string) *Regext {
return r.Find(regex, 0, -1)
}
//Find or one of it's equivelant functions must be called to load the dataset
func (r *Regext) Find(regex string, skip int, count int) *Regext {
re := regexp.MustCompile(regex)
findAll := -1
if skip <= 0 && count > 0 {
findAll = count
}
var newDataset [][]byte
for _, ds := range r.Dataset {
res := re.FindAll(ds, findAll)
//Check for bounds
if count >= len(res) || count <= 0 {
count = len(res)
}
if skip <= 0 {
skip = 0
}
//Build new result set in order.
for _, newAppend := range res[skip:count] {
newDataset = append(newDataset, newAppend)
}
r.Dataset = newDataset
}
return r
}
//ReplaceAll will replace any match with the value supplied.
func (r *Regext) ReplaceAll(regex string, value string) *Regext {
re := regexp.MustCompile(regex)
var newDataset [][]byte
for _, ds := range r.Dataset {
res := re.ReplaceAll(ds, []byte(value))
newDataset = append(newDataset, res)
}
r.Dataset = newDataset
return r
}
//FilterOut will remove anything in the dataset that matches the regex
func (r *Regext) FilterOut(regexs ...string) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
pass := false
for _, regex := range regexs {
re := regexp.MustCompile(regex)
if !re.Match(ds) {
pass = true
break
}
}
if pass {
newDataset = append(newDataset, ds)
}
}
r.Dataset = newDataset
return r
}
//FilterOutAny - filters out anything that matches in the list
func (r *Regext) FilterOutAny(regexs ...string) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
found := true
for _, regex := range regexs {
re := regexp.MustCompile(regex)
if re.Match(ds) {
found = false
break
}
}
if found {
newDataset = append(newDataset, ds)
}
}
r.Dataset = newDataset
return r
}
//Filter will keep anything in the dataset that matches the regex
func (r *Regext) Filter(regexs ...string) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
pass := true
for _, regex := range regexs {
re := regexp.MustCompile(regex)
if !re.Match(ds) {
pass = false
break
}
}
if pass {
newDataset = append(newDataset, ds)
}
}
r.Dataset = newDataset
return r
}
//FilterAny will take all of the items and filter all of them
func (r *Regext) FilterAny(regexs ...string) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
for _, regex := range regexs {
re := regexp.MustCompile(regex)
if re.Match(ds) {
newDataset = append(newDataset, ds)
break
}
}
}
r.Dataset = newDataset
return r
}
//FilterByLen - will filter by the length of the byte
func (r *Regext) FilterByLen(min int, max int) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
dslen := len(ds)
if (dslen >= min || dslen < 0) && (dslen <= max || dslen < 0) {
newDataset = append(newDataset, ds)
}
}
r.Dataset = newDataset
return r
}
//FilterOutByLen - will filter by the length of the byte
func (r *Regext) FilterOutByLen(min int, max int) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
dslen := len(ds)
if max < 0 {
max = min
}
if (dslen < min) || (dslen > max) {
newDataset = append(newDataset, ds)
}
}
r.Dataset = newDataset
return r
}
//Trim - trims whitespace
func (r *Regext) Trim() *Regext {
return r.DeleteAny(`^[\s\r\n]+`, `[\s\r\n]+$`)
}
//DeleteAny - filters out anything that matches in the list
func (r *Regext) DeleteAny(regexs ...string) *Regext {
var newDataset [][]byte
for _, ds := range r.Dataset {
for _, regex := range regexs {
re := regexp.MustCompile(regex)
ds = re.ReplaceAll(ds, []byte(""))
}
newDataset = append(newDataset, ds)
}
r.Dataset = newDataset
return r
}
//Split - splits the results based upon a regex found.
func (r *Regext) Split(regexs ...string) *Regext {
for _, regex := range regexs {
var newDataset [][]byte
for _, ds := range r.Dataset {
re := regexp.MustCompile(regex)
split := re.Split(string(ds[:len(ds)]), -1)
for _, sp := range split {
newDataset = append(newDataset, []byte(sp))
}
}
r.Dataset = newDataset
}
return r
}
//SplitIgnoreBlanks - same as split, but ignores blanks.
func (r *Regext) SplitIgnoreBlanks(regexs ...string) *Regext {
for _, regex := range regexs {
var newDataset [][]byte
for _, ds := range r.Dataset {
re := regexp.MustCompile(regex)
split := re.Split(string(ds[:len(ds)]), -1)
for _, sp := range split {
if sp != "" {
newDataset = append(newDataset, []byte(sp))
}
}
}
r.Dataset = newDataset
}
return r
}
//String - Returns an array of strings
func (r *Regext) String() string {
bytesJoined := bytes.Join(r.Dataset, []byte(""))
return string(bytesJoined[:len(bytesJoined)])
}
//JoinStr - Joins the entries together using a string separator
func (r *Regext) JoinStr(sep string) *Regext {
return r.Join([]byte(sep))
}
//Join - Joins the entries together
func (r *Regext) Join(sep []byte) *Regext {
var newDataset [][]byte
r.Dataset = append(newDataset, bytes.Join(r.Dataset, sep))
return r
}
//ToLower - Sets all of the entries to lowercase
func (r *Regext) ToLower() *Regext {
for i, ds := range r.Dataset {
r.Dataset[i] = bytes.ToLower(ds)
}
return r
}
//ToUpper - Sets all of the entries to uppercase
func (r *Regext) ToUpper() *Regext {
for i, ds := range r.Dataset {
r.Dataset[i] = bytes.ToUpper(ds)
}
return r
}
//PrintRaw is a quick and dirty print to the console mostly for debug
func (r *Regext) PrintRaw() *Regext {
r.Println("", "")
return r
}
//Print prints the current dataset to the console without new line
func (r *Regext) Print(prefix string, postfix string) *Regext {
fmt.Printf("%s%q%s", prefix, r.Dataset, postfix)
return r
}
//Println prints the current dataset to the console
func (r *Regext) Println(prefix string, postfix string) *Regext {
fmt.Printf("%s%q%s\n", prefix, r.Dataset, postfix)
return r
}
//GetData returns the data, breaking the chain.
func (r *Regext) GetData() [][]byte {
return r.Dataset
}