Add query to find number of subexpressions.

This was convenient for me to have without being forced
to parse the regexp myself. I'd understand if it's not
really wanted, but I also think that some meta information
about compiled regexps would be fine.

R=r, rsc
CC=golang-dev
https://golang.org/cl/183044
This commit is contained in:
Peter Froehlich 2009-12-24 08:43:35 +11:00 committed by Rob Pike
parent b266f39b85
commit e1033d07b4
2 changed files with 31 additions and 0 deletions

View file

@ -454,6 +454,34 @@ func TestAllMatches(t *testing.T) {
}
}
type numSubexpCase struct {
input string
expected int
}
var numSubexpCases = []numSubexpCase{
numSubexpCase{``, 0},
numSubexpCase{`.*`, 0},
numSubexpCase{`abba`, 0},
numSubexpCase{`ab(b)a`, 1},
numSubexpCase{`ab(.*)a`, 1},
numSubexpCase{`(.*)ab(.*)a`, 2},
numSubexpCase{`(.*)(ab)(.*)a`, 3},
numSubexpCase{`(.*)((a)b)(.*)a`, 4},
numSubexpCase{`(.*)(\(ab)(.*)a`, 3},
numSubexpCase{`(.*)(\(a\)b)(.*)a`, 3},
}
func TestNumSubexp(t *testing.T) {
for _, c := range numSubexpCases {
re, _ := Compile(c.input)
n := re.NumSubexp()
if n != c.expected {
t.Errorf("NumSubexp for %q returned %d, expected %d", c.input, n, c.expected)
}
}
}
func BenchmarkLiteral(b *testing.B) {
x := strings.Repeat("x", 50)
b.StopTimer()