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