| 1 |
|
| 2 |
|
| 3 |
package re2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"bufio" |
| 7 |
"compress/bzip2" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"regexp/syntax" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
"unicode/utf8" |
| 17 |
|
| 18 |
"github.com/betterleaks/go-re2/internal" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
tire |
| 55 |
ives |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func TestRE2Search(t *testing.T) { |
| 65 |
testRE2(t, "testdata/re2-search.txt") |
| 66 |
} |
| 67 |
|
| 68 |
func testRE2(t *testing.T, file string) { |
| 69 |
f, err := os.Open(file) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
defer f.Close() |
| 74 |
var txt io.Reader |
| 75 |
if strings.HasSuffix(file, ".bz2") { |
| 76 |
z := bzip2.NewReader(f) |
| 77 |
txt = z |
| 78 |
file = file[:len(file)-len(".bz2")] |
| 79 |
} else { |
| 80 |
txt = f |
| 81 |
} |
| 82 |
lineno := 0 |
| 83 |
scanner := bufio.NewScanner(txt) |
| 84 |
var ( |
| 85 |
str []string |
| 86 |
input []string |
| 87 |
inStrings bool |
| 88 |
re *Regexp |
| 89 |
reLongest *Regexp |
| 90 |
refull *Regexp |
| 91 |
refullLongest *Regexp |
| 92 |
nfail int |
| 93 |
ncase int |
| 94 |
) |
| 95 |
for lineno := 1; scanner.Scan(); lineno++ { |
| 96 |
line := scanner.Text() |
| 97 |
switch { |
| 98 |
case line == "": |
| 99 |
t.Fatalf("%s:%d: unexpected blank line", file, lineno) |
| 100 |
case line[0] == '#': |
| 101 |
continue |
| 102 |
case 'A' <= line[0] && line[0] <= 'Z': |
| 103 |
|
| 104 |
t.Logf("%s\n", line) |
| 105 |
continue |
| 106 |
case line == "strings": |
| 107 |
str = str[:0] |
| 108 |
inStrings = true |
| 109 |
case line == "regexps": |
| 110 |
inStrings = false |
| 111 |
case line[0] == '"': |
| 112 |
q, err := strconv.Unquote(line) |
| 113 |
if err != nil { |
| 114 |
|
| 115 |
t.Fatalf("%s:%d: unquote %s: %v", file, lineno, line, err) |
| 116 |
} |
| 117 |
if inStrings { |
| 118 |
str = append(str, q) |
| 119 |
continue |
| 120 |
} |
| 121 |
|
| 122 |
if len(input) != 0 { |
| 123 |
t.Fatalf("%s:%d: out of sync: have %d strings left before %#q", file, lineno, len(input), q) |
| 124 |
} |
| 125 |
re, err = tryCompile(q) |
| 126 |
if err != nil { |
| 127 |
if err.Error() == "error parsing regexp: invalid escape sequence: `\\C`" { |
| 128 |
|
| 129 |
continue |
| 130 |
} |
| 131 |
t.Errorf("%s:%d: compile %#q: %v", file, lineno, q, err) |
| 132 |
if nfail++; nfail >= 100 { |
| 133 |
t.Fatalf("stopping after %d errors", nfail) |
| 134 |
} |
| 135 |
continue |
| 136 |
} |
| 137 |
reLongest = re.Copy() |
| 138 |
reLongest.Longest() |
| 139 |
full := `\A(?:` + q + `)\z` |
| 140 |
refull, err = tryCompile(full) |
| 141 |
refullLongest = refull.Copy() |
| 142 |
refullLongest.Longest() |
| 143 |
if err != nil { |
| 144 |
|
| 145 |
t.Fatalf("%s:%d: compile full %#q: %v", file, lineno, full, err) |
| 146 |
} |
| 147 |
input = str |
| 148 |
case line[0] == '-' || '0' <= line[0] && line[0] <= '9': |
| 149 |
|
| 150 |
ncase++ |
| 151 |
if re == nil { |
| 152 |
|
| 153 |
continue |
| 154 |
} |
| 155 |
if len(input) == 0 { |
| 156 |
t.Fatalf("%s:%d: out of sync: no input remaining", file, lineno) |
| 157 |
} |
| 158 |
var text string |
| 159 |
text, input = input[0], input[1:] |
| 160 |
if !isSingleBytes(text) && strings.Contains(re.String(), `\B`) { |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
continue |
| 167 |
} |
| 168 |
res := strings.Split(line, ";") |
| 169 |
if len(res) != len(run) { |
| 170 |
t.Fatalf("%s:%d: have %d test results, want %d", file, lineno, len(res), len(run)) |
| 171 |
} |
| 172 |
for i := range res { |
| 173 |
have, suffix := run[i](re, reLongest, refull, refullLongest, text) |
| 174 |
want := parseResult(t, file, lineno, res[i]) |
| 175 |
if !same(have, want) { |
| 176 |
t.Errorf("%s:%d: %#q%s.FindSubmatchIndex(%#q) = %v, want %v", file, lineno, re, suffix, text, have, want) |
| 177 |
if nfail++; nfail >= 100 { |
| 178 |
t.Fatalf("stopping after %d errors", nfail) |
| 179 |
} |
| 180 |
continue |
| 181 |
} |
| 182 |
b, suffix := matchCases[i](re, reLongest, refull, refullLongest, text) |
| 183 |
if b != (want != nil) { |
| 184 |
t.Errorf("%s:%d: %#q%s.MatchString(%#q) = %v, want %v", file, lineno, re, suffix, text, b, !b) |
| 185 |
if nfail++; nfail >= 100 { |
| 186 |
t.Fatalf("stopping after %d errors", nfail) |
| 187 |
} |
| 188 |
continue |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
default: |
| 193 |
t.Fatalf("%s:%d: out of sync: %s\n", file, lineno, line) |
| 194 |
} |
| 195 |
} |
| 196 |
if err := scanner.Err(); err != nil { |
| 197 |
t.Fatalf("%s:%d: %v", file, lineno, err) |
| 198 |
} |
| 199 |
if len(input) != 0 { |
| 200 |
t.Fatalf("%s:%d: out of sync: have %d strings left at EOF", file, lineno, len(input)) |
| 201 |
} |
| 202 |
t.Logf("%d cases tested", ncase) |
| 203 |
} |
| 204 |
|
| 205 |
var run = []func(*Regexp, *Regexp, *Regexp, *Regexp, string) ([]int, string){ |
| 206 |
runFull, |
| 207 |
runPartial, |
| 208 |
runFullLongest, |
| 209 |
runPartialLongest, |
| 210 |
} |
| 211 |
|
| 212 |
func runFull(re, reLongest, refull, refullLongest *Regexp, text string) ([]int, string) { |
| 213 |
return refull.FindStringSubmatchIndex(text), "[full]" |
| 214 |
} |
| 215 |
|
| 216 |
func runPartial(re, reLongest, refull, refullLongest *Regexp, text string) ([]int, string) { |
| 217 |
return re.FindStringSubmatchIndex(text), "" |
| 218 |
} |
| 219 |
|
| 220 |
func runFullLongest(re, reLongest, refull, refullLongest *Regexp, text string) ([]int, string) { |
| 221 |
return refullLongest.FindStringSubmatchIndex(text), "[full,longest]" |
| 222 |
} |
| 223 |
|
| 224 |
func runPartialLongest(re, reLongest, refull, refullLongest *Regexp, text string) ([]int, string) { |
| 225 |
return reLongest.FindStringSubmatchIndex(text), "[longest]" |
| 226 |
} |
| 227 |
|
| 228 |
var matchCases = []func(*Regexp, *Regexp, *Regexp, *Regexp, string) (bool, string){ |
| 229 |
matchFull, |
| 230 |
matchPartial, |
| 231 |
matchFullLongest, |
| 232 |
matchPartialLongest, |
| 233 |
} |
| 234 |
|
| 235 |
func matchFull(re, reLongest, refull, refullLongest *Regexp, text string) (bool, string) { |
| 236 |
return refull.MatchString(text), "[full]" |
| 237 |
} |
| 238 |
|
| 239 |
func matchPartial(re, reLongest, refull, refullLongest *Regexp, text string) (bool, string) { |
| 240 |
return re.MatchString(text), "" |
| 241 |
} |
| 242 |
|
| 243 |
func matchFullLongest(re, reLongest, refull, refullLongest *Regexp, text string) (bool, string) { |
| 244 |
return refullLongest.MatchString(text), "[full,longest]" |
| 245 |
} |
| 246 |
|
| 247 |
func matchPartialLongest(re, reLongest, refull, refullLongest *Regexp, text string) (bool, string) { |
| 248 |
return reLongest.MatchString(text), "[longest]" |
| 249 |
} |
| 250 |
|
| 251 |
func isSingleBytes(s string) bool { |
| 252 |
for _, c := range s { |
| 253 |
if c >= utf8.RuneSelf { |
| 254 |
return false |
| 255 |
} |
| 256 |
} |
| 257 |
return true |
| 258 |
} |
| 259 |
|
| 260 |
func tryCompile(s string) (re *Regexp, err error) { |
| 261 |
|
| 262 |
defer func() { |
| 263 |
if r := recover(); r != nil { |
| 264 |
err = fmt.Errorf("panic: %v", r) |
| 265 |
} |
| 266 |
}() |
| 267 |
return Compile(s) |
| 268 |
} |
| 269 |
|
| 270 |
func parseResult(t *testing.T, file string, lineno int, res string) []int { |
| 271 |
|
| 272 |
if res == "-" { |
| 273 |
return nil |
| 274 |
} |
| 275 |
|
| 276 |
n := 1 |
| 277 |
for j := 0; j < len(res); j++ { |
| 278 |
if res[j] == ' ' { |
| 279 |
n++ |
| 280 |
} |
| 281 |
} |
| 282 |
out := make([]int, 2*n) |
| 283 |
i := 0 |
| 284 |
n = 0 |
| 285 |
for j := 0; j <= len(res); j++ { |
| 286 |
if j == len(res) || res[j] == ' ' { |
| 287 |
|
| 288 |
pair := res[i:j] |
| 289 |
if pair == "-" { |
| 290 |
out[n] = -1 |
| 291 |
out[n+1] = -1 |
| 292 |
} else { |
| 293 |
loStr, hiStr, _ := strings.Cut(pair, "-") |
| 294 |
lo, err1 := strconv.Atoi(loStr) |
| 295 |
hi, err2 := strconv.Atoi(hiStr) |
| 296 |
if err1 != nil || err2 != nil || lo > hi { |
| 297 |
t.Fatalf("%s:%d: invalid pair %s", file, lineno, pair) |
| 298 |
} |
| 299 |
out[n] = lo |
| 300 |
out[n+1] = hi |
| 301 |
} |
| 302 |
n += 2 |
| 303 |
i = j + 1 |
| 304 |
} |
| 305 |
} |
| 306 |
return out |
| 307 |
} |
| 308 |
|
| 309 |
func same(x, y []int) bool { |
| 310 |
if len(x) != len(y) { |
| 311 |
return false |
| 312 |
} |
| 313 |
for i, xi := range x { |
| 314 |
if xi != y[i] { |
| 315 |
return false |
| 316 |
} |
| 317 |
} |
| 318 |
return true |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
func TestFowler(t *testing.T) { |
| 325 |
files, err := filepath.Glob("testdata/*.dat") |
| 326 |
if err != nil { |
| 327 |
t.Fatal(err) |
| 328 |
} |
| 329 |
for _, file := range files { |
| 330 |
t.Log(file) |
| 331 |
testFowler(t, file) |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
var notab *Regexp |
| 336 |
|
| 337 |
func init() { |
| 338 |
notab = MustCompilePOSIX(`[^\t]+`) |
| 339 |
} |
| 340 |
|
| 341 |
func testFowler(t *testing.T, file string) { |
| 342 |
f, err := os.Open(file) |
| 343 |
if err != nil { |
| 344 |
t.Error(err) |
| 345 |
return |
| 346 |
} |
| 347 |
defer f.Close() |
| 348 |
b := bufio.NewReader(f) |
| 349 |
lineno := 0 |
| 350 |
lastRegexp := "" |
| 351 |
Reading: |
| 352 |
for { |
| 353 |
lineno++ |
| 354 |
line, err := b.ReadString('\n') |
| 355 |
if err != nil { |
| 356 |
if err != io.EOF { |
| 357 |
t.Errorf("%s:%d: %v", file, lineno, err) |
| 358 |
} |
| 359 |
break Reading |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if line[0] == '#' || line[0] == '\n' { |
| 370 |
continue Reading |
| 371 |
} |
| 372 |
line = line[:len(line)-1] |
| 373 |
field := notab.FindAllString(line, -1) |
| 374 |
for i, f := range field { |
| 375 |
if f == "NULL" { |
| 376 |
field[i] = "" |
| 377 |
} |
| 378 |
if f == "NIL" { |
| 379 |
t.Logf("%s:%d: skip: %s", file, lineno, line) |
| 380 |
continue Reading |
| 381 |
} |
| 382 |
} |
| 383 |
if len(field) == 0 { |
| 384 |
continue Reading |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
flag := field[0] |
| 448 |
switch flag[0] { |
| 449 |
case '?', '&', '|', ';', '{', '}': |
| 450 |
|
| 451 |
|
| 452 |
flag = flag[1:] |
| 453 |
if flag == "" { |
| 454 |
continue Reading |
| 455 |
} |
| 456 |
case ':': |
| 457 |
var ok bool |
| 458 |
if _, flag, ok = strings.Cut(flag[1:], ":"); !ok { |
| 459 |
t.Logf("skip: %s", line) |
| 460 |
continue Reading |
| 461 |
} |
| 462 |
case 'C', 'N', 'T', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 463 |
t.Logf("skip: %s", line) |
| 464 |
continue Reading |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
if strings.Contains(line, `a{9876543210}`) { |
| 470 |
t.Logf("skip: %s", line) |
| 471 |
continue Reading |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
if strings.Contains(line, `\x01\xff`) { |
| 476 |
t.Logf("skip: %s", line) |
| 477 |
continue Reading |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
if len(field) < 4 { |
| 482 |
t.Errorf("%s:%d: too few fields: %s", file, lineno, line) |
| 483 |
continue Reading |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
if strings.Contains(flag, "$") { |
| 488 |
f := `"` + field[1] + `"` |
| 489 |
if field[1], err = strconv.Unquote(f); err != nil { |
| 490 |
t.Errorf("%s:%d: cannot unquote %s", file, lineno, f) |
| 491 |
} |
| 492 |
f = `"` + field[2] + `"` |
| 493 |
if field[2], err = strconv.Unquote(f); err != nil { |
| 494 |
t.Errorf("%s:%d: cannot unquote %s", file, lineno, f) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
if field[1] == "SAME" { |
| 502 |
field[1] = lastRegexp |
| 503 |
} |
| 504 |
lastRegexp = field[1] |
| 505 |
|
| 506 |
|
| 507 |
text := field[2] |
| 508 |
|
| 509 |
|
| 510 |
ok, shouldCompile, shouldMatch, pos := parseFowlerResult(field[3]) |
| 511 |
if !ok { |
| 512 |
t.Errorf("%s:%d: cannot parse result %#q", file, lineno, field[3]) |
| 513 |
continue Reading |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
Testing: |
| 519 |
|
| 520 |
for _, c := range flag { |
| 521 |
pattern := field[1] |
| 522 |
syn := syntax.POSIX | syntax.ClassNL |
| 523 |
switch c { |
| 524 |
default: |
| 525 |
continue Testing |
| 526 |
case 'E': |
| 527 |
|
| 528 |
case 'L': |
| 529 |
|
| 530 |
pattern = QuoteMeta(pattern) |
| 531 |
} |
| 532 |
|
| 533 |
caseInsensitive := false |
| 534 |
for _, c := range flag { |
| 535 |
switch c { |
| 536 |
case 'i': |
| 537 |
syn |= syntax.FoldCase |
| 538 |
caseInsensitive = true |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
re, err := internal.Compile(pattern, internal.CompileOptions{Longest: true, Posix: true, CaseInsensitive: caseInsensitive}) |
| 543 |
if err != nil { |
| 544 |
if shouldCompile { |
| 545 |
t.Errorf("%s:%d: %#q did not compile", file, lineno, pattern) |
| 546 |
} |
| 547 |
continue Testing |
| 548 |
} |
| 549 |
if !shouldCompile { |
| 550 |
t.Errorf("%s:%d: %#q should not compile", file, lineno, pattern) |
| 551 |
continue Testing |
| 552 |
} |
| 553 |
match := re.MatchString(text) |
| 554 |
if match != shouldMatch { |
| 555 |
t.Errorf("%s:%d: %#q.Match(%#q) = %v, want %v", file, lineno, pattern, text, match, shouldMatch) |
| 556 |
continue Testing |
| 557 |
} |
| 558 |
have := re.FindStringSubmatchIndex(text) |
| 559 |
if (len(have) > 0) != match { |
| 560 |
t.Errorf("%s:%d: %#q.Match(%#q) = %v, but %#q.FindSubmatchIndex(%#q) = %v", file, lineno, pattern, text, match, pattern, text, have) |
| 561 |
continue Testing |
| 562 |
} |
| 563 |
if len(have) > len(pos) { |
| 564 |
have = have[:len(pos)] |
| 565 |
} |
| 566 |
if !same(have, pos) { |
| 567 |
t.Errorf("%s:%d: %#q.FindSubmatchIndex(%#q) = %v, want %v", file, lineno, pattern, text, have, pos) |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
func parseFowlerResult(s string) (ok, compiled, matched bool, pos []int) { |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
switch { |
| 589 |
case s == "": |
| 590 |
|
| 591 |
ok = true |
| 592 |
compiled = true |
| 593 |
matched = true |
| 594 |
return |
| 595 |
case s == "NOMATCH": |
| 596 |
|
| 597 |
ok = true |
| 598 |
compiled = true |
| 599 |
matched = false |
| 600 |
return |
| 601 |
case 'A' <= s[0] && s[0] <= 'Z': |
| 602 |
|
| 603 |
ok = true |
| 604 |
compiled = false |
| 605 |
return |
| 606 |
} |
| 607 |
compiled = true |
| 608 |
|
| 609 |
var x []int |
| 610 |
for s != "" { |
| 611 |
var end byte = ')' |
| 612 |
if len(x)%2 == 0 { |
| 613 |
if s[0] != '(' { |
| 614 |
ok = false |
| 615 |
return |
| 616 |
} |
| 617 |
s = s[1:] |
| 618 |
end = ',' |
| 619 |
} |
| 620 |
i := 0 |
| 621 |
for i < len(s) && s[i] != end { |
| 622 |
i++ |
| 623 |
} |
| 624 |
if i == 0 || i == len(s) { |
| 625 |
ok = false |
| 626 |
return |
| 627 |
} |
| 628 |
v := -1 |
| 629 |
var err error |
| 630 |
if s[:i] != "?" { |
| 631 |
v, err = strconv.Atoi(s[:i]) |
| 632 |
if err != nil { |
| 633 |
ok = false |
| 634 |
return |
| 635 |
} |
| 636 |
} |
| 637 |
x = append(x, v) |
| 638 |
s = s[i+1:] |
| 639 |
} |
| 640 |
if len(x)%2 != 0 { |
| 641 |
ok = false |
| 642 |
return |
| 643 |
} |
| 644 |
ok = true |
| 645 |
matched = true |
| 646 |
pos = x |
| 647 |
return |
| 648 |
} |