libgo: update to Go 1.11
[official-gcc.git] / libgo / go / regexp / all_test.go
blob0fabeae59fcbe7f61cc0bbf9dc81971559f1761d
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"
12 "unicode/utf8"
15 var goodRe = []string{
16 ``,
17 `.`,
18 `^.$`,
19 `a`,
20 `a*`,
21 `a+`,
22 `a?`,
23 `a|b`,
24 `a*|b*`,
25 `(a*|b)(c*|d)`,
26 `[a-z]`,
27 `[a-abc-c\-\]\[]`,
28 `[a-z]+`,
29 `[abc]`,
30 `[^1234]`,
31 `[^\n]`,
32 `\!\\`,
35 type stringError struct {
36 re string
37 err string
40 var badRe = []stringError{
41 {`*`, "missing argument to repetition operator: `*`"},
42 {`+`, "missing argument to repetition operator: `+`"},
43 {`?`, "missing argument to repetition operator: `?`"},
44 {`(abc`, "missing closing ): `(abc`"},
45 {`abc)`, "unexpected ): `abc)`"},
46 {`x[a-z`, "missing closing ]: `[a-z`"},
47 {`[z-a]`, "invalid character class range: `z-a`"},
48 {`abc\`, "trailing backslash at end of expression"},
49 {`a**`, "invalid nested repetition operator: `**`"},
50 {`a*+`, "invalid nested repetition operator: `*+`"},
51 {`\x`, "invalid escape sequence: `\\x`"},
54 func compileTest(t *testing.T, expr string, error string) *Regexp {
55 re, err := Compile(expr)
56 if error == "" && err != nil {
57 t.Error("compiling `", expr, "`; unexpected error: ", err.Error())
59 if error != "" && err == nil {
60 t.Error("compiling `", expr, "`; missing error")
61 } else if error != "" && !strings.Contains(err.Error(), error) {
62 t.Error("compiling `", expr, "`; wrong error: ", err.Error(), "; want ", error)
64 return re
67 func TestGoodCompile(t *testing.T) {
68 for i := 0; i < len(goodRe); i++ {
69 compileTest(t, goodRe[i], "")
73 func TestBadCompile(t *testing.T) {
74 for i := 0; i < len(badRe); i++ {
75 compileTest(t, badRe[i].re, badRe[i].err)
79 func matchTest(t *testing.T, test *FindTest) {
80 re := compileTest(t, test.pat, "")
81 if re == nil {
82 return
84 m := re.MatchString(test.text)
85 if m != (len(test.matches) > 0) {
86 t.Errorf("MatchString failure on %s: %t should be %t", test, m, len(test.matches) > 0)
88 // now try bytes
89 m = re.Match([]byte(test.text))
90 if m != (len(test.matches) > 0) {
91 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
95 func TestMatch(t *testing.T) {
96 for _, test := range findTests {
97 matchTest(t, &test)
101 func matchFunctionTest(t *testing.T, test *FindTest) {
102 m, err := MatchString(test.pat, test.text)
103 if err == nil {
104 return
106 if m != (len(test.matches) > 0) {
107 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0)
111 func TestMatchFunction(t *testing.T) {
112 for _, test := range findTests {
113 matchFunctionTest(t, &test)
117 func copyMatchTest(t *testing.T, test *FindTest) {
118 re := compileTest(t, test.pat, "")
119 if re == nil {
120 return
122 m1 := re.MatchString(test.text)
123 m2 := re.Copy().MatchString(test.text)
124 if m1 != m2 {
125 t.Errorf("Copied Regexp match failure on %s: original gave %t; copy gave %t; should be %t",
126 test, m1, m2, len(test.matches) > 0)
130 func TestCopyMatch(t *testing.T) {
131 for _, test := range findTests {
132 copyMatchTest(t, &test)
136 type ReplaceTest struct {
137 pattern, replacement, input, output string
140 var replaceTests = []ReplaceTest{
141 // Test empty input and/or replacement, with pattern that matches the empty string.
142 {"", "", "", ""},
143 {"", "x", "", "x"},
144 {"", "", "abc", "abc"},
145 {"", "x", "abc", "xaxbxcx"},
147 // Test empty input and/or replacement, with pattern that does not match the empty string.
148 {"b", "", "", ""},
149 {"b", "x", "", ""},
150 {"b", "", "abc", "ac"},
151 {"b", "x", "abc", "axc"},
152 {"y", "", "", ""},
153 {"y", "x", "", ""},
154 {"y", "", "abc", "abc"},
155 {"y", "x", "abc", "abc"},
157 // Multibyte characters -- verify that we don't try to match in the middle
158 // of a character.
159 {"[a-c]*", "x", "\u65e5", "x\u65e5x"},
160 {"[^\u65e5]", "x", "abc\u65e5def", "xxx\u65e5xxx"},
162 // Start and end of a string.
163 {"^[a-c]*", "x", "abcdabc", "xdabc"},
164 {"[a-c]*$", "x", "abcdabc", "abcdx"},
165 {"^[a-c]*$", "x", "abcdabc", "abcdabc"},
166 {"^[a-c]*", "x", "abc", "x"},
167 {"[a-c]*$", "x", "abc", "x"},
168 {"^[a-c]*$", "x", "abc", "x"},
169 {"^[a-c]*", "x", "dabce", "xdabce"},
170 {"[a-c]*$", "x", "dabce", "dabcex"},
171 {"^[a-c]*$", "x", "dabce", "dabce"},
172 {"^[a-c]*", "x", "", "x"},
173 {"[a-c]*$", "x", "", "x"},
174 {"^[a-c]*$", "x", "", "x"},
176 {"^[a-c]+", "x", "abcdabc", "xdabc"},
177 {"[a-c]+$", "x", "abcdabc", "abcdx"},
178 {"^[a-c]+$", "x", "abcdabc", "abcdabc"},
179 {"^[a-c]+", "x", "abc", "x"},
180 {"[a-c]+$", "x", "abc", "x"},
181 {"^[a-c]+$", "x", "abc", "x"},
182 {"^[a-c]+", "x", "dabce", "dabce"},
183 {"[a-c]+$", "x", "dabce", "dabce"},
184 {"^[a-c]+$", "x", "dabce", "dabce"},
185 {"^[a-c]+", "x", "", ""},
186 {"[a-c]+$", "x", "", ""},
187 {"^[a-c]+$", "x", "", ""},
189 // Other cases.
190 {"abc", "def", "abcdefg", "defdefg"},
191 {"bc", "BC", "abcbcdcdedef", "aBCBCdcdedef"},
192 {"abc", "", "abcdabc", "d"},
193 {"x", "xXx", "xxxXxxx", "xXxxXxxXxXxXxxXxxXx"},
194 {"abc", "d", "", ""},
195 {"abc", "d", "abc", "d"},
196 {".+", "x", "abc", "x"},
197 {"[a-c]*", "x", "def", "xdxexfx"},
198 {"[a-c]+", "x", "abcbcdcdedef", "xdxdedef"},
199 {"[a-c]*", "x", "abcbcdcdedef", "xdxdxexdxexfx"},
201 // Substitutions
202 {"a+", "($0)", "banana", "b(a)n(a)n(a)"},
203 {"a+", "(${0})", "banana", "b(a)n(a)n(a)"},
204 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
205 {"a+", "(${0})$0", "banana", "b(a)an(a)an(a)a"},
206 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, world"},
207 {"hello, (.+)", "goodbye, $1x", "hello, world", "goodbye, "},
208 {"hello, (.+)", "goodbye, ${1}x", "hello, world", "goodbye, worldx"},
209 {"hello, (.+)", "<$0><$1><$2><$3>", "hello, world", "<hello, world><world><><>"},
210 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, world!"},
211 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, world"},
212 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "hihihi"},
213 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "byebyebye"},
214 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", ""},
215 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "hiyz"},
216 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $x"},
217 {"a+", "${oops", "aaa", "${oops"},
218 {"a+", "$$", "aaa", "$"},
219 {"a+", "$", "aaa", "$"},
221 // Substitution when subexpression isn't found
222 {"(x)?", "$1", "123", "123"},
223 {"abc", "$1", "123", "123"},
225 // Substitutions involving a (x){0}
226 {"(a)(b){0}(c)", ".$1|$3.", "xacxacx", "x.a|c.x.a|c.x"},
227 {"(a)(((b))){0}c", ".$1.", "xacxacx", "x.a.x.a.x"},
228 {"((a(b){0}){3}){5}(h)", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
229 {"((a(b){0}){3}){5}h", "y caramb$2", "say aaaaaaaaaaaaaaaah", "say ay caramba"},
232 var replaceLiteralTests = []ReplaceTest{
233 // Substitutions
234 {"a+", "($0)", "banana", "b($0)n($0)n($0)"},
235 {"a+", "(${0})", "banana", "b(${0})n(${0})n(${0})"},
236 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
237 {"a+", "(${0})$0", "banana", "b(${0})$0n(${0})$0n(${0})$0"},
238 {"hello, (.+)", "goodbye, ${1}", "hello, world", "goodbye, ${1}"},
239 {"hello, (?P<noun>.+)", "goodbye, $noun!", "hello, world", "goodbye, $noun!"},
240 {"hello, (?P<noun>.+)", "goodbye, ${noun}", "hello, world", "goodbye, ${noun}"},
241 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "hi", "$x$x$x"},
242 {"(?P<x>hi)|(?P<x>bye)", "$x$x$x", "bye", "$x$x$x"},
243 {"(?P<x>hi)|(?P<x>bye)", "$xyz", "hi", "$xyz"},
244 {"(?P<x>hi)|(?P<x>bye)", "${x}yz", "hi", "${x}yz"},
245 {"(?P<x>hi)|(?P<x>bye)", "hello $$x", "hi", "hello $$x"},
246 {"a+", "${oops", "aaa", "${oops"},
247 {"a+", "$$", "aaa", "$$"},
248 {"a+", "$", "aaa", "$"},
251 type ReplaceFuncTest struct {
252 pattern string
253 replacement func(string) string
254 input, output string
257 var replaceFuncTests = []ReplaceFuncTest{
258 {"[a-c]", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxayxbyxcydef"},
259 {"[a-c]+", func(s string) string { return "x" + s + "y" }, "defabcdef", "defxabcydef"},
260 {"[a-c]*", func(s string) string { return "x" + s + "y" }, "defabcdef", "xydxyexyfxabcydxyexyfxy"},
263 func TestReplaceAll(t *testing.T) {
264 for _, tc := range replaceTests {
265 re, err := Compile(tc.pattern)
266 if err != nil {
267 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
268 continue
270 actual := re.ReplaceAllString(tc.input, tc.replacement)
271 if actual != tc.output {
272 t.Errorf("%q.ReplaceAllString(%q,%q) = %q; want %q",
273 tc.pattern, tc.input, tc.replacement, actual, tc.output)
275 // now try bytes
276 actual = string(re.ReplaceAll([]byte(tc.input), []byte(tc.replacement)))
277 if actual != tc.output {
278 t.Errorf("%q.ReplaceAll(%q,%q) = %q; want %q",
279 tc.pattern, tc.input, tc.replacement, actual, tc.output)
284 func TestReplaceAllLiteral(t *testing.T) {
285 // Run ReplaceAll tests that do not have $ expansions.
286 for _, tc := range replaceTests {
287 if strings.Contains(tc.replacement, "$") {
288 continue
290 re, err := Compile(tc.pattern)
291 if err != nil {
292 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
293 continue
295 actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
296 if actual != tc.output {
297 t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
298 tc.pattern, tc.input, tc.replacement, actual, tc.output)
300 // now try bytes
301 actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
302 if actual != tc.output {
303 t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
304 tc.pattern, tc.input, tc.replacement, actual, tc.output)
308 // Run literal-specific tests.
309 for _, tc := range replaceLiteralTests {
310 re, err := Compile(tc.pattern)
311 if err != nil {
312 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
313 continue
315 actual := re.ReplaceAllLiteralString(tc.input, tc.replacement)
316 if actual != tc.output {
317 t.Errorf("%q.ReplaceAllLiteralString(%q,%q) = %q; want %q",
318 tc.pattern, tc.input, tc.replacement, actual, tc.output)
320 // now try bytes
321 actual = string(re.ReplaceAllLiteral([]byte(tc.input), []byte(tc.replacement)))
322 if actual != tc.output {
323 t.Errorf("%q.ReplaceAllLiteral(%q,%q) = %q; want %q",
324 tc.pattern, tc.input, tc.replacement, actual, tc.output)
329 func TestReplaceAllFunc(t *testing.T) {
330 for _, tc := range replaceFuncTests {
331 re, err := Compile(tc.pattern)
332 if err != nil {
333 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err)
334 continue
336 actual := re.ReplaceAllStringFunc(tc.input, tc.replacement)
337 if actual != tc.output {
338 t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
339 tc.pattern, tc.input, actual, tc.output)
341 // now try bytes
342 actual = string(re.ReplaceAllFunc([]byte(tc.input), func(s []byte) []byte { return []byte(tc.replacement(string(s))) }))
343 if actual != tc.output {
344 t.Errorf("%q.ReplaceFunc(%q,fn) = %q; want %q",
345 tc.pattern, tc.input, actual, tc.output)
350 type MetaTest struct {
351 pattern, output, literal string
352 isLiteral bool
355 var metaTests = []MetaTest{
356 {``, ``, ``, true},
357 {`foo`, `foo`, `foo`, true},
358 {`日本語+`, `日本語\+`, `日本語`, false},
359 {`foo\.\$`, `foo\\\.\\\$`, `foo.$`, true}, // has meta but no operator
360 {`foo.\$`, `foo\.\\\$`, `foo`, false}, // has escaped operators and real operators
361 {`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[\{\]\}\\\|,<\.>/\?~`, `!@#`, false},
364 var literalPrefixTests = []MetaTest{
365 // See golang.org/issue/11175.
366 // output is unused.
367 {`^0^0$`, ``, `0`, false},
368 {`^0^`, ``, ``, false},
369 {`^0$`, ``, `0`, true},
370 {`$0^`, ``, ``, false},
371 {`$0$`, ``, ``, false},
372 {`^^0$$`, ``, ``, false},
373 {`^$^$`, ``, ``, false},
374 {`$$0^^`, ``, ``, false},
377 func TestQuoteMeta(t *testing.T) {
378 for _, tc := range metaTests {
379 // Verify that QuoteMeta returns the expected string.
380 quoted := QuoteMeta(tc.pattern)
381 if quoted != tc.output {
382 t.Errorf("QuoteMeta(`%s`) = `%s`; want `%s`",
383 tc.pattern, quoted, tc.output)
384 continue
387 // Verify that the quoted string is in fact treated as expected
388 // by Compile -- i.e. that it matches the original, unquoted string.
389 if tc.pattern != "" {
390 re, err := Compile(quoted)
391 if err != nil {
392 t.Errorf("Unexpected error compiling QuoteMeta(`%s`): %v", tc.pattern, err)
393 continue
395 src := "abc" + tc.pattern + "def"
396 repl := "xyz"
397 replaced := re.ReplaceAllString(src, repl)
398 expected := "abcxyzdef"
399 if replaced != expected {
400 t.Errorf("QuoteMeta(`%s`).Replace(`%s`,`%s`) = `%s`; want `%s`",
401 tc.pattern, src, repl, replaced, expected)
407 func TestLiteralPrefix(t *testing.T) {
408 for _, tc := range append(metaTests, literalPrefixTests...) {
409 // Literal method needs to scan the pattern.
410 re := MustCompile(tc.pattern)
411 str, complete := re.LiteralPrefix()
412 if complete != tc.isLiteral {
413 t.Errorf("LiteralPrefix(`%s`) = %t; want %t", tc.pattern, complete, tc.isLiteral)
415 if str != tc.literal {
416 t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal)
421 type subexpCase struct {
422 input string
423 num int
424 names []string
427 var subexpCases = []subexpCase{
428 {``, 0, nil},
429 {`.*`, 0, nil},
430 {`abba`, 0, nil},
431 {`ab(b)a`, 1, []string{"", ""}},
432 {`ab(.*)a`, 1, []string{"", ""}},
433 {`(.*)ab(.*)a`, 2, []string{"", "", ""}},
434 {`(.*)(ab)(.*)a`, 3, []string{"", "", "", ""}},
435 {`(.*)((a)b)(.*)a`, 4, []string{"", "", "", "", ""}},
436 {`(.*)(\(ab)(.*)a`, 3, []string{"", "", "", ""}},
437 {`(.*)(\(a\)b)(.*)a`, 3, []string{"", "", "", ""}},
438 {`(?P<foo>.*)(?P<bar>(a)b)(?P<foo>.*)a`, 4, []string{"", "foo", "bar", "", "foo"}},
441 func TestSubexp(t *testing.T) {
442 for _, c := range subexpCases {
443 re := MustCompile(c.input)
444 n := re.NumSubexp()
445 if n != c.num {
446 t.Errorf("%q: NumSubexp = %d, want %d", c.input, n, c.num)
447 continue
449 names := re.SubexpNames()
450 if len(names) != 1+n {
451 t.Errorf("%q: len(SubexpNames) = %d, want %d", c.input, len(names), n)
452 continue
454 if c.names != nil {
455 for i := 0; i < 1+n; i++ {
456 if names[i] != c.names[i] {
457 t.Errorf("%q: SubexpNames[%d] = %q, want %q", c.input, i, names[i], c.names[i])
464 var splitTests = []struct {
465 s string
466 r string
467 n int
468 out []string
470 {"foo:and:bar", ":", -1, []string{"foo", "and", "bar"}},
471 {"foo:and:bar", ":", 1, []string{"foo:and:bar"}},
472 {"foo:and:bar", ":", 2, []string{"foo", "and:bar"}},
473 {"foo:and:bar", "foo", -1, []string{"", ":and:bar"}},
474 {"foo:and:bar", "bar", -1, []string{"foo:and:", ""}},
475 {"foo:and:bar", "baz", -1, []string{"foo:and:bar"}},
476 {"baabaab", "a", -1, []string{"b", "", "b", "", "b"}},
477 {"baabaab", "a*", -1, []string{"b", "b", "b"}},
478 {"baabaab", "ba*", -1, []string{"", "", "", ""}},
479 {"foobar", "f*b*", -1, []string{"", "o", "o", "a", "r"}},
480 {"foobar", "f+.*b+", -1, []string{"", "ar"}},
481 {"foobooboar", "o{2}", -1, []string{"f", "b", "boar"}},
482 {"a,b,c,d,e,f", ",", 3, []string{"a", "b", "c,d,e,f"}},
483 {"a,b,c,d,e,f", ",", 0, nil},
484 {",", ",", -1, []string{"", ""}},
485 {",,,", ",", -1, []string{"", "", "", ""}},
486 {"", ",", -1, []string{""}},
487 {"", ".*", -1, []string{""}},
488 {"", ".+", -1, []string{""}},
489 {"", "", -1, []string{}},
490 {"foobar", "", -1, []string{"f", "o", "o", "b", "a", "r"}},
491 {"abaabaccadaaae", "a*", 5, []string{"", "b", "b", "c", "cadaaae"}},
492 {":x:y:z:", ":", -1, []string{"", "x", "y", "z", ""}},
495 func TestSplit(t *testing.T) {
496 for i, test := range splitTests {
497 re, err := Compile(test.r)
498 if err != nil {
499 t.Errorf("#%d: %q: compile error: %s", i, test.r, err.Error())
500 continue
503 split := re.Split(test.s, test.n)
504 if !reflect.DeepEqual(split, test.out) {
505 t.Errorf("#%d: %q: got %q; want %q", i, test.r, split, test.out)
508 if QuoteMeta(test.r) == test.r {
509 strsplit := strings.SplitN(test.s, test.r, test.n)
510 if !reflect.DeepEqual(split, strsplit) {
511 t.Errorf("#%d: Split(%q, %q, %d): regexp vs strings mismatch\nregexp=%q\nstrings=%q", i, test.s, test.r, test.n, split, strsplit)
517 // The following sequence of Match calls used to panic. See issue #12980.
518 func TestParseAndCompile(t *testing.T) {
519 expr := "a$"
520 s := "a\nb"
522 for i, tc := range []struct {
523 reFlags syntax.Flags
524 expMatch bool
526 {syntax.Perl | syntax.OneLine, false},
527 {syntax.Perl &^ syntax.OneLine, true},
529 parsed, err := syntax.Parse(expr, tc.reFlags)
530 if err != nil {
531 t.Fatalf("%d: parse: %v", i, err)
533 re, err := Compile(parsed.String())
534 if err != nil {
535 t.Fatalf("%d: compile: %v", i, err)
537 if match := re.MatchString(s); match != tc.expMatch {
538 t.Errorf("%d: %q.MatchString(%q)=%t; expected=%t", i, re, s, match, tc.expMatch)
543 // Check that one-pass cutoff does trigger.
544 func TestOnePassCutoff(t *testing.T) {
545 re, err := syntax.Parse(`^x{1,1000}y{1,1000}$`, syntax.Perl)
546 if err != nil {
547 t.Fatalf("parse: %v", err)
549 p, err := syntax.Compile(re.Simplify())
550 if err != nil {
551 t.Fatalf("compile: %v", err)
553 if compileOnePass(p) != notOnePass {
554 t.Fatalf("makeOnePass succeeded; wanted notOnePass")
558 // Check that the same machine can be used with the standard matcher
559 // and then the backtracker when there are no captures.
560 func TestSwitchBacktrack(t *testing.T) {
561 re := MustCompile(`a|b`)
562 long := make([]byte, maxBacktrackVector+1)
564 // The following sequence of Match calls used to panic. See issue #10319.
565 re.Match(long) // triggers standard matcher
566 re.Match(long[:1]) // triggers backtracker
569 func BenchmarkFind(b *testing.B) {
570 b.StopTimer()
571 re := MustCompile("a+b+")
572 wantSubs := "aaabb"
573 s := []byte("acbb" + wantSubs + "dd")
574 b.StartTimer()
575 b.ReportAllocs()
576 for i := 0; i < b.N; i++ {
577 subs := re.Find(s)
578 if string(subs) != wantSubs {
579 b.Fatalf("Find(%q) = %q; want %q", s, subs, wantSubs)
584 func BenchmarkFindAllNoMatches(b *testing.B) {
585 re := MustCompile("a+b+")
586 s := []byte("acddee")
587 b.ReportAllocs()
588 b.ResetTimer()
589 for i := 0; i < b.N; i++ {
590 all := re.FindAll(s, -1)
591 if all != nil {
592 b.Fatalf("FindAll(%q) = %q; want nil", s, all)
597 func BenchmarkFindString(b *testing.B) {
598 b.StopTimer()
599 re := MustCompile("a+b+")
600 wantSubs := "aaabb"
601 s := "acbb" + wantSubs + "dd"
602 b.StartTimer()
603 b.ReportAllocs()
604 for i := 0; i < b.N; i++ {
605 subs := re.FindString(s)
606 if subs != wantSubs {
607 b.Fatalf("FindString(%q) = %q; want %q", s, subs, wantSubs)
612 func BenchmarkFindSubmatch(b *testing.B) {
613 b.StopTimer()
614 re := MustCompile("a(a+b+)b")
615 wantSubs := "aaabb"
616 s := []byte("acbb" + wantSubs + "dd")
617 b.StartTimer()
618 b.ReportAllocs()
619 for i := 0; i < b.N; i++ {
620 subs := re.FindSubmatch(s)
621 if string(subs[0]) != wantSubs {
622 b.Fatalf("FindSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
624 if string(subs[1]) != "aab" {
625 b.Fatalf("FindSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
630 func BenchmarkFindStringSubmatch(b *testing.B) {
631 b.StopTimer()
632 re := MustCompile("a(a+b+)b")
633 wantSubs := "aaabb"
634 s := "acbb" + wantSubs + "dd"
635 b.StartTimer()
636 b.ReportAllocs()
637 for i := 0; i < b.N; i++ {
638 subs := re.FindStringSubmatch(s)
639 if subs[0] != wantSubs {
640 b.Fatalf("FindStringSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
642 if subs[1] != "aab" {
643 b.Fatalf("FindStringSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
648 func BenchmarkLiteral(b *testing.B) {
649 x := strings.Repeat("x", 50) + "y"
650 b.StopTimer()
651 re := MustCompile("y")
652 b.StartTimer()
653 for i := 0; i < b.N; i++ {
654 if !re.MatchString(x) {
655 b.Fatalf("no match!")
660 func BenchmarkNotLiteral(b *testing.B) {
661 x := strings.Repeat("x", 50) + "y"
662 b.StopTimer()
663 re := MustCompile(".y")
664 b.StartTimer()
665 for i := 0; i < b.N; i++ {
666 if !re.MatchString(x) {
667 b.Fatalf("no match!")
672 func BenchmarkMatchClass(b *testing.B) {
673 b.StopTimer()
674 x := strings.Repeat("xxxx", 20) + "w"
675 re := MustCompile("[abcdw]")
676 b.StartTimer()
677 for i := 0; i < b.N; i++ {
678 if !re.MatchString(x) {
679 b.Fatalf("no match!")
684 func BenchmarkMatchClass_InRange(b *testing.B) {
685 b.StopTimer()
686 // 'b' is between 'a' and 'c', so the charclass
687 // range checking is no help here.
688 x := strings.Repeat("bbbb", 20) + "c"
689 re := MustCompile("[ac]")
690 b.StartTimer()
691 for i := 0; i < b.N; i++ {
692 if !re.MatchString(x) {
693 b.Fatalf("no match!")
698 func BenchmarkReplaceAll(b *testing.B) {
699 x := "abcdefghijklmnopqrstuvwxyz"
700 b.StopTimer()
701 re := MustCompile("[cjrw]")
702 b.StartTimer()
703 for i := 0; i < b.N; i++ {
704 re.ReplaceAllString(x, "")
708 func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
709 b.StopTimer()
710 x := []byte("abcdefghijklmnopqrstuvwxyz")
711 re := MustCompile("^zbc(d|e)")
712 b.StartTimer()
713 for i := 0; i < b.N; i++ {
714 re.Match(x)
718 func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
719 b.StopTimer()
720 x := []byte("abcdefghijklmnopqrstuvwxyz")
721 for i := 0; i < 15; i++ {
722 x = append(x, x...)
724 re := MustCompile("^zbc(d|e)")
725 b.StartTimer()
726 for i := 0; i < b.N; i++ {
727 re.Match(x)
731 func BenchmarkAnchoredShortMatch(b *testing.B) {
732 b.StopTimer()
733 x := []byte("abcdefghijklmnopqrstuvwxyz")
734 re := MustCompile("^.bc(d|e)")
735 b.StartTimer()
736 for i := 0; i < b.N; i++ {
737 re.Match(x)
741 func BenchmarkAnchoredLongMatch(b *testing.B) {
742 b.StopTimer()
743 x := []byte("abcdefghijklmnopqrstuvwxyz")
744 for i := 0; i < 15; i++ {
745 x = append(x, x...)
747 re := MustCompile("^.bc(d|e)")
748 b.StartTimer()
749 for i := 0; i < b.N; i++ {
750 re.Match(x)
754 func BenchmarkOnePassShortA(b *testing.B) {
755 b.StopTimer()
756 x := []byte("abcddddddeeeededd")
757 re := MustCompile("^.bc(d|e)*$")
758 b.StartTimer()
759 for i := 0; i < b.N; i++ {
760 re.Match(x)
764 func BenchmarkNotOnePassShortA(b *testing.B) {
765 b.StopTimer()
766 x := []byte("abcddddddeeeededd")
767 re := MustCompile(".bc(d|e)*$")
768 b.StartTimer()
769 for i := 0; i < b.N; i++ {
770 re.Match(x)
774 func BenchmarkOnePassShortB(b *testing.B) {
775 b.StopTimer()
776 x := []byte("abcddddddeeeededd")
777 re := MustCompile("^.bc(?:d|e)*$")
778 b.StartTimer()
779 for i := 0; i < b.N; i++ {
780 re.Match(x)
784 func BenchmarkNotOnePassShortB(b *testing.B) {
785 b.StopTimer()
786 x := []byte("abcddddddeeeededd")
787 re := MustCompile(".bc(?:d|e)*$")
788 b.StartTimer()
789 for i := 0; i < b.N; i++ {
790 re.Match(x)
794 func BenchmarkOnePassLongPrefix(b *testing.B) {
795 b.StopTimer()
796 x := []byte("abcdefghijklmnopqrstuvwxyz")
797 re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$")
798 b.StartTimer()
799 for i := 0; i < b.N; i++ {
800 re.Match(x)
804 func BenchmarkOnePassLongNotPrefix(b *testing.B) {
805 b.StopTimer()
806 x := []byte("abcdefghijklmnopqrstuvwxyz")
807 re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$")
808 b.StartTimer()
809 for i := 0; i < b.N; i++ {
810 re.Match(x)
814 func BenchmarkMatchParallelShared(b *testing.B) {
815 x := []byte("this is a long line that contains foo bar baz")
816 re := MustCompile("foo (ba+r)? baz")
817 b.ResetTimer()
818 b.RunParallel(func(pb *testing.PB) {
819 for pb.Next() {
820 re.Match(x)
825 func BenchmarkMatchParallelCopied(b *testing.B) {
826 x := []byte("this is a long line that contains foo bar baz")
827 re := MustCompile("foo (ba+r)? baz")
828 b.ResetTimer()
829 b.RunParallel(func(pb *testing.PB) {
830 re := re.Copy()
831 for pb.Next() {
832 re.Match(x)
837 var sink string
839 func BenchmarkQuoteMetaAll(b *testing.B) {
840 specials := make([]byte, 0)
841 for i := byte(0); i < utf8.RuneSelf; i++ {
842 if special(i) {
843 specials = append(specials, i)
846 s := string(specials)
847 b.SetBytes(int64(len(s)))
848 b.ResetTimer()
849 for i := 0; i < b.N; i++ {
850 sink = QuoteMeta(s)
854 func BenchmarkQuoteMetaNone(b *testing.B) {
855 s := "abcdefghijklmnopqrstuvwxyz"
856 b.SetBytes(int64(len(s)))
857 b.ResetTimer()
858 for i := 0; i < b.N; i++ {
859 sink = QuoteMeta(s)