mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
regexp: add SubexpNames
Fixes #2440. R=r, dsymonds CC=golang-dev https://golang.org/cl/5559043
This commit is contained in:
parent
e3e93b0f43
commit
21d3721eb8
3 changed files with 67 additions and 23 deletions
|
|
@ -85,6 +85,7 @@ type Regexp struct {
|
|||
prefixRune rune // first rune in prefix
|
||||
cond syntax.EmptyOp // empty-width conditions required at start of match
|
||||
numSubexp int
|
||||
subexpNames []string
|
||||
longest bool
|
||||
|
||||
// cache of machines for running regexp
|
||||
|
|
@ -140,17 +141,20 @@ func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
|
|||
return nil, err
|
||||
}
|
||||
maxCap := re.MaxCap()
|
||||
capNames := re.CapNames()
|
||||
|
||||
re = re.Simplify()
|
||||
prog, err := syntax.Compile(re)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
regexp := &Regexp{
|
||||
expr: expr,
|
||||
prog: prog,
|
||||
numSubexp: maxCap,
|
||||
cond: prog.StartCond(),
|
||||
longest: longest,
|
||||
expr: expr,
|
||||
prog: prog,
|
||||
numSubexp: maxCap,
|
||||
subexpNames: capNames,
|
||||
cond: prog.StartCond(),
|
||||
longest: longest,
|
||||
}
|
||||
regexp.prefix, regexp.prefixComplete = prog.Prefix()
|
||||
if regexp.prefix != "" {
|
||||
|
|
@ -223,6 +227,15 @@ func (re *Regexp) NumSubexp() int {
|
|||
return re.numSubexp
|
||||
}
|
||||
|
||||
// SubexpNames returns the names of the parenthesized subexpressions
|
||||
// in this Regexp. 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 Regexp as a whole cannot be named, names[0] is always
|
||||
// the empty string. The slice should not be modified.
|
||||
func (re *Regexp) SubexpNames() []string {
|
||||
return re.subexpNames
|
||||
}
|
||||
|
||||
const endOfText rune = -1
|
||||
|
||||
// input abstracts different representations of the input text. It provides
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue