github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/regexp/all_test.go (about) 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. 4 5 package regexp 6 7 import ( 8 "reflect" 9 "regexp/syntax" 10 "strings" 11 "testing" 12 ) 13 14 var good_re = []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 `\!\\`, 32 } 33 34 type stringError struct { 35 re string 36 err string 37 } 38 39 var bad_re = []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`"}, 51 } 52 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()) 57 } 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) 62 } 63 return re 64 } 65 66 func TestGoodCompile(t *testing.T) { 67 for i := 0; i < len(good_re); i++ { 68 compileTest(t, good_re[i], "") 69 } 70 } 71 72 func TestBadCompile(t *testing.T) { 73 for i := 0; i < len(bad_re); i++ { 74 compileTest(t, bad_re[i].re, bad_re[i].err) 75 } 76 } 77 78 func matchTest(t *testing.T, test *FindTest) { 79 re := compileTest(t, test.pat, "") 80 if re == nil { 81 return 82 } 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) 86 } 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) 91 } 92 } 93 94 func TestMatch(t *testing.T) { 95 for _, test := range findTests { 96 matchTest(t, &test) 97 } 98 } 99 100 func matchFunctionTest(t *testing.T, test *FindTest) { 101 m, err := MatchString(test.pat, test.text) 102 if err == nil { 103 return 104 } 105 if m != (len(test.matches) > 0) { 106 t.Errorf("Match failure on %s: %t should be %t", test, m, len(test.matches) > 0) 107 } 108 } 109 110 func TestMatchFunction(t *testing.T) { 111 for _, test := range findTests { 112 matchFunctionTest(t, &test) 113 } 114 } 115 116 func copyMatchTest(t *testing.T, test *FindTest) { 117 re := compileTest(t, test.pat, "") 118 if re == nil { 119 return 120 } 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) 126 } 127 } 128 129 func TestCopyMatch(t *testing.T) { 130 for _, test := range findTests { 131 copyMatchTest(t, &test) 132 } 133 } 134 135 type ReplaceTest struct { 136 pattern, replacement, input, output string 137 } 138 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"}, 145 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"}, 155 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"}, 160 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"}, 174 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", "", ""}, 187 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"}, 199 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", "$"}, 219 220 // Substitution when subexpression isn't found 221 {"(x)?", "$1", "123", "123"}, 222 {"abc", "$1", "123", "123"}, 223 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"}, 229 } 230 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", "$"}, 248 } 249 250 type ReplaceFuncTest struct { 251 pattern string 252 replacement func(string) string 253 input, output string 254 } 255 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"}, 260 } 261 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 268 } 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) 273 } 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) 279 } 280 } 281 } 282 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 288 } 289 re, err := Compile(tc.pattern) 290 if err != nil { 291 t.Errorf("Unexpected error compiling %q: %v", tc.pattern, err) 292 continue 293 } 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) 298 } 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) 304 } 305 } 306 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 313 } 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) 318 } 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) 324 } 325 } 326 } 327 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 334 } 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) 339 } 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) 345 } 346 } 347 } 348 349 type MetaTest struct { 350 pattern, output, literal string 351 isLiteral bool 352 } 353 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}, 360 } 361 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}, 373 } 374 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 383 } 384 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 392 } 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) 400 } 401 } 402 } 403 } 404 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) 412 } 413 if str != tc.literal { 414 t.Errorf("LiteralPrefix(`%s`) = `%s`; want `%s`", tc.pattern, str, tc.literal) 415 } 416 } 417 } 418 419 type subexpCase struct { 420 input string 421 num int 422 names []string 423 } 424 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"}}, 437 } 438 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 446 } 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 451 } 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]) 456 } 457 } 458 } 459 } 460 } 461 462 var splitTests = []struct { 463 s string 464 r string 465 n int 466 out []string 467 }{ 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", ""}}, 491 } 492 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 499 } 500 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) 504 } 505 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) 510 } 511 } 512 } 513 } 514 515 // Check that one-pass cutoff does trigger. 516 func TestOnePassCutoff(t *testing.T) { 517 re, err := syntax.Parse(`^x{1,1000}y{1,1000}$`, syntax.Perl) 518 if err != nil { 519 t.Fatalf("parse: %v", err) 520 } 521 p, err := syntax.Compile(re.Simplify()) 522 if err != nil { 523 t.Fatalf("compile: %v", err) 524 } 525 if compileOnePass(p) != notOnePass { 526 t.Fatalf("makeOnePass succeeded; wanted notOnePass") 527 } 528 } 529 530 // Check that the same machine can be used with the standard matcher 531 // and then the backtracker when there are no captures. 532 func TestSwitchBacktrack(t *testing.T) { 533 re := MustCompile(`a|b`) 534 long := make([]byte, maxBacktrackVector+1) 535 536 // The following sequence of Match calls used to panic. See issue #10319. 537 re.Match(long) // triggers standard matcher 538 re.Match(long[:1]) // triggers backtracker 539 } 540 541 func BenchmarkLiteral(b *testing.B) { 542 x := strings.Repeat("x", 50) + "y" 543 b.StopTimer() 544 re := MustCompile("y") 545 b.StartTimer() 546 for i := 0; i < b.N; i++ { 547 if !re.MatchString(x) { 548 b.Fatalf("no match!") 549 } 550 } 551 } 552 553 func BenchmarkNotLiteral(b *testing.B) { 554 x := strings.Repeat("x", 50) + "y" 555 b.StopTimer() 556 re := MustCompile(".y") 557 b.StartTimer() 558 for i := 0; i < b.N; i++ { 559 if !re.MatchString(x) { 560 b.Fatalf("no match!") 561 } 562 } 563 } 564 565 func BenchmarkMatchClass(b *testing.B) { 566 b.StopTimer() 567 x := strings.Repeat("xxxx", 20) + "w" 568 re := MustCompile("[abcdw]") 569 b.StartTimer() 570 for i := 0; i < b.N; i++ { 571 if !re.MatchString(x) { 572 b.Fatalf("no match!") 573 } 574 } 575 } 576 577 func BenchmarkMatchClass_InRange(b *testing.B) { 578 b.StopTimer() 579 // 'b' is between 'a' and 'c', so the charclass 580 // range checking is no help here. 581 x := strings.Repeat("bbbb", 20) + "c" 582 re := MustCompile("[ac]") 583 b.StartTimer() 584 for i := 0; i < b.N; i++ { 585 if !re.MatchString(x) { 586 b.Fatalf("no match!") 587 } 588 } 589 } 590 591 func BenchmarkReplaceAll(b *testing.B) { 592 x := "abcdefghijklmnopqrstuvwxyz" 593 b.StopTimer() 594 re := MustCompile("[cjrw]") 595 b.StartTimer() 596 for i := 0; i < b.N; i++ { 597 re.ReplaceAllString(x, "") 598 } 599 } 600 601 func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) { 602 b.StopTimer() 603 x := []byte("abcdefghijklmnopqrstuvwxyz") 604 re := MustCompile("^zbc(d|e)") 605 b.StartTimer() 606 for i := 0; i < b.N; i++ { 607 re.Match(x) 608 } 609 } 610 611 func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) { 612 b.StopTimer() 613 x := []byte("abcdefghijklmnopqrstuvwxyz") 614 for i := 0; i < 15; i++ { 615 x = append(x, x...) 616 } 617 re := MustCompile("^zbc(d|e)") 618 b.StartTimer() 619 for i := 0; i < b.N; i++ { 620 re.Match(x) 621 } 622 } 623 624 func BenchmarkAnchoredShortMatch(b *testing.B) { 625 b.StopTimer() 626 x := []byte("abcdefghijklmnopqrstuvwxyz") 627 re := MustCompile("^.bc(d|e)") 628 b.StartTimer() 629 for i := 0; i < b.N; i++ { 630 re.Match(x) 631 } 632 } 633 634 func BenchmarkAnchoredLongMatch(b *testing.B) { 635 b.StopTimer() 636 x := []byte("abcdefghijklmnopqrstuvwxyz") 637 for i := 0; i < 15; i++ { 638 x = append(x, x...) 639 } 640 re := MustCompile("^.bc(d|e)") 641 b.StartTimer() 642 for i := 0; i < b.N; i++ { 643 re.Match(x) 644 } 645 } 646 647 func BenchmarkOnePassShortA(b *testing.B) { 648 b.StopTimer() 649 x := []byte("abcddddddeeeededd") 650 re := MustCompile("^.bc(d|e)*$") 651 b.StartTimer() 652 for i := 0; i < b.N; i++ { 653 re.Match(x) 654 } 655 } 656 657 func BenchmarkNotOnePassShortA(b *testing.B) { 658 b.StopTimer() 659 x := []byte("abcddddddeeeededd") 660 re := MustCompile(".bc(d|e)*$") 661 b.StartTimer() 662 for i := 0; i < b.N; i++ { 663 re.Match(x) 664 } 665 } 666 667 func BenchmarkOnePassShortB(b *testing.B) { 668 b.StopTimer() 669 x := []byte("abcddddddeeeededd") 670 re := MustCompile("^.bc(?:d|e)*$") 671 b.StartTimer() 672 for i := 0; i < b.N; i++ { 673 re.Match(x) 674 } 675 } 676 677 func BenchmarkNotOnePassShortB(b *testing.B) { 678 b.StopTimer() 679 x := []byte("abcddddddeeeededd") 680 re := MustCompile(".bc(?:d|e)*$") 681 b.StartTimer() 682 for i := 0; i < b.N; i++ { 683 re.Match(x) 684 } 685 } 686 687 func BenchmarkOnePassLongPrefix(b *testing.B) { 688 b.StopTimer() 689 x := []byte("abcdefghijklmnopqrstuvwxyz") 690 re := MustCompile("^abcdefghijklmnopqrstuvwxyz.*$") 691 b.StartTimer() 692 for i := 0; i < b.N; i++ { 693 re.Match(x) 694 } 695 } 696 697 func BenchmarkOnePassLongNotPrefix(b *testing.B) { 698 b.StopTimer() 699 x := []byte("abcdefghijklmnopqrstuvwxyz") 700 re := MustCompile("^.bcdefghijklmnopqrstuvwxyz.*$") 701 b.StartTimer() 702 for i := 0; i < b.N; i++ { 703 re.Match(x) 704 } 705 } 706 707 func BenchmarkMatchParallelShared(b *testing.B) { 708 x := []byte("this is a long line that contains foo bar baz") 709 re := MustCompile("foo (ba+r)? baz") 710 b.ResetTimer() 711 b.RunParallel(func(pb *testing.PB) { 712 for pb.Next() { 713 re.Match(x) 714 } 715 }) 716 } 717 718 func BenchmarkMatchParallelCopied(b *testing.B) { 719 x := []byte("this is a long line that contains foo bar baz") 720 re := MustCompile("foo (ba+r)? baz") 721 b.ResetTimer() 722 b.RunParallel(func(pb *testing.PB) { 723 re := re.Copy() 724 for pb.Next() { 725 re.Match(x) 726 } 727 }) 728 }