forked from go-enry/go-onigmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
288 lines (259 loc) · 8.84 KB
/
find.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
package onigmo
import (
"io"
)
// FindIndex returns a two-element slice of integers defining the location of
// the leftmost match in b of the regular expression. The match itself is at
// b[loc[0]:loc[1]]. A return value of nil indicates no match.
func (re *Regexp) FindIndex(b []byte) []int {
match := re.find(b, len(b), 0)
if len(match) == 0 {
return nil
}
return match[:2]
}
// Find returns a slice holding the text of the leftmost match in b of the
// regular expression. A return value of nil indicates no match.
func (re *Regexp) Find(b []byte) []byte {
loc := re.FindIndex(b)
if loc == nil {
return nil
}
return b[loc[0]:loc[1]:loc[1]]
}
// FindString returns a string holding the text of the leftmost match in s of
// the regular expression. If there is no match, the return value is an empty
// string, but it will also be empty if the regular expression successfully
// matches an empty string. Use FindStringIndex or FindStringSubmatch if it is
// necessary to distinguish these cases.
func (re *Regexp) FindString(s string) string {
mb := re.Find([]byte(s))
if mb == nil {
return ""
}
return string(mb)
}
// FindStringIndex returns a two-element slice of integers defining the location
// of the leftmost match in s of the regular expression. The match itself is at
// s[loc[0]:loc[1]]. A return value of nil indicates no match.
func (re *Regexp) FindStringIndex(s string) []int {
return re.FindIndex([]byte(s))
}
// FindAllIndex is the 'All' version of FindIndex; 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) FindAllIndex(b []byte, n int) [][]int {
if n < 0 {
n = len(b) + 1
}
var result [][]int
re.allMatches(b, n, func(match []int) {
if result == nil {
result = make([][]int, 0, startSize)
}
result = append(result, match[0:2])
})
return result
}
const startSize = 10 // The size at which to start a slice in the 'All' routines.
// FindAll is the 'All' version of Find; 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) FindAll(b []byte, n int) [][]byte {
if n < 0 {
n = len(b) + 1
}
var result [][]byte
re.allMatches(b, n, func(match []int) {
if result == nil {
result = make([][]byte, 0, startSize)
}
result = append(result, b[match[0]:match[1]:match[1]])
})
return result
}
// 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 {
if n < 0 {
n = len(s) + 1
}
var result []string
b := []byte(s)
re.allMatches(b, n, func(match []int) {
if result == nil {
result = make([]string, 0, startSize)
}
f := string(b[match[0]:match[1]])
result = append(result, f)
})
return result
}
// 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 {
if n < 0 {
n = len(s) + 1
}
var result [][]int
re.allMatches([]byte(s), n, func(match []int) {
if result == nil {
result = make([][]int, 0, startSize)
}
result = append(result, match[0:2])
})
return result
}
// FindSubmatchIndex returns a slice holding the index pairs identifying the
// leftmost match of the regular expression in b and the matches, if any, of its
// subexpressions, as defined by the 'Submatch' and 'Index' descriptions in the
// package comment. A return value of nil indicates no match.
func (re *Regexp) FindSubmatchIndex(b []byte) []int {
match := re.find(b, len(b), 0)
if len(match) == 0 {
return nil
}
return match
}
// FindSubmatch returns a slice of slices holding the text of the leftmost match
// of the regular expression in b and the matches, if any, of its subexpressions,
// as defined by the 'Submatch' descriptions in the package comment. A return
// value of nil indicates no match.
func (re *Regexp) FindSubmatch(b []byte) [][]byte {
a := re.FindSubmatchIndex(b)
if a == nil {
return nil
}
ret := make([][]byte, 1+re.numSubexp)
for i := range ret {
if 2*i < len(a) && a[2*i] >= 0 {
ret[i] = b[a[2*i]:a[2*i+1]:a[2*i+1]]
}
}
return ret
}
// FindStringSubmatch returns a slice of strings holding the text of the
// leftmost match of the regular expression in s and the matches, if any, of its
// subexpressions, as defined by the 'Submatch' description in the package
// comment. A return value of nil indicates no match.
func (re *Regexp) FindStringSubmatch(s string) []string {
b := []byte(s)
match := re.FindSubmatch(b)
if match == nil {
return nil
}
results := make([]string, 0, len(match))
for _, match := range match {
results = append(results, string(match))
}
return results
}
// FindStringSubmatchIndex returns a slice holding the index pairs identifying
// the leftmost match of the regular expression in s and the matches, if any, of
// its subexpressions, as defined by the 'Submatch' and 'Index' descriptions in
// the package comment. A return value of nil indicates no match.
func (re *Regexp) FindStringSubmatchIndex(s string) []int {
return re.FindSubmatchIndex([]byte(s))
}
// FindAllSubmatchIndex is the 'All' version of FindSubmatchIndex; 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) FindAllSubmatchIndex(b []byte, n int) [][]int {
if n < 0 {
n = len(b) + 1
}
var result [][]int
re.allMatches(b, n, func(match []int) {
if result == nil {
result = make([][]int, 0, startSize)
}
result = append(result, match)
})
return result
}
// FindAllSubmatch is the 'All' version of FindSubmatch; 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) FindAllSubmatch(b []byte, n int) [][][]byte {
if n < 0 {
n = len(b) + 1
}
var result [][][]byte
re.allMatches(b, n, func(match []int) {
if result == nil {
result = make([][][]byte, 0, startSize)
}
slice := make([][]byte, len(match)/2)
for j := range slice {
if match[2*j] >= 0 {
slice[j] = b[match[2*j]:match[2*j+1]:match[2*j+1]]
}
}
result = append(result, slice)
})
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 {
if n < 0 {
n = len(s) + 1
}
var result [][]string
re.allMatches([]byte(s), n, func(match []int) {
if result == nil {
result = make([][]string, 0, startSize)
}
slice := make([]string, len(match)/2)
for j := range slice {
if match[2*j] >= 0 {
slice[j] = s[match[2*j]:match[2*j+1]]
}
}
result = append(result, slice)
})
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 {
if n < 0 {
n = len(s) + 1
}
var result [][]int
re.allMatches([]byte(s), n, func(match []int) {
if result == nil {
result = make([][]int, 0, startSize)
}
result = append(result, match)
})
return result
}
// FindReaderIndex returns a two-element slice of integers defining the location
// of the leftmost match of the regular expression in text read from the
// RuneReader. The match text was found in the input stream at byte offset
// loc[0] through loc[1]-1. A return value of nil indicates no match.
//
// In contrast with the standard library implementation, the reader it's fully
// loaded in memory.
func (re *Regexp) FindReaderIndex(r io.RuneReader) []int {
b, _ := readAll(r)
return re.FindIndex(b)
}
// FindReaderSubmatchIndex returns a slice holding the index pairs identifying
// the leftmost match of the regular expression of text read by the RuneReader,
// and the matches, if any, of its subexpressions, as defined by the 'Submatch'
// and 'Index' descriptions in the package comment. A return value of nil
// indicates no match.
//
// In contrast with the standard library implementation, the reader it's fully
// loaded in memory.
func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int {
b, _ := readAll(r)
return re.FindSubmatchIndex(b)
}