Source file src/regexp/regexp.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 package regexp
67
68 import (
69 "bytes"
70 "io"
71 "regexp/syntax"
72 "strconv"
73 "strings"
74 "sync"
75 "unicode"
76 "unicode/utf8"
77 )
78
79
80
81
82 type Regexp struct {
83 expr string
84 prog *syntax.Prog
85 onepass *onePassProg
86 numSubexp int
87 maxBitStateLen int
88 subexpNames []string
89 prefix string
90 prefixBytes []byte
91 prefixRune rune
92 prefixEnd uint32
93 mpool int
94 matchcap int
95 prefixComplete bool
96 cond syntax.EmptyOp
97 minInputLen int
98
99
100
101 longest bool
102 }
103
104
105 func (re *Regexp) String() string {
106 return re.expr
107 }
108
109
110
111
112
113
114
115
116
117 func (re *Regexp) Copy() *Regexp {
118 re2 := *re
119 return &re2
120 }
121
122
123
124
125
126
127
128
129
130
131
132 func Compile(expr string) (*Regexp, error) {
133 return compile(expr, syntax.Perl, false)
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 func CompilePOSIX(expr string) (*Regexp, error) {
156 return compile(expr, syntax.POSIX, true)
157 }
158
159
160
161
162
163
164
165 func (re *Regexp) Longest() {
166 re.longest = true
167 }
168
169 func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
170 re, err := syntax.Parse(expr, mode)
171 if err != nil {
172 return nil, err
173 }
174 maxCap := re.MaxCap()
175 capNames := re.CapNames()
176
177 re = re.Simplify()
178 prog, err := syntax.Compile(re)
179 if err != nil {
180 return nil, err
181 }
182 matchcap := prog.NumCap
183 if matchcap < 2 {
184 matchcap = 2
185 }
186 regexp := &Regexp{
187 expr: expr,
188 prog: prog,
189 onepass: compileOnePass(prog),
190 numSubexp: maxCap,
191 subexpNames: capNames,
192 cond: prog.StartCond(),
193 longest: longest,
194 matchcap: matchcap,
195 minInputLen: minInputLen(re),
196 }
197 if regexp.onepass == nil {
198 regexp.prefix, regexp.prefixComplete = prog.Prefix()
199 regexp.maxBitStateLen = maxBitStateLen(prog)
200 } else {
201 regexp.prefix, regexp.prefixComplete, regexp.prefixEnd = onePassPrefix(prog)
202 }
203 if regexp.prefix != "" {
204
205
206 regexp.prefixBytes = []byte(regexp.prefix)
207 regexp.prefixRune, _ = utf8.DecodeRuneInString(regexp.prefix)
208 }
209
210 n := len(prog.Inst)
211 i := 0
212 for matchSize[i] != 0 && matchSize[i] < n {
213 i++
214 }
215 regexp.mpool = i
216
217 return regexp, nil
218 }
219
220
221
222
223
224
225
226 var (
227 matchSize = [...]int{128, 512, 2048, 16384, 0}
228 matchPool [len(matchSize)]sync.Pool
229 )
230
231
232
233
234 func (re *Regexp) get() *machine {
235 m, ok := matchPool[re.mpool].Get().(*machine)
236 if !ok {
237 m = new(machine)
238 }
239 m.re = re
240 m.p = re.prog
241 if cap(m.matchcap) < re.matchcap {
242 m.matchcap = make([]int, re.matchcap)
243 for _, t := range m.pool {
244 t.cap = make([]int, re.matchcap)
245 }
246 }
247
248
249
250 n := matchSize[re.mpool]
251 if n == 0 {
252 n = len(re.prog.Inst)
253 }
254 if len(m.q0.sparse) < n {
255 m.q0 = queue{make([]uint32, n), make([]entry, 0, n)}
256 m.q1 = queue{make([]uint32, n), make([]entry, 0, n)}
257 }
258 return m
259 }
260
261
262 func (re *Regexp) put(m *machine) {
263 m.re = nil
264 m.p = nil
265 m.inputs.clear()
266 matchPool[re.mpool].Put(m)
267 }
268
269
270 func minInputLen(re *syntax.Regexp) int {
271 switch re.Op {
272 default:
273 return 0
274 case syntax.OpAnyChar, syntax.OpAnyCharNotNL, syntax.OpCharClass:
275 return 1
276 case syntax.OpLiteral:
277 l := 0
278 for _, r := range re.Rune {
279 l += utf8.RuneLen(r)
280 }
281 return l
282 case syntax.OpCapture, syntax.OpPlus:
283 return minInputLen(re.Sub[0])
284 case syntax.OpRepeat:
285 return re.Min * minInputLen(re.Sub[0])
286 case syntax.OpConcat:
287 l := 0
288 for _, sub := range re.Sub {
289 l += minInputLen(sub)
290 }
291 return l
292 case syntax.OpAlternate:
293 l := minInputLen(re.Sub[0])
294 var lnext int
295 for _, sub := range re.Sub[1:] {
296 lnext = minInputLen(sub)
297 if lnext < l {
298 l = lnext
299 }
300 }
301 return l
302 }
303 }
304
305
306
307
308 func MustCompile(str string) *Regexp {
309 regexp, err := Compile(str)
310 if err != nil {
311 panic(`regexp: Compile(` + quote(str) + `): ` + err.Error())
312 }
313 return regexp
314 }
315
316
317
318
319 func MustCompilePOSIX(str string) *Regexp {
320 regexp, err := CompilePOSIX(str)
321 if err != nil {
322 panic(`regexp: CompilePOSIX(` + quote(str) + `): ` + err.Error())
323 }
324 return regexp
325 }
326
327 func quote(s string) string {
328 if strconv.CanBackquote(s) {
329 return "`" + s + "`"
330 }
331 return strconv.Quote(s)
332 }
333
334
335 func (re *Regexp) NumSubexp() int {
336 return re.numSubexp
337 }
338
339
340
341
342
343
344 func (re *Regexp) SubexpNames() []string {
345 return re.subexpNames
346 }
347
348 const endOfText rune = -1
349
350
351
352 type input interface {
353 step(pos int) (r rune, width int)
354 canCheckPrefix() bool
355 hasPrefix(re *Regexp) bool
356 index(re *Regexp, pos int) int
357 context(pos int) lazyFlag
358 }
359
360
361 type inputString struct {
362 str string
363 }
364
365 func (i *inputString) step(pos int) (rune, int) {
366 if pos < len(i.str) {
367 c := i.str[pos]
368 if c < utf8.RuneSelf {
369 return rune(c), 1
370 }
371 return utf8.DecodeRuneInString(i.str[pos:])
372 }
373 return endOfText, 0
374 }
375
376 func (i *inputString) canCheckPrefix() bool {
377 return true
378 }
379
380 func (i *inputString) hasPrefix(re *Regexp) bool {
381 return strings.HasPrefix(i.str, re.prefix)
382 }
383
384 func (i *inputString) index(re *Regexp, pos int) int {
385 return strings.Index(i.str[pos:], re.prefix)
386 }
387
388 func (i *inputString) context(pos int) lazyFlag {
389 r1, r2 := endOfText, endOfText
390
391 if uint(pos-1) < uint(len(i.str)) {
392 r1 = rune(i.str[pos-1])
393 if r1 >= utf8.RuneSelf {
394 r1, _ = utf8.DecodeLastRuneInString(i.str[:pos])
395 }
396 }
397
398 if uint(pos) < uint(len(i.str)) {
399 r2 = rune(i.str[pos])
400 if r2 >= utf8.RuneSelf {
401 r2, _ = utf8.DecodeRuneInString(i.str[pos:])
402 }
403 }
404 return newLazyFlag(r1, r2)
405 }
406
407
408 type inputBytes struct {
409 str []byte
410 }
411
412 func (i *inputBytes) step(pos int) (rune, int) {
413 if pos < len(i.str) {
414 c := i.str[pos]
415 if c < utf8.RuneSelf {
416 return rune(c), 1
417 }
418 return utf8.DecodeRune(i.str[pos:])
419 }
420 return endOfText, 0
421 }
422
423 func (i *inputBytes) canCheckPrefix() bool {
424 return true
425 }
426
427 func (i *inputBytes) hasPrefix(re *Regexp) bool {
428 return bytes.HasPrefix(i.str, re.prefixBytes)
429 }
430
431 func (i *inputBytes) index(re *Regexp, pos int) int {
432 return bytes.Index(i.str[pos:], re.prefixBytes)
433 }
434
435 func (i *inputBytes) context(pos int) lazyFlag {
436 r1, r2 := endOfText, endOfText
437
438 if uint(pos-1) < uint(len(i.str)) {
439 r1 = rune(i.str[pos-1])
440 if r1 >= utf8.RuneSelf {
441 r1, _ = utf8.DecodeLastRune(i.str[:pos])
442 }
443 }
444
445 if uint(pos) < uint(len(i.str)) {
446 r2 = rune(i.str[pos])
447 if r2 >= utf8.RuneSelf {
448 r2, _ = utf8.DecodeRune(i.str[pos:])
449 }
450 }
451 return newLazyFlag(r1, r2)
452 }
453
454
455 type inputReader struct {
456 r io.RuneReader
457 atEOT bool
458 pos int
459 }
460
461 func (i *inputReader) step(pos int) (rune, int) {
462 if !i.atEOT && pos != i.pos {
463 return endOfText, 0
464
465 }
466 r, w, err := i.r.ReadRune()
467 if err != nil {
468 i.atEOT = true
469 return endOfText, 0
470 }
471 i.pos += w
472 return r, w
473 }
474
475 func (i *inputReader) canCheckPrefix() bool {
476 return false
477 }
478
479 func (i *inputReader) hasPrefix(re *Regexp) bool {
480 return false
481 }
482
483 func (i *inputReader) index(re *Regexp, pos int) int {
484 return -1
485 }
486
487 func (i *inputReader) context(pos int) lazyFlag {
488 return 0
489 }
490
491
492
493
494 func (re *Regexp) LiteralPrefix() (prefix string, complete bool) {
495 return re.prefix, re.prefixComplete
496 }
497
498
499
500 func (re *Regexp) MatchReader(r io.RuneReader) bool {
501 return re.doMatch(r, nil, "")
502 }
503
504
505
506 func (re *Regexp) MatchString(s string) bool {
507 return re.doMatch(nil, nil, s)
508 }
509
510
511
512 func (re *Regexp) Match(b []byte) bool {
513 return re.doMatch(nil, b, "")
514 }
515
516
517
518
519 func MatchReader(pattern string, r io.RuneReader) (matched bool, err error) {
520 re, err := Compile(pattern)
521 if err != nil {
522 return false, err
523 }
524 return re.MatchReader(r), nil
525 }
526
527
528
529
530 func MatchString(pattern string, s string) (matched bool, err error) {
531 re, err := Compile(pattern)
532 if err != nil {
533 return false, err
534 }
535 return re.MatchString(s), nil
536 }
537
538
539
540
541 func Match(pattern string, b []byte) (matched bool, err error) {
542 re, err := Compile(pattern)
543 if err != nil {
544 return false, err
545 }
546 return re.Match(b), nil
547 }
548
549
550
551
552 func (re *Regexp) ReplaceAllString(src, repl string) string {
553 n := 2
554 if strings.Contains(repl, "$") {
555 n = 2 * (re.numSubexp + 1)
556 }
557 b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte {
558 return re.expand(dst, repl, nil, src, match)
559 })
560 return string(b)
561 }
562
563
564
565
566 func (re *Regexp) ReplaceAllLiteralString(src, repl string) string {
567 return string(re.replaceAll(nil, src, 2, func(dst []byte, match []int) []byte {
568 return append(dst, repl...)
569 }))
570 }
571
572
573
574
575
576 func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
577 b := re.replaceAll(nil, src, 2, func(dst []byte, match []int) []byte {
578 return append(dst, repl(src[match[0]:match[1]])...)
579 })
580 return string(b)
581 }
582
583 func (re *Regexp) replaceAll(bsrc []byte, src string, nmatch int, repl func(dst []byte, m []int) []byte) []byte {
584 lastMatchEnd := 0
585 searchPos := 0
586 var buf []byte
587 var endPos int
588 if bsrc != nil {
589 endPos = len(bsrc)
590 } else {
591 endPos = len(src)
592 }
593 if nmatch > re.prog.NumCap {
594 nmatch = re.prog.NumCap
595 }
596
597 var dstCap [2]int
598 for searchPos <= endPos {
599 a := re.doExecute(nil, bsrc, src, searchPos, nmatch, dstCap[:0])
600 if len(a) == 0 {
601 break
602 }
603
604
605 if bsrc != nil {
606 buf = append(buf, bsrc[lastMatchEnd:a[0]]...)
607 } else {
608 buf = append(buf, src[lastMatchEnd:a[0]]...)
609 }
610
611
612
613
614
615 if a[1] > lastMatchEnd || a[0] == 0 {
616 buf = repl(buf, a)
617 }
618 lastMatchEnd = a[1]
619
620
621 var width int
622 if bsrc != nil {
623 _, width = utf8.DecodeRune(bsrc[searchPos:])
624 } else {
625 _, width = utf8.DecodeRuneInString(src[searchPos:])
626 }
627 if searchPos+width > a[1] {
628 searchPos += width
629 } else if searchPos+1 > a[1] {
630
631
632 searchPos++
633 } else {
634 searchPos = a[1]
635 }
636 }
637
638
639 if bsrc != nil {
640 buf = append(buf, bsrc[lastMatchEnd:]...)
641 } else {
642 buf = append(buf, src[lastMatchEnd:]...)
643 }
644
645 return buf
646 }
647
648
649
650
651 func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
652 n := 2
653 if bytes.IndexByte(repl, '$') >= 0 {
654 n = 2 * (re.numSubexp + 1)
655 }
656 srepl := ""
657 b := re.replaceAll(src, "", n, func(dst []byte, match []int) []byte {
658 if len(srepl) != len(repl) {
659 srepl = string(repl)
660 }
661 return re.expand(dst, srepl, src, "", match)
662 })
663 return b
664 }
665
666
667
668
669 func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte {
670 return re.replaceAll(src, "", 2, func(dst []byte, match []int) []byte {
671 return append(dst, repl...)
672 })
673 }
674
675
676
677
678
679 func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte {
680 return re.replaceAll(src, "", 2, func(dst []byte, match []int) []byte {
681 return append(dst, repl(src[match[0]:match[1]])...)
682 })
683 }
684
685
686 var specialBytes [16]byte
687
688
689 func special(b byte) bool {
690 return b < utf8.RuneSelf && specialBytes[b%16]&(1<<(b/16)) != 0
691 }
692
693 func init() {
694 for _, b := range []byte(`\.+*?()|[]{}^$`) {
695 specialBytes[b%16] |= 1 << (b / 16)
696 }
697 }
698
699
700
701
702 func QuoteMeta(s string) string {
703
704 var i int
705 for i = 0; i < len(s); i++ {
706 if special(s[i]) {
707 break
708 }
709 }
710
711 if i >= len(s) {
712 return s
713 }
714
715 b := make([]byte, 2*len(s)-i)
716 copy(b, s[:i])
717 j := i
718 for ; i < len(s); i++ {
719 if special(s[i]) {
720 b[j] = '\\'
721 j++
722 }
723 b[j] = s[i]
724 j++
725 }
726 return string(b[:j])
727 }
728
729
730
731
732
733
734 func (re *Regexp) pad(a []int) []int {
735 if a == nil {
736
737 return nil
738 }
739 n := (1 + re.numSubexp) * 2
740 for len(a) < n {
741 a = append(a, -1)
742 }
743 return a
744 }
745
746
747
748
749 func (re *Regexp) allMatches(s string, b []byte, n int, deliver func([]int)) {
750 var end int
751 if b == nil {
752 end = len(s)
753 } else {
754 end = len(b)
755 }
756
757 for pos, i, prevMatchEnd := 0, 0, -1; i < n && pos <= end; {
758 matches := re.doExecute(nil, b, s, pos, re.prog.NumCap, nil)
759 if len(matches) == 0 {
760 break
761 }
762
763 accept := true
764 if matches[1] == pos {
765
766 if matches[0] == prevMatchEnd {
767
768
769 accept = false
770 }
771 var width int
772
773 if b == nil {
774 _, width = utf8.DecodeRuneInString(s[pos:end])
775 } else {
776 _, width = utf8.DecodeRune(b[pos:end])
777 }
778 if width > 0 {
779 pos += width
780 } else {
781 pos = end + 1
782 }
783 } else {
784 pos = matches[1]
785 }
786 prevMatchEnd = matches[1]
787
788 if accept {
789 deliver(re.pad(matches))
790 i++
791 }
792 }
793 }
794
795
796
797 func (re *Regexp) Find(b []byte) []byte {
798 var dstCap [2]int
799 a := re.doExecute(nil, b, "", 0, 2, dstCap[:0])
800 if a == nil {
801 return nil
802 }
803 return b[a[0]:a[1]:a[1]]
804 }
805
806
807
808
809
810 func (re *Regexp) FindIndex(b []byte) (loc []int) {
811 a := re.doExecute(nil, b, "", 0, 2, nil)
812 if a == nil {
813 return nil
814 }
815 return a[0:2]
816 }
817
818
819
820
821
822
823 func (re *Regexp) FindString(s string) string {
824 var dstCap [2]int
825 a := re.doExecute(nil, nil, s, 0, 2, dstCap[:0])
826 if a == nil {
827 return ""
828 }
829 return s[a[0]:a[1]]
830 }
831
832
833
834
835
836 func (re *Regexp) FindStringIndex(s string) (loc []int) {
837 a := re.doExecute(nil, nil, s, 0, 2, nil)
838 if a == nil {
839 return nil
840 }
841 return a[0:2]
842 }
843
844
845
846
847
848
849 func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int) {
850 a := re.doExecute(r, nil, "", 0, 2, nil)
851 if a == nil {
852 return nil
853 }
854 return a[0:2]
855 }
856
857
858
859
860
861
862 func (re *Regexp) FindSubmatch(b []byte) [][]byte {
863 var dstCap [4]int
864 a := re.doExecute(nil, b, "", 0, re.prog.NumCap, dstCap[:0])
865 if a == nil {
866 return nil
867 }
868 ret := make([][]byte, 1+re.numSubexp)
869 for i := range ret {
870 if 2*i < len(a) && a[2*i] >= 0 {
871 ret[i] = b[a[2*i]:a[2*i+1]:a[2*i+1]]
872 }
873 }
874 return ret
875 }
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894 func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte {
895 return re.expand(dst, string(template), src, "", match)
896 }
897
898
899
900
901 func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte {
902 return re.expand(dst, template, nil, src, match)
903 }
904
905 func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src string, match []int) []byte {
906 for len(template) > 0 {
907 i := strings.Index(template, "$")
908 if i < 0 {
909 break
910 }
911 dst = append(dst, template[:i]...)
912 template = template[i:]
913 if len(template) > 1 && template[1] == '$' {
914
915 dst = append(dst, '$')
916 template = template[2:]
917 continue
918 }
919 name, num, rest, ok := extract(template)
920 if !ok {
921
922 dst = append(dst, '$')
923 template = template[1:]
924 continue
925 }
926 template = rest
927 if num >= 0 {
928 if 2*num+1 < len(match) && match[2*num] >= 0 {
929 if bsrc != nil {
930 dst = append(dst, bsrc[match[2*num]:match[2*num+1]]...)
931 } else {
932 dst = append(dst, src[match[2*num]:match[2*num+1]]...)
933 }
934 }
935 } else {
936 for i, namei := range re.subexpNames {
937 if name == namei && 2*i+1 < len(match) && match[2*i] >= 0 {
938 if bsrc != nil {
939 dst = append(dst, bsrc[match[2*i]:match[2*i+1]]...)
940 } else {
941 dst = append(dst, src[match[2*i]:match[2*i+1]]...)
942 }
943 break
944 }
945 }
946 }
947 }
948 dst = append(dst, template...)
949 return dst
950 }
951
952
953
954 func extract(str string) (name string, num int, rest string, ok bool) {
955 if len(str) < 2 || str[0] != '$' {
956 return
957 }
958 brace := false
959 if str[1] == '{' {
960 brace = true
961 str = str[2:]
962 } else {
963 str = str[1:]
964 }
965 i := 0
966 for i < len(str) {
967 rune, size := utf8.DecodeRuneInString(str[i:])
968 if !unicode.IsLetter(rune) && !unicode.IsDigit(rune) && rune != '_' {
969 break
970 }
971 i += size
972 }
973 if i == 0 {
974
975 return
976 }
977 name = str[:i]
978 if brace {
979 if i >= len(str) || str[i] != '}' {
980
981 return
982 }
983 i++
984 }
985
986
987 num = 0
988 for i := 0; i < len(name); i++ {
989 if name[i] < '0' || '9' < name[i] || num >= 1e8 {
990 num = -1
991 break
992 }
993 num = num*10 + int(name[i]) - '0'
994 }
995
996 if name[0] == '0' && len(name) > 1 {
997 num = -1
998 }
999
1000 rest = str[i:]
1001 ok = true
1002 return
1003 }
1004
1005
1006
1007
1008
1009
1010 func (re *Regexp) FindSubmatchIndex(b []byte) []int {
1011 return re.pad(re.doExecute(nil, b, "", 0, re.prog.NumCap, nil))
1012 }
1013
1014
1015
1016
1017
1018
1019 func (re *Regexp) FindStringSubmatch(s string) []string {
1020 var dstCap [4]int
1021 a := re.doExecute(nil, nil, s, 0, re.prog.NumCap, dstCap[:0])
1022 if a == nil {
1023 return nil
1024 }
1025 ret := make([]string, 1+re.numSubexp)
1026 for i := range ret {
1027 if 2*i < len(a) && a[2*i] >= 0 {
1028 ret[i] = s[a[2*i]:a[2*i+1]]
1029 }
1030 }
1031 return ret
1032 }
1033
1034
1035
1036
1037
1038
1039 func (re *Regexp) FindStringSubmatchIndex(s string) []int {
1040 return re.pad(re.doExecute(nil, nil, s, 0, re.prog.NumCap, nil))
1041 }
1042
1043
1044
1045
1046
1047
1048 func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int {
1049 return re.pad(re.doExecute(r, nil, "", 0, re.prog.NumCap, nil))
1050 }
1051
1052 const startSize = 10
1053
1054
1055
1056
1057
1058 func (re *Regexp) FindAll(b []byte, n int) [][]byte {
1059 if n < 0 {
1060 n = len(b) + 1
1061 }
1062 var result [][]byte
1063 re.allMatches("", b, n, func(match []int) {
1064 if result == nil {
1065 result = make([][]byte, 0, startSize)
1066 }
1067 result = append(result, b[match[0]:match[1]:match[1]])
1068 })
1069 return result
1070 }
1071
1072
1073
1074
1075
1076 func (re *Regexp) FindAllIndex(b []byte, n int) [][]int {
1077 if n < 0 {
1078 n = len(b) + 1
1079 }
1080 var result [][]int
1081 re.allMatches("", b, n, func(match []int) {
1082 if result == nil {
1083 result = make([][]int, 0, startSize)
1084 }
1085 result = append(result, match[0:2])
1086 })
1087 return result
1088 }
1089
1090
1091
1092
1093
1094 func (re *Regexp) FindAllString(s string, n int) []string {
1095 if n < 0 {
1096 n = len(s) + 1
1097 }
1098 var result []string
1099 re.allMatches(s, nil, n, func(match []int) {
1100 if result == nil {
1101 result = make([]string, 0, startSize)
1102 }
1103 result = append(result, s[match[0]:match[1]])
1104 })
1105 return result
1106 }
1107
1108
1109
1110
1111
1112 func (re *Regexp) FindAllStringIndex(s string, n int) [][]int {
1113 if n < 0 {
1114 n = len(s) + 1
1115 }
1116 var result [][]int
1117 re.allMatches(s, nil, n, func(match []int) {
1118 if result == nil {
1119 result = make([][]int, 0, startSize)
1120 }
1121 result = append(result, match[0:2])
1122 })
1123 return result
1124 }
1125
1126
1127
1128
1129
1130 func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte {
1131 if n < 0 {
1132 n = len(b) + 1
1133 }
1134 var result [][][]byte
1135 re.allMatches("", b, n, func(match []int) {
1136 if result == nil {
1137 result = make([][][]byte, 0, startSize)
1138 }
1139 slice := make([][]byte, len(match)/2)
1140 for j := range slice {
1141 if match[2*j] >= 0 {
1142 slice[j] = b[match[2*j]:match[2*j+1]:match[2*j+1]]
1143 }
1144 }
1145 result = append(result, slice)
1146 })
1147 return result
1148 }
1149
1150
1151
1152
1153
1154 func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int {
1155 if n < 0 {
1156 n = len(b) + 1
1157 }
1158 var result [][]int
1159 re.allMatches("", b, n, func(match []int) {
1160 if result == nil {
1161 result = make([][]int, 0, startSize)
1162 }
1163 result = append(result, match)
1164 })
1165 return result
1166 }
1167
1168
1169
1170
1171
1172 func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
1173 if n < 0 {
1174 n = len(s) + 1
1175 }
1176 var result [][]string
1177 re.allMatches(s, nil, n, func(match []int) {
1178 if result == nil {
1179 result = make([][]string, 0, startSize)
1180 }
1181 slice := make([]string, len(match)/2)
1182 for j := range slice {
1183 if match[2*j] >= 0 {
1184 slice[j] = s[match[2*j]:match[2*j+1]]
1185 }
1186 }
1187 result = append(result, slice)
1188 })
1189 return result
1190 }
1191
1192
1193
1194
1195
1196
1197 func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int {
1198 if n < 0 {
1199 n = len(s) + 1
1200 }
1201 var result [][]int
1202 re.allMatches(s, nil, n, func(match []int) {
1203 if result == nil {
1204 result = make([][]int, 0, startSize)
1205 }
1206 result = append(result, match)
1207 })
1208 return result
1209 }
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226 func (re *Regexp) Split(s string, n int) []string {
1227
1228 if n == 0 {
1229 return nil
1230 }
1231
1232 if len(re.expr) > 0 && len(s) == 0 {
1233 return []string{""}
1234 }
1235
1236 matches := re.FindAllStringIndex(s, n)
1237 strings := make([]string, 0, len(matches))
1238
1239 beg := 0
1240 end := 0
1241 for _, match := range matches {
1242 if n > 0 && len(strings) >= n-1 {
1243 break
1244 }
1245
1246 end = match[0]
1247 if match[1] != 0 {
1248 strings = append(strings, s[beg:end])
1249 }
1250 beg = match[1]
1251 }
1252
1253 if end != len(s) {
1254 strings = append(strings, s[beg:])
1255 }
1256
1257 return strings
1258 }
1259
View as plain text