2008-10-09 20:40:53 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-11-19 16:38:46 -07:00
|
|
|
package regexp
|
2008-10-09 20:40:53 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
2009-08-05 16:44:45 -06:00
|
|
|
"strings";
|
2008-11-19 16:38:46 -07:00
|
|
|
"testing";
|
2008-10-09 20:40:53 -06:00
|
|
|
)
|
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
var good_re = []string{
|
2008-10-14 18:45:49 -06:00
|
|
|
``,
|
|
|
|
`.`,
|
|
|
|
`^.$`,
|
|
|
|
`a`,
|
|
|
|
`a*`,
|
|
|
|
`a+`,
|
|
|
|
`a?`,
|
|
|
|
`a|b`,
|
|
|
|
`a*|b*`,
|
|
|
|
`(a*|b)(c*|d)`,
|
|
|
|
`[a-z]`,
|
|
|
|
`[a-abc-c\-\]\[]`,
|
|
|
|
`[a-z]+`,
|
|
|
|
`[]`,
|
|
|
|
`[abc]`,
|
|
|
|
`[^1234]`,
|
2009-08-05 15:40:34 -06:00
|
|
|
`[^\n]`,
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2009-01-06 14:54:53 -07:00
|
|
|
// TODO: nice to do this with a map
|
2009-01-15 18:22:15 -07:00
|
|
|
type stringError struct {
|
2008-10-14 17:32:43 -06:00
|
|
|
re string;
|
2009-04-17 01:08:24 -06:00
|
|
|
err os.Error;
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
|
2009-03-03 09:39:12 -07:00
|
|
|
var bad_re = []stringError{
|
2009-10-07 12:55:06 -06:00
|
|
|
stringError{`*`, ErrBareClosure},
|
|
|
|
stringError{`(abc`, ErrUnmatchedLpar},
|
|
|
|
stringError{`abc)`, ErrUnmatchedRpar},
|
|
|
|
stringError{`x[a-z`, ErrUnmatchedLbkt},
|
|
|
|
stringError{`abc]`, ErrUnmatchedRbkt},
|
|
|
|
stringError{`[z-a]`, ErrBadRange},
|
|
|
|
stringError{`abc\`, ErrExtraneousBackslash},
|
|
|
|
stringError{`a**`, ErrBadClosure},
|
|
|
|
stringError{`a*+`, ErrBadClosure},
|
|
|
|
stringError{`a??`, ErrBadClosure},
|
|
|
|
stringError{`*`, ErrBareClosure},
|
|
|
|
stringError{`\x`, ErrBadBackslash},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
type vec []int
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2009-01-15 18:22:15 -07:00
|
|
|
type tester struct {
|
2008-10-14 17:32:43 -06:00
|
|
|
re string;
|
|
|
|
text string;
|
2009-01-15 18:22:15 -07:00
|
|
|
match vec;
|
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var matches = []tester{
|
2009-11-20 00:12:01 -07:00
|
|
|
tester{`^abcdefg`, "abcdefg", vec{0, 7}},
|
2009-11-19 17:45:50 -07:00
|
|
|
tester{`a+`, "baaab", vec{1, 4}},
|
|
|
|
tester{"abcd..", "abcdef", vec{0, 6}},
|
2009-10-07 12:55:06 -06:00
|
|
|
tester{``, "", vec{0, 0}},
|
|
|
|
tester{`a`, "a", vec{0, 1}},
|
|
|
|
tester{`x`, "y", vec{}},
|
|
|
|
tester{`b`, "abc", vec{1, 2}},
|
|
|
|
tester{`.`, "a", vec{0, 1}},
|
|
|
|
tester{`.*`, "abcdef", vec{0, 6}},
|
2009-11-14 13:23:24 -07:00
|
|
|
tester{`^`, "abcde", vec{0, 0}},
|
|
|
|
tester{`$`, "abcde", vec{5, 5}},
|
2009-10-07 12:55:06 -06:00
|
|
|
tester{`^abcd$`, "abcd", vec{0, 4}},
|
|
|
|
tester{`^bcd'`, "abcdef", vec{}},
|
|
|
|
tester{`^abcd$`, "abcde", vec{}},
|
|
|
|
tester{`a+`, "baaab", vec{1, 4}},
|
|
|
|
tester{`a*`, "baaab", vec{0, 0}},
|
|
|
|
tester{`[a-z]+`, "abcd", vec{0, 4}},
|
|
|
|
tester{`[^a-z]+`, "ab1234cd", vec{2, 6}},
|
|
|
|
tester{`[a\-\]z]+`, "az]-bcz", vec{0, 4}},
|
|
|
|
tester{`[^\n]+`, "abcd\n", vec{0, 4}},
|
|
|
|
tester{`[日本語]+`, "日本語日本語", vec{0, 18}},
|
2009-11-19 17:45:50 -07:00
|
|
|
tester{`日本語+`, "日本語", vec{0, 9}},
|
|
|
|
tester{`日本語+`, "日本語語語語", vec{0, 18}},
|
2009-10-07 12:55:06 -06:00
|
|
|
tester{`()`, "", vec{0, 0, 0, 0}},
|
|
|
|
tester{`(a)`, "a", vec{0, 1, 0, 1}},
|
|
|
|
tester{`(.)(.)`, "日a", vec{0, 4, 0, 3, 3, 4}},
|
|
|
|
tester{`(.*)`, "", vec{0, 0, 0, 0}},
|
|
|
|
tester{`(.*)`, "abcd", vec{0, 4, 0, 4}},
|
|
|
|
tester{`(..)(..)`, "abcd", vec{0, 4, 0, 2, 2, 4}},
|
|
|
|
tester{`(([^xyz]*)(d))`, "abcd", vec{0, 4, 0, 4, 0, 3, 3, 4}},
|
|
|
|
tester{`((a|b|c)*(d))`, "abcd", vec{0, 4, 0, 4, 2, 3, 3, 4}},
|
|
|
|
tester{`(((a|b|c)*)(d))`, "abcd", vec{0, 4, 0, 4, 0, 3, 2, 3, 3, 4}},
|
|
|
|
tester{`a*(|(b))c*`, "aacc", vec{0, 4, 2, 2, -1, -1}},
|
2009-11-14 13:23:24 -07:00
|
|
|
tester{`(.*).*`, "ab", vec{0, 2, 0, 2}},
|
2009-11-19 17:45:50 -07:00
|
|
|
tester{`[.]`, ".", vec{0, 1}},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-10-14 17:32:43 -06:00
|
|
|
|
2009-08-11 14:54:47 -06:00
|
|
|
func compileTest(t *testing.T, expr string, error os.Error) *Regexp {
|
|
|
|
re, err := Compile(expr);
|
2008-10-14 17:32:43 -06:00
|
|
|
if err != error {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error("compiling `", expr, "`; unexpected error: ", err.String())
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
return re;
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func printVec(t *testing.T, m []int) {
|
2008-12-18 23:37:22 -07:00
|
|
|
l := len(m);
|
2008-10-14 18:45:49 -06:00
|
|
|
if l == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Log("\t<no match>")
|
2008-10-14 18:45:49 -06:00
|
|
|
} else {
|
2009-11-13 00:35:45 -07:00
|
|
|
if m[len(m)-1] == -1 {
|
|
|
|
m = m[0 : len(m)-2]
|
2009-08-05 16:44:45 -06:00
|
|
|
}
|
2009-11-13 00:35:45 -07:00
|
|
|
t.Log("\t", m);
|
2009-08-05 16:44:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func equal(m1, m2 []int) bool {
|
2008-12-18 23:37:22 -07:00
|
|
|
l := len(m1);
|
|
|
|
if l != len(m2) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if m1[i] != m2[i] {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
return true;
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func equalStrings(m1, m2 []string) bool {
|
2009-01-06 14:54:53 -07:00
|
|
|
l := len(m1);
|
|
|
|
if l != len(m2) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if m1[i] != m2[i] {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-07 12:55:06 -06:00
|
|
|
return true;
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func executeTest(t *testing.T, expr string, str string, match []int) {
|
|
|
|
re := compileTest(t, expr, nil);
|
2008-11-19 16:38:46 -07:00
|
|
|
if re == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2008-11-19 16:38:46 -07:00
|
|
|
}
|
2009-08-05 16:44:45 -06:00
|
|
|
m := re.ExecuteString(str);
|
|
|
|
if !equal(m, match) {
|
2009-11-13 00:35:45 -07:00
|
|
|
t.Errorf("ExecuteString failure on %#q matching %q:", expr, str);
|
2009-08-05 16:44:45 -06:00
|
|
|
printVec(t, m);
|
|
|
|
t.Log("should be:");
|
|
|
|
printVec(t, match);
|
|
|
|
}
|
|
|
|
// now try bytes
|
|
|
|
m = re.Execute(strings.Bytes(str));
|
2009-01-16 11:34:36 -07:00
|
|
|
if !equal(m, match) {
|
2009-11-13 00:35:45 -07:00
|
|
|
t.Errorf("Execute failure on %#q matching %q:", expr, str);
|
2009-01-16 11:34:36 -07:00
|
|
|
printVec(t, m);
|
2008-11-19 16:38:46 -07:00
|
|
|
t.Log("should be:");
|
2009-01-16 11:34:36 -07:00
|
|
|
printVec(t, match);
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestGoodCompile(t *testing.T) {
|
2008-10-14 17:32:43 -06:00
|
|
|
for i := 0; i < len(good_re); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
compileTest(t, good_re[i], nil)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2008-11-19 16:38:46 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestBadCompile(t *testing.T) {
|
2008-10-14 17:32:43 -06:00
|
|
|
for i := 0; i < len(bad_re); i++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
compileTest(t, bad_re[i].re, bad_re[i].err)
|
2008-10-14 17:32:43 -06:00
|
|
|
}
|
2008-11-19 16:38:46 -07:00
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestExecute(t *testing.T) {
|
2009-01-06 14:54:53 -07:00
|
|
|
for i := 0; i < len(matches); i++ {
|
|
|
|
test := &matches[i];
|
2009-10-07 12:55:06 -06:00
|
|
|
executeTest(t, test.re, test.text, test.match);
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func matchTest(t *testing.T, expr string, str string, match []int) {
|
|
|
|
re := compileTest(t, expr, nil);
|
2009-01-06 14:54:53 -07:00
|
|
|
if re == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
2009-08-05 16:44:45 -06:00
|
|
|
m := re.MatchString(str);
|
|
|
|
if m != (len(match) > 0) {
|
2009-11-14 13:23:24 -07:00
|
|
|
t.Errorf("MatchString failure on %#q matching %q: %t should be %t", expr, str, m, len(match) > 0)
|
2009-08-05 16:44:45 -06:00
|
|
|
}
|
|
|
|
// now try bytes
|
|
|
|
m = re.Match(strings.Bytes(str));
|
2009-01-06 14:54:53 -07:00
|
|
|
if m != (len(match) > 0) {
|
2009-11-14 13:23:24 -07:00
|
|
|
t.Errorf("Match failure on %#q matching %q: %t should be %t", expr, str, m, len(match) > 0)
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestMatch(t *testing.T) {
|
2008-10-14 17:32:43 -06:00
|
|
|
for i := 0; i < len(matches); i++ {
|
2008-11-19 16:38:46 -07:00
|
|
|
test := &matches[i];
|
2009-10-07 12:55:06 -06:00
|
|
|
matchTest(t, test.re, test.text, test.match);
|
2008-10-09 20:40:53 -06:00
|
|
|
}
|
|
|
|
}
|
2009-01-06 14:54:53 -07:00
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestMatchStrings(t *testing.T) {
|
2009-01-06 14:54:53 -07:00
|
|
|
for i := 0; i < len(matches); i++ {
|
|
|
|
test := &matches[i];
|
2009-10-07 12:55:06 -06:00
|
|
|
matchTest(t, test.re, test.text, test.match);
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 11:34:36 -07:00
|
|
|
func matchFunctionTest(t *testing.T, expr string, str string, match []int) {
|
2009-08-05 16:44:45 -06:00
|
|
|
m, err := MatchString(expr, str);
|
2009-01-06 14:54:53 -07:00
|
|
|
if err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
if m != (len(match) > 0) {
|
2009-11-13 00:35:45 -07:00
|
|
|
t.Errorf("Match failure on %#q matching %q: %d should be %d", expr, str, m, len(match) > 0)
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestMatchFunction(t *testing.T) {
|
2009-01-06 14:54:53 -07:00
|
|
|
for i := 0; i < len(matches); i++ {
|
|
|
|
test := &matches[i];
|
2009-10-07 12:55:06 -06:00
|
|
|
matchFunctionTest(t, test.re, test.text, test.match);
|
2009-01-06 14:54:53 -07:00
|
|
|
}
|
|
|
|
}
|
2009-06-18 18:55:47 -06:00
|
|
|
|
|
|
|
type ReplaceTest struct {
|
|
|
|
pattern, replacement, input, output string;
|
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var replaceTests = []ReplaceTest{
|
2009-06-18 18:55:47 -06:00
|
|
|
// Test empty input and/or replacement, with pattern that matches the empty string.
|
|
|
|
ReplaceTest{"", "", "", ""},
|
|
|
|
ReplaceTest{"", "x", "", "x"},
|
|
|
|
ReplaceTest{"", "", "abc", "abc"},
|
|
|
|
ReplaceTest{"", "x", "abc", "xaxbxcx"},
|
|
|
|
|
|
|
|
// Test empty input and/or replacement, with pattern that does not match the empty string.
|
|
|
|
ReplaceTest{"b", "", "", ""},
|
|
|
|
ReplaceTest{"b", "x", "", ""},
|
|
|
|
ReplaceTest{"b", "", "abc", "ac"},
|
|
|
|
ReplaceTest{"b", "x", "abc", "axc"},
|
|
|
|
ReplaceTest{"y", "", "", ""},
|
|
|
|
ReplaceTest{"y", "x", "", ""},
|
|
|
|
ReplaceTest{"y", "", "abc", "abc"},
|
|
|
|
ReplaceTest{"y", "x", "abc", "abc"},
|
|
|
|
|
|
|
|
// Multibyte characters -- verify that we don't try to match in the middle
|
|
|
|
// of a character.
|
|
|
|
ReplaceTest{"[a-c]*", "x", "\u65e5", "x\u65e5x"},
|
|
|
|
ReplaceTest{"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
|
|
|
|
|
|
|
|
// Start and end of a string.
|
|
|
|
ReplaceTest{"^[a-c]*", "x", "abcdabc", "xdabc"},
|
|
|
|
ReplaceTest{"[a-c]*$", "x", "abcdabc", "abcdx"},
|
|
|
|
ReplaceTest{"^[a-c]*$", "x", "abcdabc", "abcdabc"},
|
|
|
|
ReplaceTest{"^[a-c]*", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"[a-c]*$", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"^[a-c]*$", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"^[a-c]*", "x", "dabce", "xdabce"},
|
|
|
|
ReplaceTest{"[a-c]*$", "x", "dabce", "dabcex"},
|
|
|
|
ReplaceTest{"^[a-c]*$", "x", "dabce", "dabce"},
|
|
|
|
ReplaceTest{"^[a-c]*", "x", "", "x"},
|
|
|
|
ReplaceTest{"[a-c]*$", "x", "", "x"},
|
|
|
|
ReplaceTest{"^[a-c]*$", "x", "", "x"},
|
|
|
|
|
|
|
|
ReplaceTest{"^[a-c]+", "x", "abcdabc", "xdabc"},
|
|
|
|
ReplaceTest{"[a-c]+$", "x", "abcdabc", "abcdx"},
|
|
|
|
ReplaceTest{"^[a-c]+$", "x", "abcdabc", "abcdabc"},
|
|
|
|
ReplaceTest{"^[a-c]+", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"[a-c]+$", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"^[a-c]+$", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"^[a-c]+", "x", "dabce", "dabce"},
|
|
|
|
ReplaceTest{"[a-c]+$", "x", "dabce", "dabce"},
|
|
|
|
ReplaceTest{"^[a-c]+$", "x", "dabce", "dabce"},
|
|
|
|
ReplaceTest{"^[a-c]+", "x", "", ""},
|
|
|
|
ReplaceTest{"[a-c]+$", "x", "", ""},
|
|
|
|
ReplaceTest{"^[a-c]+$", "x", "", ""},
|
|
|
|
|
|
|
|
// Other cases.
|
|
|
|
ReplaceTest{"abc", "def", "abcdefg", "defdefg"},
|
|
|
|
ReplaceTest{"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
|
|
|
|
ReplaceTest{"abc", "", "abcdabc", "d"},
|
|
|
|
ReplaceTest{"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
|
|
|
|
ReplaceTest{"abc", "d", "", ""},
|
|
|
|
ReplaceTest{"abc", "d", "abc", "d"},
|
|
|
|
ReplaceTest{".+", "x", "abc", "x"},
|
|
|
|
ReplaceTest{"[a-c]*", "x", "def", "xdxexfx"},
|
|
|
|
ReplaceTest{"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
|
|
|
|
ReplaceTest{"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplaceAll(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, tc := range replaceTests {
|
2009-06-18 18:55:47 -06:00
|
|
|
re, err := Compile(tc.pattern);
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err);
|
|
|
|
continue;
|
|
|
|
}
|
2009-08-05 16:44:45 -06:00
|
|
|
actual := re.ReplaceAllString(tc.input, tc.replacement);
|
|
|
|
if actual != tc.output {
|
|
|
|
t.Errorf("%q.Replace(%q,%q) = %q; want %q",
|
2009-11-09 13:07:39 -07:00
|
|
|
tc.pattern, tc.input, tc.replacement, actual, tc.output)
|
2009-08-05 16:44:45 -06:00
|
|
|
}
|
|
|
|
// now try bytes
|
|
|
|
actual = string(re.ReplaceAll(strings.Bytes(tc.input), strings.Bytes(tc.replacement)));
|
2009-06-18 18:55:47 -06:00
|
|
|
if actual != tc.output {
|
|
|
|
t.Errorf("%q.Replace(%q,%q) = %q; want %q",
|
2009-11-09 13:07:39 -07:00
|
|
|
tc.pattern, tc.input, tc.replacement, actual, tc.output)
|
2009-06-18 18:55:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type QuoteMetaTest struct {
|
|
|
|
pattern, output string;
|
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var quoteMetaTests = []QuoteMetaTest{
|
2009-06-18 18:55:47 -06:00
|
|
|
QuoteMetaTest{``, ``},
|
|
|
|
QuoteMetaTest{`foo`, `foo`},
|
|
|
|
QuoteMetaTest{`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[{\]}\\\|,<\.>/\?~`},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuoteMeta(t *testing.T) {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, tc := range quoteMetaTests {
|
2009-06-18 18:55:47 -06:00
|
|
|
// Verify that QuoteMeta returns the expected string.
|
|
|
|
quoted := QuoteMeta(tc.pattern);
|
|
|
|
if quoted != tc.output {
|
|
|
|
t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
|
|
|
|
tc.pattern, quoted, tc.output);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the quoted string is in fact treated as expected
|
|
|
|
// by Compile -- i.e. that it matches the original, unquoted string.
|
|
|
|
if tc.pattern != "" {
|
|
|
|
re, err := Compile(quoted);
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
src := "abc" + tc.pattern + "def";
|
|
|
|
repl := "xyz";
|
2009-08-05 16:44:45 -06:00
|
|
|
replaced := re.ReplaceAllString(src, repl);
|
2009-06-18 18:55:47 -06:00
|
|
|
expected := "abcxyzdef";
|
|
|
|
if replaced != expected {
|
|
|
|
t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
|
2009-11-09 13:07:39 -07:00
|
|
|
tc.pattern, src, repl, replaced, expected)
|
2009-06-18 18:55:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-09 20:30:47 -06:00
|
|
|
type matchCase struct {
|
2009-10-07 12:55:06 -06:00
|
|
|
matchfunc string;
|
|
|
|
input string;
|
|
|
|
n int;
|
|
|
|
regexp string;
|
|
|
|
expected []string;
|
2009-08-09 20:30:47 -06:00
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var matchCases = []matchCase{
|
|
|
|
matchCase{"match", " aa b", 0, "[^ ]+", []string{"aa", "b"}},
|
|
|
|
matchCase{"match", " aa b", 0, "[^ ]*", []string{"", "aa", "b"}},
|
|
|
|
matchCase{"match", "a b c", 0, "[^ ]*", []string{"a", "b", "c"}},
|
|
|
|
matchCase{"match", "a:a: a:", 0, "^.:", []string{"a:"}},
|
|
|
|
matchCase{"match", "", 0, "[^ ]*", []string{""}},
|
|
|
|
matchCase{"match", "", 0, "", []string{""}},
|
|
|
|
matchCase{"match", "a", 0, "", []string{"", ""}},
|
|
|
|
matchCase{"match", "ab", 0, "^", []string{""}},
|
|
|
|
matchCase{"match", "ab", 0, "$", []string{""}},
|
|
|
|
matchCase{"match", "ab", 0, "X*", []string{"", "", ""}},
|
|
|
|
matchCase{"match", "aX", 0, "X*", []string{"", "X"}},
|
|
|
|
matchCase{"match", "XabX", 0, "X*", []string{"X", "", "X"}},
|
|
|
|
|
|
|
|
matchCase{"matchit", "", 0, ".", []string{}},
|
|
|
|
matchCase{"matchit", "abc", 2, ".", []string{"a", "b"}},
|
|
|
|
matchCase{"matchit", "abc", 0, ".", []string{"a", "b", "c"}},
|
2009-08-09 20:30:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func printStringSlice(t *testing.T, s []string) {
|
2009-11-14 13:23:24 -07:00
|
|
|
t.Logf("%#v", s)
|
2009-08-09 20:30:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllMatches(t *testing.T) {
|
|
|
|
ch := make(chan matchCase);
|
|
|
|
go func() {
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, c := range matchCases {
|
2009-08-09 20:30:47 -06:00
|
|
|
ch <- c;
|
|
|
|
stringCase := matchCase{
|
|
|
|
"string" + c.matchfunc,
|
|
|
|
c.input,
|
|
|
|
c.n,
|
|
|
|
c.regexp,
|
2009-10-07 12:55:06 -06:00
|
|
|
c.expected,
|
|
|
|
};
|
2009-08-09 20:30:47 -06:00
|
|
|
ch <- stringCase;
|
|
|
|
}
|
|
|
|
close(ch);
|
|
|
|
}();
|
|
|
|
|
|
|
|
for c := range ch {
|
|
|
|
var result []string;
|
2009-09-15 10:41:59 -06:00
|
|
|
re, _ := Compile(c.regexp);
|
2009-08-09 20:30:47 -06:00
|
|
|
|
|
|
|
switch c.matchfunc {
|
|
|
|
case "matchit":
|
2009-10-08 16:14:54 -06:00
|
|
|
result = make([]string, len(c.input)+1);
|
2009-08-09 20:30:47 -06:00
|
|
|
i := 0;
|
|
|
|
b := strings.Bytes(c.input);
|
|
|
|
for match := range re.AllMatchesIter(b, c.n) {
|
|
|
|
result[i] = string(match);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
result = result[0:i];
|
|
|
|
case "stringmatchit":
|
2009-10-08 16:14:54 -06:00
|
|
|
result = make([]string, len(c.input)+1);
|
2009-08-09 20:30:47 -06:00
|
|
|
i := 0;
|
|
|
|
for match := range re.AllMatchesStringIter(c.input, c.n) {
|
|
|
|
result[i] = match;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
result = result[0:i];
|
|
|
|
case "match":
|
2009-10-08 16:14:54 -06:00
|
|
|
result = make([]string, len(c.input)+1);
|
2009-08-09 20:30:47 -06:00
|
|
|
b := strings.Bytes(c.input);
|
|
|
|
i := 0;
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, match := range re.AllMatches(b, c.n) {
|
2009-08-09 20:30:47 -06:00
|
|
|
result[i] = string(match);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
result = result[0:i];
|
|
|
|
case "stringmatch":
|
2009-11-09 13:07:39 -07:00
|
|
|
result = re.AllMatchesString(c.input, c.n)
|
2009-08-09 20:30:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !equalStrings(result, c.expected) {
|
|
|
|
t.Errorf("testing '%s'.%s('%s', %d), expected: ",
|
|
|
|
c.regexp, c.matchfunc, c.input, c.n);
|
|
|
|
printStringSlice(t, c.expected);
|
|
|
|
t.Log("got: ");
|
|
|
|
printStringSlice(t, result);
|
|
|
|
t.Log("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 00:12:01 -07:00
|
|
|
|
|
|
|
func BenchmarkLiteral(b *testing.B) {
|
|
|
|
x := strings.Repeat("x", 50);
|
|
|
|
b.StopTimer();
|
|
|
|
re, _ := Compile(x);
|
|
|
|
b.StartTimer();
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !re.MatchString(x) {
|
|
|
|
println("no match!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNotLiteral(b *testing.B) {
|
|
|
|
x := strings.Repeat("x", 49);
|
|
|
|
b.StopTimer();
|
|
|
|
re, _ := Compile("^" + x);
|
|
|
|
b.StartTimer();
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if !re.MatchString(x) {
|
|
|
|
println("no match!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|