}
-// ExecuteString matches the Regexp against the string s.
-// The return value is an array of integers, in pairs, identifying the positions of
-// substrings matched by the expression.
-// s[a[0]:a[1]] is the substring matched by the entire expression.
-// s[a[2*i]:a[2*i+1]] for i > 0 is the substring matched by the ith parenthesized subexpression.
-// A negative value means the subexpression did not match any element of the string.
-// An empty array means "no match".
-func (re *Regexp) ExecuteString(s string) (a []int) {
- return re.doExecute(s, nil, 0)
-}
-
-
-// Execute matches the Regexp against the byte slice b.
-// The return value is an array of integers, in pairs, identifying the positions of
-// subslices matched by the expression.
-// b[a[0]:a[1]] is the subslice matched by the entire expression.
-// b[a[2*i]:a[2*i+1]] for i > 0 is the subslice matched by the ith parenthesized subexpression.
-// A negative value means the subexpression did not match any element of the slice.
-// An empty array means "no match".
-func (re *Regexp) Execute(b []byte) (a []int) { return re.doExecute("", b, 0) }
-
-
// MatchString returns whether the Regexp matches the string s.
// The return value is a boolean: true for match, false for no match.
func (re *Regexp) MatchString(s string) bool { return len(re.doExecute(s, nil, 0)) > 0 }
func (re *Regexp) Match(b []byte) bool { return len(re.doExecute("", b, 0)) > 0 }
-// MatchStrings matches the Regexp against the string s.
-// The return value is an array of strings matched by the expression.
-// a[0] is the substring matched by the entire expression.
-// a[i] for i > 0 is the substring matched by the ith parenthesized subexpression.
-// An empty array means ``no match''.
-func (re *Regexp) MatchStrings(s string) (a []string) {
- r := re.doExecute(s, nil, 0)
- if r == nil {
- return nil
- }
- a = make([]string, len(r)/2)
- for i := 0; i < len(r); i += 2 {
- if r[i] != -1 { // -1 means no match for this subexpression
- a[i/2] = s[r[i]:r[i+1]]
- }
- }
- return
-}
-
-// MatchSlices matches the Regexp against the byte slice b.
-// The return value is an array of subslices matched by the expression.
-// a[0] is the subslice matched by the entire expression.
-// a[i] for i > 0 is the subslice matched by the ith parenthesized subexpression.
-// An empty array means ``no match''.
-func (re *Regexp) MatchSlices(b []byte) (a [][]byte) {
- r := re.doExecute("", b, 0)
- if r == nil {
- return nil
- }
- a = make([][]byte, len(r)/2)
- for i := 0; i < len(r); i += 2 {
- if r[i] != -1 { // -1 means no match for this subexpression
- a[i/2] = b[r[i]:r[i+1]]
- }
- }
- return
-}
-
// MatchString checks whether a textual regular expression
// matches a string. More complicated queries need
// to use Compile and the full Regexp interface.
return re.MatchString(s), ""
}
+
// Match checks whether a textual regular expression
// matches a byte slice. More complicated queries need
// to use Compile and the full Regexp interface.
return re
}
-func printVec(t *T, m []int) {
- l := len(m)
- if l == 0 {
- t.Log("\t<no match>")
- } else {
- for i := 0; i < l; i = i + 2 {
- t.Log("\t", m[i], ",", m[i+1])
- }
- }
-}
-
-func printStrings(t *T, m []string) {
- l := len(m)
- if l == 0 {
- t.Log("\t<no match>")
- } else {
- for i := 0; i < l; i = i + 2 {
- t.Logf("\t%q", m[i])
- }
- }
-}
-
-func printBytes(t *T, b [][]byte) {
- l := len(b)
- if l == 0 {
- t.Log("\t<no match>")
- } else {
- for i := 0; i < l; i = i + 2 {
- t.Logf("\t%q", b[i])
- }
- }
-}
-
-func equal(m1, m2 []int) bool {
- l := len(m1)
- if l != len(m2) {
- return false
- }
- for i := 0; i < l; i++ {
- if m1[i] != m2[i] {
- return false
- }
- }
- return true
-}
-
-func equalStrings(m1, m2 []string) bool {
- l := len(m1)
- if l != len(m2) {
- return false
- }
- for i := 0; i < l; i++ {
- if m1[i] != m2[i] {
- return false
- }
- }
- return true
-}
-
-func equalBytes(m1 [][]byte, m2 []string) bool {
- l := len(m1)
- if l != len(m2) {
- return false
- }
- for i := 0; i < l; i++ {
- if string(m1[i]) != m2[i] {
- return false
- }
- }
- return true
-}
-
-func executeTest(t *T, expr string, str string, match []int) {
- re := compileTest(t, expr, "")
- if re == nil {
- return
- }
- m := re.ExecuteString(str)
- if !equal(m, match) {
- t.Error("ExecuteString failure on `", expr, "` matching `", str, "`:")
- printVec(t, m)
- t.Log("should be:")
- printVec(t, match)
- }
- // now try bytes
- m = re.Execute([]byte(str))
- if !equal(m, match) {
- t.Error("Execute failure on `", expr, "` matching `", str, "`:")
- printVec(t, m)
- t.Log("should be:")
- printVec(t, match)
- }
-}
-
func TestGoodCompile(t *T) {
for i := 0; i < len(good_re); i++ {
compileTest(t, good_re[i], "")
}
}
-func TestExecute(t *T) {
- for i := 0; i < len(matches); i++ {
- test := &matches[i]
- executeTest(t, test.re, test.text, test.match)
- }
-}
-
func matchTest(t *T, expr string, str string, match []int) {
re := compileTest(t, expr, "")
if re == nil {
}
}
-func matchStringsTest(t *T, expr string, str string, match []int) {
- re := compileTest(t, expr, "")
- if re == nil {
- return
- }
- strs := make([]string, len(match)/2)
- for i := 0; i < len(match); i++ {
- strs[i/2] = str[match[i]:match[i+1]]
- }
- m := re.MatchStrings(str)
- if !equalStrings(m, strs) {
- t.Error("MatchStrings failure on `", expr, "` matching `", str, "`:")
- printStrings(t, m)
- t.Log("should be:")
- printStrings(t, strs)
- }
- // now try bytes
- s := re.MatchSlices([]byte(str))
- if !equalBytes(s, strs) {
- t.Error("MatchSlices failure on `", expr, "` matching `", str, "`:")
- printBytes(t, s)
- t.Log("should be:")
- printStrings(t, strs)
- }
-}
-
-func TestMatchStrings(t *T) {
- for i := 0; i < len(matches); i++ {
- test := &matches[i]
- matchTest(t, test.re, test.text, test.match)
- }
-}
-
func matchFunctionTest(t *T, expr string, str string, match []int) {
m, err := MatchString(expr, str)
if err == "" {
matchFunctionTest(t, test.re, test.text, test.match)
}
}
-
-func BenchmarkSimpleMatch(b *B) {
- b.StopTimer()
- re, _ := CompileRegexp("a")
- b.StartTimer()
-
- for i := 0; i < b.N; i++ {
- re.MatchString("a")
- }
-}
-
-func BenchmarkUngroupedMatch(b *B) {
- b.StopTimer()
- re, _ := CompileRegexp("[a-z]+ [0-9]+ [a-z]+")
- b.StartTimer()
-
- for i := 0; i < b.N; i++ {
- re.MatchString("word 123 other")
- }
-}
-
-func BenchmarkGroupedMatch(b *B) {
- b.StopTimer()
- re, _ := CompileRegexp("([a-z]+) ([0-9]+) ([a-z]+)")
- b.StartTimer()
-
- for i := 0; i < b.N; i++ {
- re.MatchString("word 123 other")
- }
-}