* reload1.c (eliminate_regs_1): Call gen_rtx_raw_SUBREG for SUBREGs
[official-gcc.git] / libgo / go / regexp / all_test.go
blobbeb46e70995140c8e0a0a5b84064ca0f25010602
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.
5 package regexp
7 import (
8 "reflect"
9 "regexp/syntax"
10 "strings"
11 "testing"
14 var goodRe = []string{
15 ``,
16 `.`,
17 `^.$`,
18 `a`,
19 `a*`,
20 `a+`,
21 `a?`,
22 `a|b`,
23 `a*|b*`,
24 `(a*|b)(c*|d)`,
25 `[a-z]`,
26 `[a-abc-c\-\]\[]`,
27 `[a-z]+`,
28 `[abc]`,
29 `[^1234]`,
30 `[^\n]`,
31 `\!\\`,
34 type stringError struct {
35 re string
36 err string
39 var badRe = []stringError{
40 {`*`, "missing argument to repetition operator: `*`"},
41 {`+`, "missing argument to repetition operator: `+`"},
42 {`?`, "missing argument to repetition operator: `?`"},
43 {`(abc`, "missing closing ): `(abc`"},
44 {`abc)`, "unexpected ): `abc)`"},
45 {`x[a-z`, "missing closing ]: `[a-z`"},
46 {`[z-a]`, "invalid character class range: `z-a`"},
47 {`abc\`, "trailing backslash at end of expression"},
48 {`a**`, "invalid nested repetition operator: `**`"},
49 {`a*+`, "invalid nested repetition operator: `*+`"},
50 {`\x`, "invalid escape sequence: `\\x`"},
53 func compileTest(t *testing.T, expr string, error string) *Regexp {
54 re, err := Compile(expr)
55 if error == "" && err != nil {
56 t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
58 if error != "" && err == nil {
59 t.Error("compiling `", expr, "`; missing error")
60 } else if error != "" && !strings.Contains(err.Error(), error) {
61 t.Error("compiling `", expr, "`; wrong error: ", err.Error(), "; want ", error)
63 return re
66 func TestGoodCompile(t *testing.T) {
67 for i := 0; i < len(goodRe); i++ {
68 compileTest(t, goodRe[i], "")
72 func TestBadCompile(t *testing.T) {
73 for i := 0; i < len(badRe); i++ {
74 compileTest(t, badRe[i].re, badRe[i].err)
78 func matchTest(t *testing.T, test *FindTest) {
79 re := compileTest(t, test.pat, "")
80 if re == nil {
81 return
83 m := re.MatchString(test.text)
84 if m != (len(test.matches) > 0) {
85 t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0)
87 // now try bytes
88 m = re.Match([]byte(test.text))
89 if m != (len(test.matches) > 0) {
90 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
94 func TestMatch(t *testing.T) {
95 for _, test := range findTests {
96 matchTest(t, &test)
100 func matchFunctionTest(t *testing.T, test *FindTest) {
101 m, err := MatchString(test.pat, test.text)
102 if err == nil {
103 return
105 if m != (len(test.matches) > 0) {
106 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
110 func TestMatchFunction(t *testing.T) {
111 for _, test := range findTests {
112 matchFunctionTest(t, &test)
116 func copyMatchTest(t *testing.T, test *FindTest) {
117 re := compileTest(t, test.pat, "")
118 if re == nil {
119 return
121 m1 := re.MatchString(test.text)
122 m2 := re.Copy().MatchString(test.text)
123 if m1 != m2 {
124 t.Errorf("Copied Regexp match failure on %s: original gave %t; copy gave %t; should be %t",
125 test, m1, m2, len(test.matches) > 0)
129 func TestCopyMatch(t *testing.T) {
130 for _, test := range findTests {
131 copyMatchTest(t, &test)
135 type ReplaceTest struct {
136 pattern, replacement, input, output string
139 var replaceTests = []ReplaceTest{
140 // Test empty input and/or replacement, with pattern that matches the empty string.
141 {"", "", "", ""},
142 {"", "x", "", "x"},
143 {"", "", "abc", "abc"},
144 {"", "x", "abc", "xaxbxcx"},
146 // Test empty input and/or replacement, with pattern that does not match the empty string.
147 {"b", "", "", ""},
148 {"b", "x", "", ""},
149 {"b", "", "abc", "ac"},
150 {"b", "x", "abc", "axc"},
151 {"y", "", "", ""},
152 {"y", "x", "", ""},
153 {"y", "", "abc", "abc"},
154 {"y", "x", "abc", "abc"},
156 // Multibyte characters -- verify that we don't try to match in the middle
157 // of a character.
158 {"[a-c]*", "x", "\u65e5", "x\u65e5x"},
159 {"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
161 // Start and end of a string.
162 {"^[a-c]*", "x", "abcdabc", "xdabc"},
163 {"[a-c]*$", "x", "abcdabc", "abcdx"},
164 {"^[a-c]*$", "x", "abcdabc", "abcdabc"},
165 {"^[a-c]*", "x", "abc", "x"},
166 {"[a-c]*$", "x", "abc", "x"},
167 {"^[a-c]*$", "x", "abc", "x"},
168 {"^[a-c]*", "x", "dabce", "xdabce"},
169 {"[a-c]*$", "x", "dabce", "dabcex"},
170 {"^[a-c]*$", "x", "dabce", "dabce"},
171 {"^[a-c]*", "x", "", "x"},
172 {"[a-c]*$", "x", "", "x"},
173 {"^[a-c]*$", "x", "", "x"},
175 {"^[a-c]+", "x", "abcdabc", "xdabc"},
176 {"[a-c]+$", "x", "abcdabc", "abcdx"},
177 {"^[a-c]+$", "x", "abcdabc", "abcdabc"},
178 {"^[a-c]+", "x", "abc", "x"},
179 {"[a-c]+$", "x", "abc", "x"},
180 {"^[a-c]+$", "x", "abc", "x"},
181 {"^[a-c]+", "x", "dabce", "dabce"},
182 {"[a-c]+$", "x", "dabce", "dabce"},
183 {"^[a-c]+$", "x", "dabce", "dabce"},
184 {"^[a-c]+", "x", "", ""},
185 {"[a-c]+$", "x", "", ""},
186 {"^[a-c]+$", "x", "", ""},
188 // Other cases.
189 {"abc", "def", "abcdefg", "defdefg"},
190 {"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
191 {"abc", "", "abcdabc", "d"},
192 {"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
193 {"abc", "d", "", ""},
194 {"abc", "d", "abc", "d"},
195 {".+", "x", "abc", "x"},
196 {"[a-c]*", "x", "def", "xdxexfx"},
197 {"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
198 {"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
200 // Substitutions
201 {"a+", "($0)", "banana", "b(a)n(a)n(a)"},
202 {"a+", "(${0})", "banana", "b(a)n(a)n(a)"},
203 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
204 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
205 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"},
206 {"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "},
207 {"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"},
208 {"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"},
209 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"},
210 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"},
211 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"},
212 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"},
213 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""},
214 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"},
215 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"},
216 {"a+", "${oops", "aaa", "${oops"},
217 {"a+", "$$", "aaa", "$"},
218 {"a+", "$", "aaa", "$"},
220 // Substitution when subexpression isn't found
221 {"(x)?", "$1", "123", "123"},
222 {"abc", "$1", "123", "123"},
224 // Substitutions involving a (x){0}
225 {"(a)(b){0}(c)", ".$1|$3.", "xacxacx", "x.a|c.x.a|c.x"},
226 {"(a)(((b))){0}c", ".$1.", "xacxacx", "x.a.x.a.x"},
227 {"((a(b){0}){3}){5}(h)", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
228 {"((a(b){0}){3}){5}h", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
231 var replaceLiteralTests = []ReplaceTest{
232 // Substitutions
233 {"a+", "($0)", "banana", "b($0)n($0)n($0)"},
234 {"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"},
235 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
236 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
237 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"},
238 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"},
239 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"},
240 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"},
241 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"},
242 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"},
243 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"},
244 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"},
245 {"a+", "${oops", "aaa", "${oops"},
246 {"a+", "$$", "aaa", "$$"},
247 {"a+", "$", "aaa", "$"},
250 type ReplaceFuncTest struct {
251 pattern string
252 replacement func(string) string
253 input, output string
256 var replaceFuncTests = []ReplaceFuncTest{
257 {"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
258 {"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
259 {"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
262 func TestReplaceAll(t *testing.T) {
263 for _, tc := range replaceTests {
264 re, err := Compile(tc.pattern)
265 if err != nil {
266 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
267 continue
269 actual := re.ReplaceAllString(tc.input, tc.replacement)
270 if actual != tc.output {
271 t.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q",
272 tc.pattern, tc.input, tc.replacement, actual, tc.output)
274 // now try bytes
275 actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
276 if actual != tc.output {
277 t.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q",
278 tc.pattern, tc.input, tc.replacement, actual, tc.output)
283 func TestReplaceAllLiteral(t *testing.T) {
284 // Run ReplaceAll tests that do not have $ expansions.
285 for _, tc := range replaceTests {
286 if strings.Contains(tc.replacement, "$") {
287 continue
289 re, err := Compile(tc.pattern)
290 if err != nil {
291 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
292 continue
294 actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
295 if actual != tc.output {
296 t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
297 tc.pattern, tc.input, tc.replacement, actual, tc.output)
299 // now try bytes
300 actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
301 if actual != tc.output {
302 t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
303 tc.pattern, tc.input, tc.replacement, actual, tc.output)
307 // Run literal-specific tests.
308 for _, tc := range replaceLiteralTests {
309 re, err := Compile(tc.pattern)
310 if err != nil {
311 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
312 continue
314 actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
315 if actual != tc.output {
316 t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
317 tc.pattern, tc.input, tc.replacement, actual, tc.output)
319 // now try bytes
320 actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
321 if actual != tc.output {
322 t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
323 tc.pattern, tc.input, tc.replacement, actual, tc.output)
328 func TestReplaceAllFunc(t *testing.T) {
329 for _, tc := range replaceFuncTests {
330 re, err := Compile(tc.pattern)
331 if err != nil {
332 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
333 continue
335 actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
336 if actual != tc.output {
337 t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
338 tc.pattern, tc.input, actual, tc.output)
340 // now try bytes
341 actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
342 if actual != tc.output {
343 t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
344 tc.pattern, tc.input, actual, tc.output)
349 type MetaTest struct {
350 pattern, output, literal string
351 isLiteral bool
354 var metaTests = []MetaTest{
355 {``, ``, ``, true},
356 {`foo`, `foo`, `foo`, true},
357 {`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
358 {`foo.\$`, `foo\.\\\$`, `foo`, false}, // has escaped operators and real operators
359 {`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
362 var literalPrefixTests = []MetaTest{
363 // See golang.org/issue/11175.
364 // output is unused.
365 {`^0^0$`, ``, `0`, false},
366 {`^0^`, ``, ``, false},
367 {`^0$`, ``, `0`, true},
368 {`$0^`, ``, ``, false},
369 {`$0$`, ``, ``, false},
370 {`^^0$$`, ``, ``, false},
371 {`^$^$`, ``, ``, false},
372 {`$$0^^`, ``, ``, false},
375 func TestQuoteMeta(t *testing.T) {
376 for _, tc := range metaTests {
377 // Verify that QuoteMeta returns the expected string.
378 quoted := QuoteMeta(tc.pattern)
379 if quoted != tc.output {
380 t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
381 tc.pattern, quoted, tc.output)
382 continue
385 // Verify that the quoted string is in fact treated as expected
386 // by Compile -- i.e. that it matches the original, unquoted string.
387 if tc.pattern != "" {
388 re, err := Compile(quoted)
389 if err != nil {
390 t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
391 continue
393 src := "abc" + tc.pattern + "def"
394 repl := "xyz"
395 replaced := re.ReplaceAllString(src, repl)
396 expected := "abcxyzdef"
397 if replaced != expected {
398 t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
399 tc.pattern, src, repl, replaced, expected)
405 func TestLiteralPrefix(t *testing.T) {
406 for _, tc := range append(metaTests, literalPrefixTests...) {
407 // Literal method needs to scan the pattern.
408 re := MustCompile(tc.pattern)
409 str, complete := re.LiteralPrefix()
410 if complete != tc.isLiteral {
411 t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
413 if str != tc.literal {
414 t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
419 type subexpCase struct {
420 input string
421 num int
422 names []string
425 var subexpCases = []subexpCase{
426 {``, 0, nil},
427 {`.*`, 0, nil},
428 {`abba`, 0, nil},
429 {`ab(b)a`, 1, []string{"", ""}},
430 {`ab(.*)a`, 1, []string{"", ""}},
431 {`(.*)ab(.*)a`, 2, []string{"", "", ""}},
432 {`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}},
433 {`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}},
434 {`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}},
435 {`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}},
436 {`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}},
439 func TestSubexp(t *testing.T) {
440 for _, c := range subexpCases {
441 re := MustCompile(c.input)
442 n := re.NumSubexp()
443 if n != c.num {
444 t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num)
445 continue
447 names := re.SubexpNames()
448 if len(names) != 1+n {
449 t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n)
450 continue
452 if c.names != nil {
453 for i := 0; i < 1+n; i++ {
454 if names[i] != c.names[i] {
455 t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i])
462 var splitTests = []struct {
463 s string
464 r string
465 n int
466 out []string
468 {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
469 {"foo:and:bar", ":", 1, []string{"foo:and:bar"}},
470 {"foo:and:bar", ":", 2, []string{"foo", "and:bar"}},
471 {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
472 {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
473 {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
474 {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
475 {"baabaab", "a*", -1, []string{"b", "b", "b"}},
476 {"baabaab", "ba*", -1, []string{"", "", "", ""}},
477 {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}},
478 {"foobar", "f+.*b+", -1, []string{"", "ar"}},
479 {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
480 {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}},
481 {"a,b,c,d,e,f", ",", 0, nil},
482 {",", ",", -1, []string{"", ""}},
483 {",,,", ",", -1, []string{"", "", "", ""}},
484 {"", ",", -1, []string{""}},
485 {"", ".*", -1, []string{""}},
486 {"", ".+", -1, []string{""}},
487 {"", "", -1, []string{}},
488 {"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}},
489 {"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}},
490 {":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}},
493 func TestSplit(t *testing.T) {
494 for i, test := range splitTests {
495 re, err := Compile(test.r)
496 if err != nil {
497 t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Error())
498 continue
501 split := re.Split(test.s, test.n)
502 if !reflect.DeepEqual(split, test.out) {
503 t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out)
506 if QuoteMeta(test.r) == test.r {
507 strsplit := strings.SplitN(test.s, test.r, test.n)
508 if !reflect.DeepEqual(split, strsplit) {
509 t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit)
515 // The following sequence of Match calls used to panic. See issue #12980.
516 func TestParseAndCompile(t *testing.T) {
517 expr := "a$"
518 s := "a\nb"
520 for i, tc := range []struct {
521 reFlags syntax.Flags
522 expMatch bool
524 {syntax.Perl | syntax.OneLine, false},
525 {syntax.Perl &^ syntax.OneLine, true},
527 parsed, err := syntax.Parse(expr, tc.reFlags)
528 if err != nil {
529 t.Fatalf("%d: parse: %v", i, err)
531 re, err := Compile(parsed.String())
532 if err != nil {
533 t.Fatalf("%d: compile: %v", i, err)
535 if match := re.MatchString(s); match != tc.expMatch {
536 t.Errorf("%d: %q.MatchString(%q)=%t; expected=%t", i, re, s, match, tc.expMatch)
541 // Check that one-pass cutoff does trigger.
542 func TestOnePassCutoff(t *testing.T) {
543 re, err := syntax.Parse(`^x{1,1000}y{1,1000}$`, syntax.Perl)
544 if err != nil {
545 t.Fatalf("parse: %v", err)
547 p, err := syntax.Compile(re.Simplify())
548 if err != nil {
549 t.Fatalf("compile: %v", err)
551 if compileOnePass(p) != notOnePass {
552 t.Fatalf("makeOnePass succeeded; wanted notOnePass")
556 // Check that the same machine can be used with the standard matcher
557 // and then the backtracker when there are no captures.
558 func TestSwitchBacktrack(t *testing.T) {
559 re := MustCompile(`a|b`)
560 long := make([]byte, maxBacktrackVector+1)
562 // The following sequence of Match calls used to panic. See issue #10319.
563 re.Match(long) // triggers standard matcher
564 re.Match(long[:1]) // triggers backtracker
567 func BenchmarkFind(b *testing.B) {
568 b.StopTimer()
569 re := MustCompile("a+b+")
570 wantSubs := "aaabb"
571 s := []byte("acbb" + wantSubs + "dd")
572 b.StartTimer()
573 b.ReportAllocs()
574 for i := 0; i < b.N; i++ {
575 subs := re.Find(s)
576 if string(subs) != wantSubs {
577 b.Fatalf("Find(%q) = %q; want %q", s, subs, wantSubs)
582 func BenchmarkFindString(b *testing.B) {
583 b.StopTimer()
584 re := MustCompile("a+b+")
585 wantSubs := "aaabb"
586 s := "acbb" + wantSubs + "dd"
587 b.StartTimer()
588 b.ReportAllocs()
589 for i := 0; i < b.N; i++ {
590 subs := re.FindString(s)
591 if subs != wantSubs {
592 b.Fatalf("FindString(%q) = %q; want %q", s, subs, wantSubs)
597 func BenchmarkFindSubmatch(b *testing.B) {
598 b.StopTimer()
599 re := MustCompile("a(a+b+)b")
600 wantSubs := "aaabb"
601 s := []byte("acbb" + wantSubs + "dd")
602 b.StartTimer()
603 b.ReportAllocs()
604 for i := 0; i < b.N; i++ {
605 subs := re.FindSubmatch(s)
606 if string(subs[0]) != wantSubs {
607 b.Fatalf("FindSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
609 if string(subs[1]) != "aab" {
610 b.Fatalf("FindSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
615 func BenchmarkFindStringSubmatch(b *testing.B) {
616 b.StopTimer()
617 re := MustCompile("a(a+b+)b")
618 wantSubs := "aaabb"
619 s := "acbb" + wantSubs + "dd"
620 b.StartTimer()
621 b.ReportAllocs()
622 for i := 0; i < b.N; i++ {
623 subs := re.FindStringSubmatch(s)
624 if subs[0] != wantSubs {
625 b.Fatalf("FindStringSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
627 if subs[1] != "aab" {
628 b.Fatalf("FindStringSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
633 func BenchmarkLiteral(b *testing.B) {
634 x := strings.Repeat("x", 50) + "y"
635 b.StopTimer()
636 re := MustCompile("y")
637 b.StartTimer()
638 for i := 0; i < b.N; i++ {
639 if !re.MatchString(x) {
640 b.Fatalf("no match!")
645 func BenchmarkNotLiteral(b *testing.B) {
646 x := strings.Repeat("x", 50) + "y"
647 b.StopTimer()
648 re := MustCompile(".y")
649 b.StartTimer()
650 for i := 0; i < b.N; i++ {
651 if !re.MatchString(x) {
652 b.Fatalf("no match!")
657 func BenchmarkMatchClass(b *testing.B) {
658 b.StopTimer()
659 x := strings.Repeat("xxxx", 20) + "w"
660 re := MustCompile("[abcdw]")
661 b.StartTimer()
662 for i := 0; i < b.N; i++ {
663 if !re.MatchString(x) {
664 b.Fatalf("no match!")
669 func BenchmarkMatchClass_InRange(b *testing.B) {
670 b.StopTimer()
671 // 'b' is between 'a' and 'c', so the charclass
672 // range checking is no help here.
673 x := strings.Repeat("bbbb", 20) + "c"
674 re := MustCompile("[ac]")
675 b.StartTimer()
676 for i := 0; i < b.N; i++ {
677 if !re.MatchString(x) {
678 b.Fatalf("no match!")
683 func BenchmarkReplaceAll(b *testing.B) {
684 x := "abcdefghijklmnopqrstuvwxyz"
685 b.StopTimer()
686 re := MustCompile("[cjrw]")
687 b.StartTimer()
688 for i := 0; i < b.N; i++ {
689 re.ReplaceAllString(x, "")
693 func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
694 b.StopTimer()
695 x := []byte("abcdefghijklmnopqrstuvwxyz")
696 re := MustCompile("^zbc(d|e)")
697 b.StartTimer()
698 for i := 0; i < b.N; i++ {
699 re.Match(x)
703 func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
704 b.StopTimer()
705 x := []byte("abcdefghijklmnopqrstuvwxyz")
706 for i := 0; i < 15; i++ {
707 x = append(x, x...)
709 re := MustCompile("^zbc(d|e)")
710 b.StartTimer()
711 for i := 0; i < b.N; i++ {
712 re.Match(x)
716 func BenchmarkAnchoredShortMatch(b *testing.B) {
717 b.StopTimer()
718 x := []byte("abcdefghijklmnopqrstuvwxyz")
719 re := MustCompile("^.bc(d|e)")
720 b.StartTimer()
721 for i := 0; i < b.N; i++ {
722 re.Match(x)
726 func BenchmarkAnchoredLongMatch(b *testing.B) {
727 b.StopTimer()
728 x := []byte("abcdefghijklmnopqrstuvwxyz")
729 for i := 0; i < 15; i++ {
730 x = append(x, x...)
732 re := MustCompile("^.bc(d|e)")
733 b.StartTimer()
734 for i := 0; i < b.N; i++ {
735 re.Match(x)
739 func BenchmarkOnePassShortA(b *testing.B) {
740 b.StopTimer()
741 x := []byte("abcddddddeeeededd")
742 re := MustCompile("^.bc(d|e)*$")
743 b.StartTimer()
744 for i := 0; i < b.N; i++ {
745 re.Match(x)
749 func BenchmarkNotOnePassShortA(b *testing.B) {
750 b.StopTimer()
751 x := []byte("abcddddddeeeededd")
752 re := MustCompile(".bc(d|e)*$")
753 b.StartTimer()
754 for i := 0; i < b.N; i++ {
755 re.Match(x)
759 func BenchmarkOnePassShortB(b *testing.B) {
760 b.StopTimer()
761 x := []byte("abcddddddeeeededd")
762 re := MustCompile("^.bc(?:d|e)*$")
763 b.StartTimer()
764 for i := 0; i < b.N; i++ {
765 re.Match(x)
769 func BenchmarkNotOnePassShortB(b *testing.B) {
770 b.StopTimer()
771 x := []byte("abcddddddeeeededd")
772 re := MustCompile(".bc(?:d|e)*$")
773 b.StartTimer()
774 for i := 0; i < b.N; i++ {
775 re.Match(x)
779 func BenchmarkOnePassLongPrefix(b *testing.B) {
780 b.StopTimer()
781 x := []byte("abcdefghijklmnopqrstuvwxyz")
782 re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$")
783 b.StartTimer()
784 for i := 0; i < b.N; i++ {
785 re.Match(x)
789 func BenchmarkOnePassLongNotPrefix(b *testing.B) {
790 b.StopTimer()
791 x := []byte("abcdefghijklmnopqrstuvwxyz")
792 re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$")
793 b.StartTimer()
794 for i := 0; i < b.N; i++ {
795 re.Match(x)
799 func BenchmarkMatchParallelShared(b *testing.B) {
800 x := []byte("this is a long line that contains foo bar baz")
801 re := MustCompile("foo (ba+r)? baz")
802 b.ResetTimer()
803 b.RunParallel(func(pb *testing.PB) {
804 for pb.Next() {
805 re.Match(x)
810 func BenchmarkMatchParallelCopied(b *testing.B) {
811 x := []byte("this is a long line that contains foo bar baz")
812 re := MustCompile("foo (ba+r)? baz")
813 b.ResetTimer()
814 b.RunParallel(func(pb *testing.PB) {
815 re := re.Copy()
816 for pb.Next() {
817 re.Match(x)
822 var sink string
824 func BenchmarkQuoteMetaAll(b *testing.B) {
825 s := string(specialBytes)
826 b.SetBytes(int64(len(s)))
827 b.ResetTimer()
828 for i := 0; i < b.N; i++ {
829 sink = QuoteMeta(s)
833 func BenchmarkQuoteMetaNone(b *testing.B) {
834 s := "abcdefghijklmnopqrstuvwxyz"
835 b.SetBytes(int64(len(s)))
836 b.ResetTimer()
837 for i := 0; i < b.N; i++ {
838 sink = QuoteMeta(s)