| 1 |
// Copyright (c) 2005, Google Inc. |
// -*- coding: utf-8 -*- |
| 2 |
|
// |
| 3 |
|
// Copyright (c) 2005 - 2006, Google Inc. |
| 4 |
// All rights reserved. |
// All rights reserved. |
| 5 |
// |
// |
| 6 |
// Redistribution and use in source and binary forms, with or without |
// Redistribution and use in source and binary forms, with or without |
| 33 |
// |
// |
| 34 |
// TODO: Test extractions for PartialMatch/Consume |
// TODO: Test extractions for PartialMatch/Consume |
| 35 |
|
|
| 36 |
|
#ifdef HAVE_CONFIG_H |
| 37 |
|
#include "config.h" |
| 38 |
|
#endif |
| 39 |
|
|
| 40 |
|
#ifdef _WIN32 |
| 41 |
|
#define snprintf _snprintf |
| 42 |
|
#endif |
| 43 |
|
|
| 44 |
#include <stdio.h> |
#include <stdio.h> |
| 45 |
|
#include <cassert> |
| 46 |
#include <vector> |
#include <vector> |
|
#include "config.h" |
|
| 47 |
#include "pcrecpp.h" |
#include "pcrecpp.h" |
| 48 |
|
|
| 49 |
using pcrecpp::StringPiece; |
using pcrecpp::StringPiece; |
| 53 |
using pcrecpp::Octal; |
using pcrecpp::Octal; |
| 54 |
using pcrecpp::CRadix; |
using pcrecpp::CRadix; |
| 55 |
|
|
| 56 |
|
static bool VERBOSE_TEST = false; |
| 57 |
|
|
| 58 |
// CHECK dies with a fatal error if condition is not true. It is *not* |
// CHECK dies with a fatal error if condition is not true. It is *not* |
| 59 |
// controlled by NDEBUG, so the check will be executed regardless of |
// controlled by NDEBUG, so the check will be executed regardless of |
| 60 |
// compilation mode. Therefore, it is safe to do things like: |
// compilation mode. Therefore, it is safe to do things like: |
| 269 |
"aaaaa", |
"aaaaa", |
| 270 |
"bbaaaaa", |
"bbaaaaa", |
| 271 |
"bbabbabbabbabbabb" }, |
"bbabbabbabbabbabb" }, |
| 272 |
|
{ "b*", |
| 273 |
|
"bb", |
| 274 |
|
"aa\naa\n", |
| 275 |
|
"bbaa\naa\n", |
| 276 |
|
"bbabbabb\nbbabbabb\nbb" }, |
| 277 |
|
{ "b*", |
| 278 |
|
"bb", |
| 279 |
|
"aa\raa\r", |
| 280 |
|
"bbaa\raa\r", |
| 281 |
|
"bbabbabb\rbbabbabb\rbb" }, |
| 282 |
|
{ "b*", |
| 283 |
|
"bb", |
| 284 |
|
"aa\r\naa\r\n", |
| 285 |
|
"bbaa\r\naa\r\n", |
| 286 |
|
"bbabbabb\r\nbbabbabb\r\nbb" }, |
| 287 |
|
#ifdef SUPPORT_UTF8 |
| 288 |
|
{ "b*", |
| 289 |
|
"bb", |
| 290 |
|
"\xE3\x83\x9B\xE3\x83\xBC\xE3\x83\xA0\xE3\x81\xB8", // utf8 |
| 291 |
|
"bb\xE3\x83\x9B\xE3\x83\xBC\xE3\x83\xA0\xE3\x81\xB8", |
| 292 |
|
"bb\xE3\x83\x9B""bb""\xE3\x83\xBC""bb""\xE3\x83\xA0""bb""\xE3\x81\xB8""bb" }, |
| 293 |
|
{ "b*", |
| 294 |
|
"bb", |
| 295 |
|
"\xE3\x83\x9B\r\n\xE3\x83\xBC\r\xE3\x83\xA0\n\xE3\x81\xB8\r\n", // utf8 |
| 296 |
|
"bb\xE3\x83\x9B\r\n\xE3\x83\xBC\r\xE3\x83\xA0\n\xE3\x81\xB8\r\n", |
| 297 |
|
("bb\xE3\x83\x9B""bb\r\nbb""\xE3\x83\xBC""bb\rbb""\xE3\x83\xA0" |
| 298 |
|
"bb\nbb""\xE3\x81\xB8""bb\r\nbb") }, |
| 299 |
|
#endif |
| 300 |
{ "", NULL, NULL, NULL, NULL } |
{ "", NULL, NULL, NULL, NULL } |
| 301 |
}; |
}; |
| 302 |
|
|
| 303 |
|
#ifdef SUPPORT_UTF8 |
| 304 |
|
const bool support_utf8 = true; |
| 305 |
|
#else |
| 306 |
|
const bool support_utf8 = false; |
| 307 |
|
#endif |
| 308 |
|
|
| 309 |
for (const ReplaceTest *t = tests; t->original != NULL; ++t) { |
for (const ReplaceTest *t = tests; t->original != NULL; ++t) { |
| 310 |
|
RE re(t->regexp, RE_Options(PCRE_NEWLINE_CRLF).set_utf8(support_utf8)); |
| 311 |
|
assert(re.error().empty()); |
| 312 |
string one(t->original); |
string one(t->original); |
| 313 |
CHECK(RE(t->regexp).Replace(t->rewrite, &one)); |
CHECK(re.Replace(t->rewrite, &one)); |
| 314 |
CHECK_EQ(one, t->single); |
CHECK_EQ(one, t->single); |
| 315 |
string all(t->original); |
string all(t->original); |
| 316 |
CHECK(RE(t->regexp).GlobalReplace(t->rewrite, &all) > 0); |
CHECK(re.GlobalReplace(t->rewrite, &all) > 0); |
| 317 |
CHECK_EQ(all, t->global); |
CHECK_EQ(all, t->global); |
| 318 |
} |
} |
| 319 |
|
|
| 320 |
|
// One final test: test \r\n replacement when we're not in CRLF mode |
| 321 |
|
{ |
| 322 |
|
RE re("b*", RE_Options(PCRE_NEWLINE_CR).set_utf8(support_utf8)); |
| 323 |
|
assert(re.error().empty()); |
| 324 |
|
string all("aa\r\naa\r\n"); |
| 325 |
|
CHECK(re.GlobalReplace("bb", &all) > 0); |
| 326 |
|
CHECK_EQ(all, string("bbabbabb\rbb\nbbabbabb\rbb\nbb")); |
| 327 |
|
} |
| 328 |
|
{ |
| 329 |
|
RE re("b*", RE_Options(PCRE_NEWLINE_LF).set_utf8(support_utf8)); |
| 330 |
|
assert(re.error().empty()); |
| 331 |
|
string all("aa\r\naa\r\n"); |
| 332 |
|
CHECK(re.GlobalReplace("bb", &all) > 0); |
| 333 |
|
CHECK_EQ(all, string("bbabbabb\rbb\nbbabbabb\rbb\nbb")); |
| 334 |
|
} |
| 335 |
|
// TODO: test what happens when no PCRE_NEWLINE_* flag is set. |
| 336 |
|
// Alas, the answer depends on how pcre was compiled. |
| 337 |
} |
} |
| 338 |
|
|
| 339 |
static void TestExtract() { |
static void TestExtract() { |
| 412 |
CHECK_EQ(a, ""); |
CHECK_EQ(a, ""); |
| 413 |
} |
} |
| 414 |
|
|
| 415 |
static void TestRecursion(int size, const char *pattern, int match_limit) { |
static void TestRecursion() { |
| 416 |
printf("Testing recursion\n"); |
printf("Testing recursion\n"); |
| 417 |
|
|
| 418 |
// Fill up a string repeating the pattern given |
// Get one string that passes (sometimes), one that never does. |
| 419 |
string domain; |
string text_good("abcdefghijk"); |
| 420 |
domain.resize(size); |
string text_bad("acdefghijkl"); |
| 421 |
int patlen = strlen(pattern); |
|
| 422 |
for (int i = 0; i < size; ++i) { |
// According to pcretest, matching text_good against (\w+)*b |
| 423 |
domain[i] = pattern[i % patlen]; |
// requires match_limit of at least 8192, and match_recursion_limit |
| 424 |
|
// of at least 37. |
| 425 |
|
|
| 426 |
|
RE_Options options_ml; |
| 427 |
|
options_ml.set_match_limit(8192); |
| 428 |
|
RE re("(\\w+)*b", options_ml); |
| 429 |
|
CHECK(re.PartialMatch(text_good) == true); |
| 430 |
|
CHECK(re.PartialMatch(text_bad) == false); |
| 431 |
|
CHECK(re.FullMatch(text_good) == false); |
| 432 |
|
CHECK(re.FullMatch(text_bad) == false); |
| 433 |
|
|
| 434 |
|
options_ml.set_match_limit(1024); |
| 435 |
|
RE re2("(\\w+)*b", options_ml); |
| 436 |
|
CHECK(re2.PartialMatch(text_good) == false); // because of match_limit |
| 437 |
|
CHECK(re2.PartialMatch(text_bad) == false); |
| 438 |
|
CHECK(re2.FullMatch(text_good) == false); |
| 439 |
|
CHECK(re2.FullMatch(text_bad) == false); |
| 440 |
|
|
| 441 |
|
RE_Options options_mlr; |
| 442 |
|
options_mlr.set_match_limit_recursion(50); |
| 443 |
|
RE re3("(\\w+)*b", options_mlr); |
| 444 |
|
CHECK(re3.PartialMatch(text_good) == true); |
| 445 |
|
CHECK(re3.PartialMatch(text_bad) == false); |
| 446 |
|
CHECK(re3.FullMatch(text_good) == false); |
| 447 |
|
CHECK(re3.FullMatch(text_bad) == false); |
| 448 |
|
|
| 449 |
|
options_mlr.set_match_limit_recursion(10); |
| 450 |
|
RE re4("(\\w+)*b", options_mlr); |
| 451 |
|
CHECK(re4.PartialMatch(text_good) == false); |
| 452 |
|
CHECK(re4.PartialMatch(text_bad) == false); |
| 453 |
|
CHECK(re4.FullMatch(text_good) == false); |
| 454 |
|
CHECK(re4.FullMatch(text_bad) == false); |
| 455 |
|
} |
| 456 |
|
|
| 457 |
|
// A meta-quoted string, interpreted as a pattern, should always match |
| 458 |
|
// the original unquoted string. |
| 459 |
|
static void TestQuoteMeta(string unquoted, RE_Options options = RE_Options()) { |
| 460 |
|
string quoted = RE::QuoteMeta(unquoted); |
| 461 |
|
RE re(quoted, options); |
| 462 |
|
CHECK(re.FullMatch(unquoted)); |
| 463 |
|
} |
| 464 |
|
|
| 465 |
|
// A string containing meaningful regexp characters, which is then meta- |
| 466 |
|
// quoted, should not generally match a string the unquoted string does. |
| 467 |
|
static void NegativeTestQuoteMeta(string unquoted, string should_not_match, |
| 468 |
|
RE_Options options = RE_Options()) { |
| 469 |
|
string quoted = RE::QuoteMeta(unquoted); |
| 470 |
|
RE re(quoted, options); |
| 471 |
|
CHECK(!re.FullMatch(should_not_match)); |
| 472 |
|
} |
| 473 |
|
|
| 474 |
|
// Tests that quoted meta characters match their original strings, |
| 475 |
|
// and that a few things that shouldn't match indeed do not. |
| 476 |
|
static void TestQuotaMetaSimple() { |
| 477 |
|
TestQuoteMeta("foo"); |
| 478 |
|
TestQuoteMeta("foo.bar"); |
| 479 |
|
TestQuoteMeta("foo\\.bar"); |
| 480 |
|
TestQuoteMeta("[1-9]"); |
| 481 |
|
TestQuoteMeta("1.5-2.0?"); |
| 482 |
|
TestQuoteMeta("\\d"); |
| 483 |
|
TestQuoteMeta("Who doesn't like ice cream?"); |
| 484 |
|
TestQuoteMeta("((a|b)c?d*e+[f-h]i)"); |
| 485 |
|
TestQuoteMeta("((?!)xxx).*yyy"); |
| 486 |
|
TestQuoteMeta("(["); |
| 487 |
|
} |
| 488 |
|
|
| 489 |
|
static void TestQuoteMetaSimpleNegative() { |
| 490 |
|
NegativeTestQuoteMeta("foo", "bar"); |
| 491 |
|
NegativeTestQuoteMeta("...", "bar"); |
| 492 |
|
NegativeTestQuoteMeta("\\.", "."); |
| 493 |
|
NegativeTestQuoteMeta("\\.", ".."); |
| 494 |
|
NegativeTestQuoteMeta("(a)", "a"); |
| 495 |
|
NegativeTestQuoteMeta("(a|b)", "a"); |
| 496 |
|
NegativeTestQuoteMeta("(a|b)", "(a)"); |
| 497 |
|
NegativeTestQuoteMeta("(a|b)", "a|b"); |
| 498 |
|
NegativeTestQuoteMeta("[0-9]", "0"); |
| 499 |
|
NegativeTestQuoteMeta("[0-9]", "0-9"); |
| 500 |
|
NegativeTestQuoteMeta("[0-9]", "[9]"); |
| 501 |
|
NegativeTestQuoteMeta("((?!)xxx)", "xxx"); |
| 502 |
|
} |
| 503 |
|
|
| 504 |
|
static void TestQuoteMetaLatin1() { |
| 505 |
|
TestQuoteMeta("3\xb2 = 9"); |
| 506 |
|
} |
| 507 |
|
|
| 508 |
|
static void TestQuoteMetaUtf8() { |
| 509 |
|
#ifdef SUPPORT_UTF8 |
| 510 |
|
TestQuoteMeta("Pl\xc3\xa1\x63ido Domingo", pcrecpp::UTF8()); |
| 511 |
|
TestQuoteMeta("xyz", pcrecpp::UTF8()); // No fancy utf8 |
| 512 |
|
TestQuoteMeta("\xc2\xb0", pcrecpp::UTF8()); // 2-byte utf8 (degree symbol) |
| 513 |
|
TestQuoteMeta("27\xc2\xb0 degrees", pcrecpp::UTF8()); // As a middle character |
| 514 |
|
TestQuoteMeta("\xe2\x80\xb3", pcrecpp::UTF8()); // 3-byte utf8 (double prime) |
| 515 |
|
TestQuoteMeta("\xf0\x9d\x85\x9f", pcrecpp::UTF8()); // 4-byte utf8 (music note) |
| 516 |
|
TestQuoteMeta("27\xc2\xb0"); // Interpreted as Latin-1, but should still work |
| 517 |
|
NegativeTestQuoteMeta("27\xc2\xb0", // 2-byte utf (degree symbol) |
| 518 |
|
"27\\\xc2\\\xb0", |
| 519 |
|
pcrecpp::UTF8()); |
| 520 |
|
#endif |
| 521 |
|
} |
| 522 |
|
|
| 523 |
|
static void TestQuoteMetaAll() { |
| 524 |
|
printf("Testing QuoteMeta\n"); |
| 525 |
|
TestQuotaMetaSimple(); |
| 526 |
|
TestQuoteMetaSimpleNegative(); |
| 527 |
|
TestQuoteMetaLatin1(); |
| 528 |
|
TestQuoteMetaUtf8(); |
| 529 |
|
} |
| 530 |
|
|
| 531 |
|
// |
| 532 |
|
// Options tests contributed by |
| 533 |
|
// Giuseppe Maxia, CTO, Stardata s.r.l. |
| 534 |
|
// July 2005 |
| 535 |
|
// |
| 536 |
|
static void GetOneOptionResult( |
| 537 |
|
const char *option_name, |
| 538 |
|
const char *regex, |
| 539 |
|
const char *str, |
| 540 |
|
RE_Options options, |
| 541 |
|
bool full, |
| 542 |
|
string expected) { |
| 543 |
|
|
| 544 |
|
printf("Testing Option <%s>\n", option_name); |
| 545 |
|
if(VERBOSE_TEST) |
| 546 |
|
printf("/%s/ finds \"%s\" within \"%s\" \n", |
| 547 |
|
regex, |
| 548 |
|
expected.c_str(), |
| 549 |
|
str); |
| 550 |
|
string captured(""); |
| 551 |
|
if (full) |
| 552 |
|
RE(regex,options).FullMatch(str, &captured); |
| 553 |
|
else |
| 554 |
|
RE(regex,options).PartialMatch(str, &captured); |
| 555 |
|
CHECK_EQ(captured, expected); |
| 556 |
|
} |
| 557 |
|
|
| 558 |
|
static void TestOneOption( |
| 559 |
|
const char *option_name, |
| 560 |
|
const char *regex, |
| 561 |
|
const char *str, |
| 562 |
|
RE_Options options, |
| 563 |
|
bool full, |
| 564 |
|
bool assertive = true) { |
| 565 |
|
|
| 566 |
|
printf("Testing Option <%s>\n", option_name); |
| 567 |
|
if (VERBOSE_TEST) |
| 568 |
|
printf("'%s' %s /%s/ \n", |
| 569 |
|
str, |
| 570 |
|
(assertive? "matches" : "doesn't match"), |
| 571 |
|
regex); |
| 572 |
|
if (assertive) { |
| 573 |
|
if (full) |
| 574 |
|
CHECK(RE(regex,options).FullMatch(str)); |
| 575 |
|
else |
| 576 |
|
CHECK(RE(regex,options).PartialMatch(str)); |
| 577 |
|
} else { |
| 578 |
|
if (full) |
| 579 |
|
CHECK(!RE(regex,options).FullMatch(str)); |
| 580 |
|
else |
| 581 |
|
CHECK(!RE(regex,options).PartialMatch(str)); |
| 582 |
} |
} |
| 583 |
// Just make sure it doesn't crash due to too much recursion. |
} |
| 584 |
|
|
| 585 |
|
static void Test_CASELESS() { |
| 586 |
|
RE_Options options; |
| 587 |
|
RE_Options options2; |
| 588 |
|
|
| 589 |
|
options.set_caseless(true); |
| 590 |
|
TestOneOption("CASELESS (class)", "HELLO", "hello", options, false); |
| 591 |
|
TestOneOption("CASELESS (class2)", "HELLO", "hello", options2.set_caseless(true), false); |
| 592 |
|
TestOneOption("CASELESS (class)", "^[A-Z]+$", "Hello", options, false); |
| 593 |
|
|
| 594 |
|
TestOneOption("CASELESS (function)", "HELLO", "hello", pcrecpp::CASELESS(), false); |
| 595 |
|
TestOneOption("CASELESS (function)", "^[A-Z]+$", "Hello", pcrecpp::CASELESS(), false); |
| 596 |
|
options.set_caseless(false); |
| 597 |
|
TestOneOption("no CASELESS", "HELLO", "hello", options, false, false); |
| 598 |
|
} |
| 599 |
|
|
| 600 |
|
static void Test_MULTILINE() { |
| 601 |
|
RE_Options options; |
| 602 |
|
RE_Options options2; |
| 603 |
|
const char *str = "HELLO\n" "cruel\n" "world\n"; |
| 604 |
|
|
| 605 |
|
options.set_multiline(true); |
| 606 |
|
TestOneOption("MULTILINE (class)", "^cruel$", str, options, false); |
| 607 |
|
TestOneOption("MULTILINE (class2)", "^cruel$", str, options2.set_multiline(true), false); |
| 608 |
|
TestOneOption("MULTILINE (function)", "^cruel$", str, pcrecpp::MULTILINE(), false); |
| 609 |
|
options.set_multiline(false); |
| 610 |
|
TestOneOption("no MULTILINE", "^cruel$", str, options, false, false); |
| 611 |
|
} |
| 612 |
|
|
| 613 |
|
static void Test_DOTALL() { |
| 614 |
|
RE_Options options; |
| 615 |
|
RE_Options options2; |
| 616 |
|
const char *str = "HELLO\n" "cruel\n" "world"; |
| 617 |
|
|
| 618 |
|
options.set_dotall(true); |
| 619 |
|
TestOneOption("DOTALL (class)", "HELLO.*world", str, options, true); |
| 620 |
|
TestOneOption("DOTALL (class2)", "HELLO.*world", str, options2.set_dotall(true), true); |
| 621 |
|
TestOneOption("DOTALL (function)", "HELLO.*world", str, pcrecpp::DOTALL(), true); |
| 622 |
|
options.set_dotall(false); |
| 623 |
|
TestOneOption("no DOTALL", "HELLO.*world", str, options, true, false); |
| 624 |
|
} |
| 625 |
|
|
| 626 |
|
static void Test_DOLLAR_ENDONLY() { |
| 627 |
|
RE_Options options; |
| 628 |
|
RE_Options options2; |
| 629 |
|
const char *str = "HELLO world\n"; |
| 630 |
|
|
| 631 |
|
TestOneOption("no DOLLAR_ENDONLY", "world$", str, options, false); |
| 632 |
|
options.set_dollar_endonly(true); |
| 633 |
|
TestOneOption("DOLLAR_ENDONLY 1", "world$", str, options, false, false); |
| 634 |
|
TestOneOption("DOLLAR_ENDONLY 2", "world$", str, options2.set_dollar_endonly(true), false, false); |
| 635 |
|
} |
| 636 |
|
|
| 637 |
|
static void Test_EXTRA() { |
| 638 |
|
RE_Options options; |
| 639 |
|
const char *str = "HELLO"; |
| 640 |
|
|
| 641 |
|
options.set_extra(true); |
| 642 |
|
TestOneOption("EXTRA 1", "\\HELL\\O", str, options, true, false ); |
| 643 |
|
TestOneOption("EXTRA 2", "\\HELL\\O", str, RE_Options().set_extra(true), true, false ); |
| 644 |
|
options.set_extra(false); |
| 645 |
|
TestOneOption("no EXTRA", "\\HELL\\O", str, options, true ); |
| 646 |
|
} |
| 647 |
|
|
| 648 |
|
static void Test_EXTENDED() { |
| 649 |
|
RE_Options options; |
| 650 |
|
RE_Options options2; |
| 651 |
|
const char *str = "HELLO world"; |
| 652 |
|
|
| 653 |
|
options.set_extended(true); |
| 654 |
|
TestOneOption("EXTENDED (class)", "HELLO world", str, options, false, false); |
| 655 |
|
TestOneOption("EXTENDED (class2)", "HELLO world", str, options2.set_extended(true), false, false); |
| 656 |
|
TestOneOption("EXTENDED (class)", |
| 657 |
|
"^ HE L{2} O " |
| 658 |
|
"\\s+ " |
| 659 |
|
"\\w+ $ ", |
| 660 |
|
str, |
| 661 |
|
options, |
| 662 |
|
false); |
| 663 |
|
|
| 664 |
|
TestOneOption("EXTENDED (function)", "HELLO world", str, pcrecpp::EXTENDED(), false, false); |
| 665 |
|
TestOneOption("EXTENDED (function)", |
| 666 |
|
"^ HE L{2} O " |
| 667 |
|
"\\s+ " |
| 668 |
|
"\\w+ $ ", |
| 669 |
|
str, |
| 670 |
|
pcrecpp::EXTENDED(), |
| 671 |
|
false); |
| 672 |
|
|
| 673 |
|
options.set_extended(false); |
| 674 |
|
TestOneOption("no EXTENDED", "HELLO world", str, options, false); |
| 675 |
|
} |
| 676 |
|
|
| 677 |
|
static void Test_NO_AUTO_CAPTURE() { |
| 678 |
RE_Options options; |
RE_Options options; |
| 679 |
options.set_match_limit(match_limit); |
const char *str = "HELLO world"; |
| 680 |
RE re("([a-zA-Z0-9]|-)+(\\.([a-zA-Z0-9]|-)+)*(\\.)?", options); |
string captured; |
| 681 |
re.FullMatch(domain); |
|
| 682 |
|
printf("Testing Option <no NO_AUTO_CAPTURE>\n"); |
| 683 |
|
if (VERBOSE_TEST) |
| 684 |
|
printf("parentheses capture text\n"); |
| 685 |
|
RE re("(world|universe)$", options); |
| 686 |
|
CHECK(re.Extract("\\1", str , &captured)); |
| 687 |
|
CHECK_EQ(captured, "world"); |
| 688 |
|
options.set_no_auto_capture(true); |
| 689 |
|
printf("testing Option <NO_AUTO_CAPTURE>\n"); |
| 690 |
|
if (VERBOSE_TEST) |
| 691 |
|
printf("parentheses do not capture text\n"); |
| 692 |
|
re.Extract("\\1",str, &captured ); |
| 693 |
|
CHECK_EQ(captured, "world"); |
| 694 |
} |
} |
| 695 |
|
|
| 696 |
|
static void Test_UNGREEDY() { |
| 697 |
|
RE_Options options; |
| 698 |
|
const char *str = "HELLO, 'this' is the 'world'"; |
| 699 |
|
|
| 700 |
|
options.set_ungreedy(true); |
| 701 |
|
GetOneOptionResult("UNGREEDY 1", "('.*')", str, options, false, "'this'" ); |
| 702 |
|
GetOneOptionResult("UNGREEDY 2", "('.*')", str, RE_Options().set_ungreedy(true), false, "'this'" ); |
| 703 |
|
GetOneOptionResult("UNGREEDY", "('.*?')", str, options, false, "'this' is the 'world'" ); |
| 704 |
|
|
| 705 |
|
options.set_ungreedy(false); |
| 706 |
|
GetOneOptionResult("no UNGREEDY", "('.*')", str, options, false, "'this' is the 'world'" ); |
| 707 |
|
GetOneOptionResult("no UNGREEDY", "('.*?')", str, options, false, "'this'" ); |
| 708 |
|
} |
| 709 |
|
|
| 710 |
|
static void Test_all_options() { |
| 711 |
|
const char *str = "HELLO\n" "cruel\n" "world"; |
| 712 |
|
RE_Options options; |
| 713 |
|
options.set_all_options(PCRE_CASELESS | PCRE_DOTALL); |
| 714 |
|
|
| 715 |
|
TestOneOption("all_options (CASELESS|DOTALL)", "^hello.*WORLD", str , options, false); |
| 716 |
|
options.set_all_options(0); |
| 717 |
|
TestOneOption("all_options (0)", "^hello.*WORLD", str , options, false, false); |
| 718 |
|
options.set_all_options(PCRE_MULTILINE | PCRE_EXTENDED); |
| 719 |
|
|
| 720 |
|
TestOneOption("all_options (MULTILINE|EXTENDED)", " ^ c r u e l $ ", str, options, false); |
| 721 |
|
TestOneOption("all_options (MULTILINE|EXTENDED) with constructor", |
| 722 |
|
" ^ c r u e l $ ", |
| 723 |
|
str, |
| 724 |
|
RE_Options(PCRE_MULTILINE | PCRE_EXTENDED), |
| 725 |
|
false); |
| 726 |
|
|
| 727 |
|
TestOneOption("all_options (MULTILINE|EXTENDED) with concatenation", |
| 728 |
|
" ^ c r u e l $ ", |
| 729 |
|
str, |
| 730 |
|
RE_Options() |
| 731 |
|
.set_multiline(true) |
| 732 |
|
.set_extended(true), |
| 733 |
|
false); |
| 734 |
|
|
| 735 |
|
options.set_all_options(0); |
| 736 |
|
TestOneOption("all_options (0)", "^ c r u e l $", str, options, false, false); |
| 737 |
|
|
| 738 |
|
} |
| 739 |
|
|
| 740 |
|
static void TestOptions() { |
| 741 |
|
printf("Testing Options\n"); |
| 742 |
|
Test_CASELESS(); |
| 743 |
|
Test_MULTILINE(); |
| 744 |
|
Test_DOTALL(); |
| 745 |
|
Test_DOLLAR_ENDONLY(); |
| 746 |
|
Test_EXTENDED(); |
| 747 |
|
Test_NO_AUTO_CAPTURE(); |
| 748 |
|
Test_UNGREEDY(); |
| 749 |
|
Test_EXTRA(); |
| 750 |
|
Test_all_options(); |
| 751 |
|
} |
| 752 |
|
|
| 753 |
|
static void TestConstructors() { |
| 754 |
|
printf("Testing constructors\n"); |
| 755 |
|
|
| 756 |
|
RE_Options options; |
| 757 |
|
options.set_dotall(true); |
| 758 |
|
const char *str = "HELLO\n" "cruel\n" "world"; |
| 759 |
|
|
| 760 |
|
RE orig("HELLO.*world", options); |
| 761 |
|
CHECK(orig.FullMatch(str)); |
| 762 |
|
|
| 763 |
|
RE copy1(orig); |
| 764 |
|
CHECK(copy1.FullMatch(str)); |
| 765 |
|
|
| 766 |
|
RE copy2("not a match"); |
| 767 |
|
CHECK(!copy2.FullMatch(str)); |
| 768 |
|
copy2 = copy1; |
| 769 |
|
CHECK(copy2.FullMatch(str)); |
| 770 |
|
copy2 = orig; |
| 771 |
|
CHECK(copy2.FullMatch(str)); |
| 772 |
|
|
| 773 |
|
// Make sure when we assign to ourselves, nothing bad happens |
| 774 |
|
orig = orig; |
| 775 |
|
copy1 = copy1; |
| 776 |
|
copy2 = copy2; |
| 777 |
|
CHECK(orig.FullMatch(str)); |
| 778 |
|
CHECK(copy1.FullMatch(str)); |
| 779 |
|
CHECK(copy2.FullMatch(str)); |
| 780 |
|
} |
| 781 |
|
|
| 782 |
int main(int argc, char** argv) { |
int main(int argc, char** argv) { |
| 783 |
// Treat any flag as --help |
// Treat any flag as --help |
| 813 |
/***** FullMatch with no args *****/ |
/***** FullMatch with no args *****/ |
| 814 |
|
|
| 815 |
CHECK(RE("h.*o").FullMatch("hello")); |
CHECK(RE("h.*o").FullMatch("hello")); |
| 816 |
CHECK(!RE("h.*o").FullMatch("othello")); |
CHECK(!RE("h.*o").FullMatch("othello")); // Must be anchored at front |
| 817 |
CHECK(!RE("h.*o").FullMatch("hello!")); |
CHECK(!RE("h.*o").FullMatch("hello!")); // Must be anchored at end |
| 818 |
|
CHECK(RE("a*").FullMatch("aaaa")); // Fullmatch with normal op |
| 819 |
|
CHECK(RE("a*?").FullMatch("aaaa")); // Fullmatch with nongreedy op |
| 820 |
|
CHECK(RE("a*?\\z").FullMatch("aaaa")); // Two unusual ops |
| 821 |
|
|
| 822 |
/***** FullMatch with args *****/ |
/***** FullMatch with args *****/ |
| 823 |
|
|
| 912 |
CHECK(!RE("(\\d+)").FullMatch("4294967296", &v)); |
CHECK(!RE("(\\d+)").FullMatch("4294967296", &v)); |
| 913 |
} |
} |
| 914 |
#ifdef HAVE_LONG_LONG |
#ifdef HAVE_LONG_LONG |
| 915 |
|
# if defined(__MINGW__) || defined(__MINGW32__) |
| 916 |
|
# define LLD "%I64d" |
| 917 |
|
# define LLU "%I64u" |
| 918 |
|
# else |
| 919 |
|
# define LLD "%lld" |
| 920 |
|
# define LLU "%llu" |
| 921 |
|
# endif |
| 922 |
{ |
{ |
| 923 |
long long v; |
long long v; |
| 924 |
static const long long max_value = 0x7fffffffffffffffLL; |
static const long long max_value = 0x7fffffffffffffffLL; |
| 928 |
CHECK(RE("(-?\\d+)").FullMatch("100", &v)); CHECK_EQ(v, 100); |
CHECK(RE("(-?\\d+)").FullMatch("100", &v)); CHECK_EQ(v, 100); |
| 929 |
CHECK(RE("(-?\\d+)").FullMatch("-100",&v)); CHECK_EQ(v, -100); |
CHECK(RE("(-?\\d+)").FullMatch("-100",&v)); CHECK_EQ(v, -100); |
| 930 |
|
|
| 931 |
snprintf(buf, sizeof(buf), "%lld", max_value); |
snprintf(buf, sizeof(buf), LLD, max_value); |
| 932 |
CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, max_value); |
CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, max_value); |
| 933 |
|
|
| 934 |
snprintf(buf, sizeof(buf), "%lld", min_value); |
snprintf(buf, sizeof(buf), LLD, min_value); |
| 935 |
CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, min_value); |
CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, min_value); |
| 936 |
|
|
| 937 |
snprintf(buf, sizeof(buf), "%lld", max_value); |
snprintf(buf, sizeof(buf), LLD, max_value); |
| 938 |
assert(buf[strlen(buf)-1] != '9'); |
assert(buf[strlen(buf)-1] != '9'); |
| 939 |
buf[strlen(buf)-1]++; |
buf[strlen(buf)-1]++; |
| 940 |
CHECK(!RE("(-?\\d+)").FullMatch(buf, &v)); |
CHECK(!RE("(-?\\d+)").FullMatch(buf, &v)); |
| 941 |
|
|
| 942 |
snprintf(buf, sizeof(buf), "%lld", min_value); |
snprintf(buf, sizeof(buf), LLD, min_value); |
| 943 |
assert(buf[strlen(buf)-1] != '9'); |
assert(buf[strlen(buf)-1] != '9'); |
| 944 |
buf[strlen(buf)-1]++; |
buf[strlen(buf)-1]++; |
| 945 |
CHECK(!RE("(-?\\d+)").FullMatch(buf, &v)); |
CHECK(!RE("(-?\\d+)").FullMatch(buf, &v)); |
| 955 |
CHECK(RE("(-?\\d+)").FullMatch("100",&v)); CHECK_EQ(v, 100); |
CHECK(RE("(-?\\d+)").FullMatch("100",&v)); CHECK_EQ(v, 100); |
| 956 |
CHECK(RE("(-?\\d+)").FullMatch("-100",&v2)); CHECK_EQ(v2, -100); |
CHECK(RE("(-?\\d+)").FullMatch("-100",&v2)); CHECK_EQ(v2, -100); |
| 957 |
|
|
| 958 |
snprintf(buf, sizeof(buf), "%llu", max_value); |
snprintf(buf, sizeof(buf), LLU, max_value); |
| 959 |
CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, max_value); |
CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, max_value); |
| 960 |
|
|
| 961 |
assert(buf[strlen(buf)-1] != '9'); |
assert(buf[strlen(buf)-1] != '9'); |
| 1107 |
CHECK(RE("h.*o").PartialMatch("hello!")); |
CHECK(RE("h.*o").PartialMatch("hello!")); |
| 1108 |
CHECK(RE("((((((((((((((((((((x))))))))))))))))))))").PartialMatch("x")); |
CHECK(RE("((((((((((((((((((((x))))))))))))))))))))").PartialMatch("x")); |
| 1109 |
|
|
| 1110 |
|
/***** other tests *****/ |
| 1111 |
|
|
| 1112 |
RadixTests(); |
RadixTests(); |
| 1113 |
TestReplace(); |
TestReplace(); |
| 1114 |
TestExtract(); |
TestExtract(); |
| 1115 |
TestConsume(); |
TestConsume(); |
| 1116 |
TestFindAndConsume(); |
TestFindAndConsume(); |
| 1117 |
|
TestQuoteMetaAll(); |
| 1118 |
TestMatchNumberPeculiarity(); |
TestMatchNumberPeculiarity(); |
| 1119 |
|
|
| 1120 |
// Check the pattern() accessor |
// Check the pattern() accessor |
| 1226 |
CHECK(!re.error().empty()); |
CHECK(!re.error().empty()); |
| 1227 |
} |
} |
| 1228 |
|
|
| 1229 |
// Test that recursion is stopped: there will be some errors reported |
// Test that recursion is stopped |
| 1230 |
int matchlimit = 5000; |
TestRecursion(); |
| 1231 |
int bytes = 15 * 1024; // enough to crash if there was no match limit |
|
| 1232 |
TestRecursion(bytes, ".", matchlimit); |
// Test Options |
| 1233 |
TestRecursion(bytes, "a", matchlimit); |
if (getenv("VERBOSE_TEST") != NULL) |
| 1234 |
TestRecursion(bytes, "a.", matchlimit); |
VERBOSE_TEST = true; |
| 1235 |
TestRecursion(bytes, "ab.", matchlimit); |
TestOptions(); |
| 1236 |
TestRecursion(bytes, "abc.", matchlimit); |
|
| 1237 |
|
// Test the constructors |
| 1238 |
|
TestConstructors(); |
| 1239 |
|
|
| 1240 |
// Done |
// Done |
| 1241 |
printf("OK\n"); |
printf("OK\n"); |