-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathother.go
More file actions
71 lines (56 loc) · 1.36 KB
/
other.go
File metadata and controls
71 lines (56 loc) · 1.36 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
package regex
// Match returns true if a []byte matches a regex
func (reg *Regexp) Match(buf []byte) bool {
return reg.RE.Match(buf)
}
// MatchStr returns true if a []byte matches a regex
func (reg *Regexp) MatchStr(buf string) bool {
return reg.RE.MatchString(buf)
}
// Split splits a string, and keeps capture groups
//
// Similar to JavaScript .split(/re/)
func (reg *Regexp) Split(buf []byte) [][]byte {
ind := reg.RE.FindAllIndex(buf, -1)
res := [][]byte{}
trim := 0
for _, pos := range ind {
v := buf[pos[0]:pos[1]]
m := reg.RE.FindSubmatch(v)
if trim == 0 {
res = append(res, buf[:pos[0]])
} else {
res = append(res, buf[trim:pos[0]])
}
trim = pos[1]
for i := 1; i <= len(m)-1; i++ {
res = append(res, m[i])
}
}
res = append(res, buf[trim:])
return res
}
// SplitStr splits a string, and keeps capture groups
//
// Similar to JavaScript .split(/re/)
func (reg *Regexp) SplitStr(str string) []string {
buf := []byte(str)
ind := reg.RE.FindAllIndex(buf, -1)
res := []string{}
trim := 0
for _, pos := range ind {
v := buf[pos[0]:pos[1]]
m := reg.RE.FindSubmatch(v)
if trim == 0 {
res = append(res, string(buf[:pos[0]]))
} else {
res = append(res, string(buf[trim:pos[0]]))
}
trim = pos[1]
for i := 1; i <= len(m)-1; i++ {
res = append(res, string(m[i]))
}
}
res = append(res, string(buf[trim:]))
return res
}