@@ -286,7 +286,7 @@ func (re *Regexp) FindAll(b []byte, n int) [][]byte {
286
286
287
287
var matches [][]byte
288
288
289
- re .findAll (& alloc , cs , n , func (match []int ) {
289
+ re .findAll (& alloc , b , "" , cs , n , func (match []int ) {
290
290
matches = append (matches , matchedBytes (b , match ))
291
291
})
292
292
@@ -305,7 +305,7 @@ func (re *Regexp) FindAllIndex(b []byte, n int) [][]int {
305
305
306
306
var matches [][]int
307
307
308
- re .findAll (& alloc , cs , n , func (match []int ) {
308
+ re .findAll (& alloc , b , "" , cs , n , func (match []int ) {
309
309
matches = append (matches , append ([]int (nil ), match ... ))
310
310
})
311
311
@@ -326,7 +326,7 @@ func (re *Regexp) FindAllString(s string, n int) []string {
326
326
327
327
var matches []string
328
328
329
- re .findAll (& alloc , cs , n , func (match []int ) {
329
+ re .findAll (& alloc , nil , s , cs , n , func (match []int ) {
330
330
matches = append (matches , matchedString (s , match ))
331
331
})
332
332
@@ -345,7 +345,7 @@ func (re *Regexp) FindAllStringIndex(s string, n int) [][]int {
345
345
346
346
var matches [][]int
347
347
348
- re .findAll (& alloc , cs , n , func (match []int ) {
348
+ re .findAll (& alloc , nil , s , cs , n , func (match []int ) {
349
349
matches = append (matches , append ([]int (nil ), match ... ))
350
350
})
351
351
@@ -354,7 +354,7 @@ func (re *Regexp) FindAllStringIndex(s string, n int) [][]int {
354
354
return res
355
355
}
356
356
357
- func (re * Regexp ) findAll (alloc * allocation , cs cString , n int , deliver func (match []int )) {
357
+ func (re * Regexp ) findAll (alloc * allocation , bsrc [] byte , src string , cs cString , n int , deliver func (match []int )) {
358
358
var dstCap [2 ]int
359
359
360
360
if n < 0 {
@@ -372,24 +372,22 @@ func (re *Regexp) findAll(alloc *allocation, cs cString, n int, deliver func(mat
372
372
break
373
373
}
374
374
375
- matches := readMatch (alloc , cs , matchArr .ptr , dstCap [:0 ])
375
+ match := readMatch (alloc , cs , matchArr .ptr , dstCap [:0 ])
376
376
accept := true
377
- if matches [0 ] == matches [1 ] {
378
- // We've found an empty match.
379
- if matches [0 ] == prevMatchEnd {
380
- // We don't allow an empty match right
381
- // after a previous match, so ignore it.
382
- accept = false
383
- }
384
- pos ++
385
- } else {
386
- pos = matches [1 ]
377
+ // Check if it's an empty match following a match, which we ignore.
378
+ if match [0 ] == match [1 ] && match [0 ] == prevMatchEnd {
379
+ // We don't allow an empty match right
380
+ // after a previous match, so ignore it.
381
+ accept = false
387
382
}
383
+
384
+ pos = nextPos (bsrc , src , pos , match [1 ])
385
+
388
386
if accept {
389
- deliver (matches )
387
+ deliver (match )
390
388
count ++
391
389
}
392
- prevMatchEnd = matches [1 ]
390
+ prevMatchEnd = match [1 ]
393
391
394
392
if count == n {
395
393
break
@@ -514,23 +512,8 @@ func (re *Regexp) findAllSubmatch(alloc *allocation, bsrc []byte, src string, cs
514
512
if match [0 ] == match [1 ] && match [0 ] == prevMatchEnd {
515
513
accept = false
516
514
}
517
- // Advance past this match; always advance at least one character.
518
- var width int
519
- if bsrc != nil {
520
- _ , width = utf8 .DecodeRune (bsrc [pos :])
521
- } else {
522
- _ , width = utf8 .DecodeRuneInString (src [pos :])
523
- }
524
515
525
- if pos + width > match [1 ] {
526
- pos += width
527
- } else if pos + 1 > match [1 ] {
528
- // This clause is only needed at the end of the input
529
- // string. In that case, DecodeRuneInString returns width=0.
530
- pos ++
531
- } else {
532
- pos = match [1 ]
533
- }
516
+ pos = nextPos (bsrc , src , pos , match [1 ])
534
517
prevMatchEnd = match [1 ]
535
518
}
536
519
if accept {
@@ -1032,6 +1015,26 @@ func matchedString(s string, match []int) string {
1032
1015
return s [match [0 ]:match [1 ]]
1033
1016
}
1034
1017
1018
+ func nextPos (bsrc []byte , src string , pos int , matchEnd int ) int {
1019
+ // Advance past the match; always advance at least one character.
1020
+ var width int
1021
+ if bsrc != nil {
1022
+ _ , width = utf8 .DecodeRune (bsrc [pos :])
1023
+ } else {
1024
+ _ , width = utf8 .DecodeRuneInString (src [pos :])
1025
+ }
1026
+
1027
+ if pos + width > matchEnd {
1028
+ return pos + width
1029
+ } else if pos + 1 > matchEnd {
1030
+ // This clause is only needed at the end of the input
1031
+ // string. In that case, DecodeRuneInString returns width=0.
1032
+ return pos + 1
1033
+ } else {
1034
+ return matchEnd
1035
+ }
1036
+ }
1037
+
1035
1038
func QuoteForError (s string ) string {
1036
1039
if strconv .CanBackquote (s ) {
1037
1040
return "`" + s + "`"
0 commit comments