forked from dlclark/regexp2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd.go
More file actions
183 lines (151 loc) · 3.93 KB
/
std.go
File metadata and controls
183 lines (151 loc) · 3.93 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
177
178
179
180
181
182
183
package regexp2
func CompileStd(s string) (*Regexp, error) {
return Compile(s, RE2)
}
func MustCompileStd(s string) *Regexp {
re, err := CompileStd(s)
if err != nil {
panic(err)
}
return re
}
func MatchString(pattern, s string) (bool, error) {
re, err := CompileStd(pattern)
if err != nil {
return false, err
}
return re.MatchStringStd(s), nil
}
func (re *Regexp) MatchStringStd(s string) bool {
match, err := re.MatchString(s)
if err != nil {
panic(err)
}
return match
}
// FindAllString is the 'All' version of FindString; it returns a slice of all
// successive matches of the expression, as defined by the 'All' description
// in the package comment.
//
// A return value of nil indicates no match.
func (re *Regexp) FindAllString(s string, n int) []string {
var result []string
m, err := re.FindStringMatch(s)
if err != nil {
panic(err)
}
for m != nil {
result = append(result, m.Group.String())
m, err = re.FindNextMatch(m)
if err != nil {
panic(err)
}
}
if n > -1 {
result = result[:n]
}
return result
}
func (re *Regexp) FindAllStringMatches(s string) []*Match {
var matches []*Match
m, _ := re.FindStringMatch(s)
for m != nil {
matches = append(matches, m)
m, _ = re.FindNextMatch(m)
}
return matches
}
// FindAllStringIndex is the 'All' version of FindStringIndex; it returns a
// slice of all successive matches of the expression, as defined by the 'All'
// description in the package comment.
//
// A return value of nil indicates no match.
func (re *Regexp) FindAllStringIndex(s string, n int) [][]int {
var result [][]int
m, err := re.FindStringMatch(s)
if err != nil {
panic(err)
}
for m != nil {
result = append(result,
[]int{m.Group.Index, m.Group.Index + m.Group.Length})
m, err = re.FindNextMatch(m)
if err != nil {
panic(err)
}
}
return result
}
// FindAllStringSubmatch is the 'All' version of FindStringSubmatch; it
// returns a slice of all successive matches of the expression, as defined by
// the 'All' description in the package comment.
//
// A return value of nil indicates no match.
func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
var result [][]string
m, err := re.FindStringMatch(s)
if err != nil {
panic(err)
}
for m != nil {
m.populateOtherGroups()
subs := make([]string, 0, len(m.otherGroups)+1)
subs = append(subs, m.Group.String())
for i := 0; i < len(m.otherGroups); i++ {
subs = append(subs, (&m.otherGroups[i]).String())
}
result = append(result, subs)
m, err = re.FindNextMatch(m)
if err != nil {
panic(err)
}
}
return result
}
// FindAllStringSubmatchIndex is the 'All' version of
// FindStringSubmatchIndex; it returns a slice of all successive matches of
// the expression, as defined by the 'All' description in the package
// comment.
//
// A return value of nil indicates no match.
func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int {
var result [][]int
m, err := re.FindStringMatch(s)
if err != nil {
panic(err)
}
for m != nil {
subs := []int{m.Group.Index, m.Group.Index + m.Group.Length}
m.populateOtherGroups()
for i := 0; i < len(m.otherGroups); i++ {
g := m.otherGroups[i]
if g.Index+g.Length == 0 {
g.Index = -1
}
subs = append(subs, g.Index)
subs = append(subs, g.Index+g.Length)
}
result = append(result, subs)
m, err = re.FindNextMatch(m)
if err != nil {
panic(err)
}
}
return result
}
// SubexpNames returns the names of the parenthesized subexpressions
// in this StdRegexp. The name for the first sub-expression is names[1],
// so that if m is a match slice, the name for m[i] is SubexpNames()[i].
// Since the StdRegexp as a whole cannot be named, names[0] is always
// the empty string. The slice should not be modified.
func (re *Regexp) SubexpNames() []string {
results := []string{}
for i, s := range re.capslist {
if i == 0 {
results = append(results, "")
} else {
results = append(results, s)
}
}
return results
}