1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 var good_re
= []string{
33 type stringError
struct {
38 var bad_re
= []stringError
{
39 {`*`, "missing argument to repetition operator: `*`"},
40 {`+`, "missing argument to repetition operator: `+`"},
41 {`?`, "missing argument to repetition operator: `?`"},
42 {`(abc`, "missing closing ): `(abc`"},
43 {`abc)`, "unexpected ): `abc)`"},
44 {`x[a-z`, "missing closing ]: `[a-z`"},
45 {`[z-a]`, "invalid character class range: `z-a`"},
46 {`abc\`, "trailing backslash at end of expression"},
47 {`a**`, "invalid nested repetition operator: `**`"},
48 {`a*+`, "invalid nested repetition operator: `*+`"},
49 {`\x`, "invalid escape sequence: `\\x`"},
52 func compileTest(t
*testing
.T
, expr
string, error
string) *Regexp
{
53 re
, err
:= Compile(expr
)
54 if error
== "" && err
!= nil {
55 t
.Error("compiling `", expr
, "`; unexpected error: ", err
.Error())
57 if error
!= "" && err
== nil {
58 t
.Error("compiling `", expr
, "`; missing error")
59 } else if error
!= "" && !strings
.Contains(err
.Error(), error
) {
60 t
.Error("compiling `", expr
, "`; wrong error: ", err
.Error(), "; want ", error
)
65 func TestGoodCompile(t
*testing
.T
) {
66 for i
:= 0; i
< len(good_re
); i
++ {
67 compileTest(t
, good_re
[i
], "")
71 func TestBadCompile(t
*testing
.T
) {
72 for i
:= 0; i
< len(bad_re
); i
++ {
73 compileTest(t
, bad_re
[i
].re
, bad_re
[i
].err
)
77 func matchTest(t
*testing
.T
, test
*FindTest
) {
78 re
:= compileTest(t
, test
.pat
, "")
82 m
:= re
.MatchString(test
.text
)
83 if m
!= (len(test
.matches
) > 0) {
84 t
.Errorf("MatchString failure on %s: %t should be %t", test
, m
, len(test
.matches
) > 0)
87 m
= re
.Match([]byte(test
.text
))
88 if m
!= (len(test
.matches
) > 0) {
89 t
.Errorf("Match failure on %s: %t should be %t", test
, m
, len(test
.matches
) > 0)
93 func TestMatch(t
*testing
.T
) {
94 for _
, test
:= range findTests
{
99 func matchFunctionTest(t
*testing
.T
, test
*FindTest
) {
100 m
, err
:= MatchString(test
.pat
, test
.text
)
104 if m
!= (len(test
.matches
) > 0) {
105 t
.Errorf("Match failure on %s: %t should be %t", test
, m
, len(test
.matches
) > 0)
109 func TestMatchFunction(t
*testing
.T
) {
110 for _
, test
:= range findTests
{
111 matchFunctionTest(t
, &test
)
115 type ReplaceTest
struct {
116 pattern
, replacement
, input
, output
string
119 var replaceTests
= []ReplaceTest
{
120 // Test empty input and/or replacement, with pattern that matches the empty string.
123 {"", "", "abc", "abc"},
124 {"", "x", "abc", "xaxbxcx"},
126 // Test empty input and/or replacement, with pattern that does not match the empty string.
129 {"b", "", "abc", "ac"},
130 {"b", "x", "abc", "axc"},
133 {"y", "", "abc", "abc"},
134 {"y", "x", "abc", "abc"},
136 // Multibyte characters -- verify that we don't try to match in the middle
138 {"[a-c]*", "x", "\u65e5", "x\u65e5x"},
139 {"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
141 // Start and end of a string.
142 {"^[a-c]*", "x", "abcdabc", "xdabc"},
143 {"[a-c]*$", "x", "abcdabc", "abcdx"},
144 {"^[a-c]*$", "x", "abcdabc", "abcdabc"},
145 {"^[a-c]*", "x", "abc", "x"},
146 {"[a-c]*$", "x", "abc", "x"},
147 {"^[a-c]*$", "x", "abc", "x"},
148 {"^[a-c]*", "x", "dabce", "xdabce"},
149 {"[a-c]*$", "x", "dabce", "dabcex"},
150 {"^[a-c]*$", "x", "dabce", "dabce"},
151 {"^[a-c]*", "x", "", "x"},
152 {"[a-c]*$", "x", "", "x"},
153 {"^[a-c]*$", "x", "", "x"},
155 {"^[a-c]+", "x", "abcdabc", "xdabc"},
156 {"[a-c]+$", "x", "abcdabc", "abcdx"},
157 {"^[a-c]+$", "x", "abcdabc", "abcdabc"},
158 {"^[a-c]+", "x", "abc", "x"},
159 {"[a-c]+$", "x", "abc", "x"},
160 {"^[a-c]+$", "x", "abc", "x"},
161 {"^[a-c]+", "x", "dabce", "dabce"},
162 {"[a-c]+$", "x", "dabce", "dabce"},
163 {"^[a-c]+$", "x", "dabce", "dabce"},
164 {"^[a-c]+", "x", "", ""},
165 {"[a-c]+$", "x", "", ""},
166 {"^[a-c]+$", "x", "", ""},
169 {"abc", "def", "abcdefg", "defdefg"},
170 {"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
171 {"abc", "", "abcdabc", "d"},
172 {"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
173 {"abc", "d", "", ""},
174 {"abc", "d", "abc", "d"},
175 {".+", "x", "abc", "x"},
176 {"[a-c]*", "x", "def", "xdxexfx"},
177 {"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
178 {"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
181 {"a+", "($0)", "banana", "b(a)n(a)n(a)"},
182 {"a+", "(${0})", "banana", "b(a)n(a)n(a)"},
183 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
184 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
185 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"},
186 {"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "},
187 {"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"},
188 {"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"},
189 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"},
190 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"},
191 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"},
192 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"},
193 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""},
194 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"},
195 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"},
196 {"a+", "${oops", "aaa", "${oops"},
197 {"a+", "$$", "aaa", "$"},
198 {"a+", "$", "aaa", "$"},
200 // Substitution when subexpression isn't found
201 {"(x)?", "$1", "123", "123"},
202 {"abc", "$1", "123", "123"},
205 var replaceLiteralTests
= []ReplaceTest
{
207 {"a+", "($0)", "banana", "b($0)n($0)n($0)"},
208 {"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"},
209 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
210 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
211 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"},
212 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"},
213 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"},
214 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"},
215 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"},
216 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"},
217 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"},
218 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"},
219 {"a+", "${oops", "aaa", "${oops"},
220 {"a+", "$$", "aaa", "$$"},
221 {"a+", "$", "aaa", "$"},
224 type ReplaceFuncTest
struct {
226 replacement
func(string) string
230 var replaceFuncTests
= []ReplaceFuncTest
{
231 {"[a-c]", func(s
string) string { return "x" + s
+ "y" }, "defabcdef", "defxayxbyxcydef"},
232 {"[a-c]+", func(s
string) string { return "x" + s
+ "y" }, "defabcdef", "defxabcydef"},
233 {"[a-c]*", func(s
string) string { return "x" + s
+ "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
236 func TestReplaceAll(t
*testing
.T
) {
237 for _
, tc
:= range replaceTests
{
238 re
, err
:= Compile(tc
.pattern
)
240 t
.Errorf("Unexpected error compiling %q: %v", tc
.pattern
, err
)
243 actual
:= re
.ReplaceAllString(tc
.input
, tc
.replacement
)
244 if actual
!= tc
.output
{
245 t
.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q",
246 tc
.pattern
, tc
.input
, tc
.replacement
, actual
, tc
.output
)
249 actual
= string(re
.ReplaceAll([]byte(tc
.input
), []byte(tc
.replacement
)))
250 if actual
!= tc
.output
{
251 t
.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q",
252 tc
.pattern
, tc
.input
, tc
.replacement
, actual
, tc
.output
)
257 func TestReplaceAllLiteral(t
*testing
.T
) {
258 // Run ReplaceAll tests that do not have $ expansions.
259 for _
, tc
:= range replaceTests
{
260 if strings
.Contains(tc
.replacement
, "$") {
263 re
, err
:= Compile(tc
.pattern
)
265 t
.Errorf("Unexpected error compiling %q: %v", tc
.pattern
, err
)
268 actual
:= re
.ReplaceAllLiteralString(tc
.input
, tc
.replacement
)
269 if actual
!= tc
.output
{
270 t
.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
271 tc
.pattern
, tc
.input
, tc
.replacement
, actual
, tc
.output
)
274 actual
= string(re
.ReplaceAllLiteral([]byte(tc
.input
), []byte(tc
.replacement
)))
275 if actual
!= tc
.output
{
276 t
.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
277 tc
.pattern
, tc
.input
, tc
.replacement
, actual
, tc
.output
)
281 // Run literal-specific tests.
282 for _
, tc
:= range replaceLiteralTests
{
283 re
, err
:= Compile(tc
.pattern
)
285 t
.Errorf("Unexpected error compiling %q: %v", tc
.pattern
, err
)
288 actual
:= re
.ReplaceAllLiteralString(tc
.input
, tc
.replacement
)
289 if actual
!= tc
.output
{
290 t
.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
291 tc
.pattern
, tc
.input
, tc
.replacement
, actual
, tc
.output
)
294 actual
= string(re
.ReplaceAllLiteral([]byte(tc
.input
), []byte(tc
.replacement
)))
295 if actual
!= tc
.output
{
296 t
.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
297 tc
.pattern
, tc
.input
, tc
.replacement
, actual
, tc
.output
)
302 func TestReplaceAllFunc(t
*testing
.T
) {
303 for _
, tc
:= range replaceFuncTests
{
304 re
, err
:= Compile(tc
.pattern
)
306 t
.Errorf("Unexpected error compiling %q: %v", tc
.pattern
, err
)
309 actual
:= re
.ReplaceAllStringFunc(tc
.input
, tc
.replacement
)
310 if actual
!= tc
.output
{
311 t
.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
312 tc
.pattern
, tc
.input
, actual
, tc
.output
)
315 actual
= string(re
.ReplaceAllFunc([]byte(tc
.input
), func(s
[]byte) []byte { return []byte(tc
.replacement(string(s
))) }))
316 if actual
!= tc
.output
{
317 t
.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
318 tc
.pattern
, tc
.input
, actual
, tc
.output
)
323 type MetaTest
struct {
324 pattern
, output
, literal
string
328 var metaTests
= []MetaTest
{
330 {`foo`, `foo`, `foo`, true},
331 {`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
332 {`foo.\$`, `foo\.\\\$`, `foo`, false}, // has escaped operators and real operators
333 {`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
336 func TestQuoteMeta(t
*testing
.T
) {
337 for _
, tc
:= range metaTests
{
338 // Verify that QuoteMeta returns the expected string.
339 quoted
:= QuoteMeta(tc
.pattern
)
340 if quoted
!= tc
.output
{
341 t
.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
342 tc
.pattern
, quoted
, tc
.output
)
346 // Verify that the quoted string is in fact treated as expected
347 // by Compile -- i.e. that it matches the original, unquoted string.
348 if tc
.pattern
!= "" {
349 re
, err
:= Compile(quoted
)
351 t
.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc
.pattern
, err
)
354 src
:= "abc" + tc
.pattern
+ "def"
356 replaced
:= re
.ReplaceAllString(src
, repl
)
357 expected
:= "abcxyzdef"
358 if replaced
!= expected
{
359 t
.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
360 tc
.pattern
, src
, repl
, replaced
, expected
)
366 func TestLiteralPrefix(t
*testing
.T
) {
367 for _
, tc
:= range metaTests
{
368 // Literal method needs to scan the pattern.
369 re
:= MustCompile(tc
.pattern
)
370 str
, complete
:= re
.LiteralPrefix()
371 if complete
!= tc
.isLiteral
{
372 t
.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc
.pattern
, complete
, tc
.isLiteral
)
374 if str
!= tc
.literal
{
375 t
.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc
.pattern
, str
, tc
.literal
)
380 type subexpCase
struct {
386 var subexpCases
= []subexpCase
{
390 {`ab(b)a`, 1, []string{"", ""}},
391 {`ab(.*)a`, 1, []string{"", ""}},
392 {`(.*)ab(.*)a`, 2, []string{"", "", ""}},
393 {`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}},
394 {`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}},
395 {`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}},
396 {`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}},
397 {`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}},
400 func TestSubexp(t
*testing
.T
) {
401 for _
, c
:= range subexpCases
{
402 re
:= MustCompile(c
.input
)
405 t
.Errorf("%q: NumSubexp = %d, want %d", c
.input
, n
, c
.num
)
408 names
:= re
.SubexpNames()
409 if len(names
) != 1+n
{
410 t
.Errorf("%q: len(SubexpNames) = %d, want %d", c
.input
, len(names
), n
)
414 for i
:= 0; i
< 1+n
; i
++ {
415 if names
[i
] != c
.names
[i
] {
416 t
.Errorf("%q: SubexpNames[%d] = %q, want %q", c
.input
, i
, names
[i
], c
.names
[i
])
423 var splitTests
= []struct {
429 {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
430 {"foo:and:bar", ":", 1, []string{"foo:and:bar"}},
431 {"foo:and:bar", ":", 2, []string{"foo", "and:bar"}},
432 {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
433 {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
434 {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
435 {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
436 {"baabaab", "a*", -1, []string{"b", "b", "b"}},
437 {"baabaab", "ba*", -1, []string{"", "", "", ""}},
438 {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}},
439 {"foobar", "f+.*b+", -1, []string{"", "ar"}},
440 {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
441 {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}},
442 {"a,b,c,d,e,f", ",", 0, nil},
443 {",", ",", -1, []string{"", ""}},
444 {",,,", ",", -1, []string{"", "", "", ""}},
445 {"", ",", -1, []string{""}},
446 {"", ".*", -1, []string{""}},
447 {"", ".+", -1, []string{""}},
448 {"", "", -1, []string{}},
449 {"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}},
450 {"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}},
451 {":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}},
454 func TestSplit(t
*testing
.T
) {
455 for i
, test
:= range splitTests
{
456 re
, err
:= Compile(test
.r
)
458 t
.Errorf("#%d: %q: compile error: %s", i
, test
.r
, err
.Error())
462 split
:= re
.Split(test
.s
, test
.n
)
463 if !reflect
.DeepEqual(split
, test
.out
) {
464 t
.Errorf("#%d: %q: got %q; want %q", i
, test
.r
, split
, test
.out
)
467 if QuoteMeta(test
.r
) == test
.r
{
468 strsplit
:= strings
.SplitN(test
.s
, test
.r
, test
.n
)
469 if !reflect
.DeepEqual(split
, strsplit
) {
470 t
.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i
, test
.s
, test
.r
, test
.n
, split
, strsplit
)
476 func BenchmarkLiteral(b
*testing
.B
) {
477 x
:= strings
.Repeat("x", 50) + "y"
479 re
:= MustCompile("y")
481 for i
:= 0; i
< b
.N
; i
++ {
482 if !re
.MatchString(x
) {
483 b
.Fatalf("no match!")
488 func BenchmarkNotLiteral(b
*testing
.B
) {
489 x
:= strings
.Repeat("x", 50) + "y"
491 re
:= MustCompile(".y")
493 for i
:= 0; i
< b
.N
; i
++ {
494 if !re
.MatchString(x
) {
495 b
.Fatalf("no match!")
500 func BenchmarkMatchClass(b
*testing
.B
) {
502 x
:= strings
.Repeat("xxxx", 20) + "w"
503 re
:= MustCompile("[abcdw]")
505 for i
:= 0; i
< b
.N
; i
++ {
506 if !re
.MatchString(x
) {
507 b
.Fatalf("no match!")
512 func BenchmarkMatchClass_InRange(b
*testing
.B
) {
514 // 'b' is between 'a' and 'c', so the charclass
515 // range checking is no help here.
516 x
:= strings
.Repeat("bbbb", 20) + "c"
517 re
:= MustCompile("[ac]")
519 for i
:= 0; i
< b
.N
; i
++ {
520 if !re
.MatchString(x
) {
521 b
.Fatalf("no match!")
526 func BenchmarkReplaceAll(b
*testing
.B
) {
527 x
:= "abcdefghijklmnopqrstuvwxyz"
529 re
:= MustCompile("[cjrw]")
531 for i
:= 0; i
< b
.N
; i
++ {
532 re
.ReplaceAllString(x
, "")
536 func BenchmarkAnchoredLiteralShortNonMatch(b
*testing
.B
) {
538 x
:= []byte("abcdefghijklmnopqrstuvwxyz")
539 re
:= MustCompile("^zbc(d|e)")
541 for i
:= 0; i
< b
.N
; i
++ {
546 func BenchmarkAnchoredLiteralLongNonMatch(b
*testing
.B
) {
548 x
:= []byte("abcdefghijklmnopqrstuvwxyz")
549 for i
:= 0; i
< 15; i
++ {
552 re
:= MustCompile("^zbc(d|e)")
554 for i
:= 0; i
< b
.N
; i
++ {
559 func BenchmarkAnchoredShortMatch(b
*testing
.B
) {
561 x
:= []byte("abcdefghijklmnopqrstuvwxyz")
562 re
:= MustCompile("^.bc(d|e)")
564 for i
:= 0; i
< b
.N
; i
++ {
569 func BenchmarkAnchoredLongMatch(b
*testing
.B
) {
571 x
:= []byte("abcdefghijklmnopqrstuvwxyz")
572 for i
:= 0; i
< 15; i
++ {
575 re
:= MustCompile("^.bc(d|e)")
577 for i
:= 0; i
< b
.N
; i
++ {