forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp_api.go
104 lines (85 loc) · 3.04 KB
/
regexp_api.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
package regexp
import "github.com/kirillDanshin/go-pcre"
func (re *Regexp) FindStringIndex(s string) []int {
return re.re.FindIndex([]byte(s), pcre.CONFIG_UTF32)
}
func (re *Regexp) FindStringSubmatchIndex(s string) (indexPairs []int) {
r := re.FindAllStringIndex(s)
for _, v := range r {
indexPairs = append(indexPairs, v[0], v[1])
}
return
}
func (re *Regexp) FindAllStringSubmatchIndex(s string, _ int) [][]int {
return re.re.FindAllIndex([]byte(s), pcre.CONFIG_UTF32)
}
func (re *Regexp) FindAllSubmatchIndex(b []byte, _ int) [][]int {
result := make([][]int, len(b))
for i := 0; i < len(b); {
r := re.re.FindIndex(b[i:], 0)
if r != nil {
result = append(result, r)
if len(r) >= 2 && r[1] != 0 {
i += r[1]
continue
}
}
i++
}
return result
}
func (re *Regexp) FindAllStringIndex(str string, _ ...int) [][]int {
return re.re.FindAllIndex([]byte(str), pcre.CONFIG_UTF32)
}
// IsCaseless checks if regexp is case insensetive
func (re *Regexp) IsCaseless() bool {
return re.re.IsCaseless()
}
// IsMultiline checks if regexp is multiline
func (re *Regexp) IsMultiline() bool {
return re.re.IsMultiline()
}
// FindAllIndex returns the start and end of the first match.
func (re *Regexp) FindAllIndex(bytes []byte, flags int) [][]int {
return re.re.FindAllIndex(bytes, flags)
}
// FindIndex returns the start and end of the first match, or nil if no match.
// loc[0] is the start and loc[1] is the end.
func (re *Regexp) FindIndex(bytes []byte, flags int) []int {
return re.re.FindIndex(bytes, flags)
}
// FindString returns the start and end of the first match, or nil if no match.
// loc[0] is the start and loc[1] is the end.
func (re *Regexp) FindString(s string, flags int) string {
return re.re.FindString(s, flags)
}
// Groups returns the number of capture groups in the compiled regexp pattern.
func (re Regexp) Groups() int {
return re.re.Groups()
}
// Match tries to match the speficied byte array slice to the current pattern.
// Returns true if the match succeeds.
func (re *Regexp) Match(subject []byte, flags int) bool {
return re.re.Match(subject, flags)
}
// MatchString is same as Match, but accept string as argument
func (re *Regexp) MatchString(subject string, flags int) bool {
return re.re.MatchString(subject, flags)
}
// Matcher returns a new matcher object, with the byte array slice as a
// subject.
func (re *Regexp) Matcher(subject []byte, flags int) (m *Matcher) {
return &Matcher{re.re.Matcher(subject, flags)}
}
// MatcherString returns a new matcher object, with the specified subject string.
func (re *Regexp) MatcherString(subject string, flags int) (m *Matcher) {
return &Matcher{re.re.MatcherString(subject, flags)}
}
// ReplaceAll returns a copy of a byte slice with pattern matches replaced by repl.
func (re *Regexp) ReplaceAll(bytes, repl []byte, flags int) []byte {
return re.re.ReplaceAll(bytes, repl, flags)
}
// ReplaceAllString is same as ReplaceAll, but accept strings as arguments
func (re *Regexp) ReplaceAllString(src, repl string, flags int) string {
return re.re.ReplaceAllString(src, repl, flags)
}