Go to the documentation of this file.
15 #if !defined(_MSC_VER) && !defined(__CYGWIN__) && !defined(__MINGW32__)
20 #include "util/test.h"
21 #include "util/logging.h"
22 #include "util/strutil.h"
24 #include "re2/regexp.h"
29 #define ASSERT_HEX(type, value) \
33 RE2::FullMatch(#value, "([0-9a-fA-F]+)[uUlL]*", RE2::Hex(&v))); \
34 ASSERT_EQ(v, 0x##value); \
35 ASSERT_TRUE(RE2::FullMatch("0x" #value, "([0-9a-fA-FxX]+)[uUlL]*", \
37 ASSERT_EQ(v, 0x##value); \
47 ASSERT_HEX(
unsigned long long, cafebabedeadbeefULL);
53 #define ASSERT_OCTAL(type, value) \
56 ASSERT_TRUE(RE2::FullMatch(#value, "([0-7]+)[uUlL]*", RE2::Octal(&v))); \
57 ASSERT_EQ(v, 0##value); \
58 ASSERT_TRUE(RE2::FullMatch("0" #value, "([0-9a-fA-FxX]+)[uUlL]*", \
60 ASSERT_EQ(v, 0##value); \
76 #define ASSERT_DECIMAL(type, value) \
79 ASSERT_TRUE(RE2::FullMatch(#value, "(-?[0-9]+)[uUlL]*", &v)); \
80 ASSERT_EQ(v, value); \
82 RE2::FullMatch(#value, "(-?[0-9a-fA-FxX]+)[uUlL]*", RE2::CRadix(&v))); \
83 ASSERT_EQ(v, value); \
102 const char *original;
107 static const ReplaceTest
tests[] = {
108 {
"(qu|[b-df-hj-np-tv-z]*)([a-z]+)",
110 "the quick brown fox jumps over the lazy dogs.",
111 "ethay quick brown fox jumps over the lazy dogs.",
112 "ethay ickquay ownbray oxfay umpsjay overay ethay azylay ogsday.",
116 "abcd.efghi@google.com",
117 "abcd-NOSPAM.efghi@google.com",
118 "abcd-NOSPAM.efghi-NOSPAM@google-NOSPAM.com-NOSPAM",
175 {
"", NULL, NULL, NULL, NULL, 0 }
178 for (
const ReplaceTest* t =
tests; t->original != NULL; t++) {
194 EXPECT_EQ(expect_ok, actual_ok) <<
" for " << rewrite <<
" error: " <<
error;
312 RE2 r(
"(foo)|(bar)|(baz)");
337 RE2 re(
"((\\w+):([0-9]+))");
346 s =
"a chrisr:9000 here";
366 size_t patlen = strlen(
pattern);
367 for (
int i = 0;
i <
size;
i++) {
371 RE2 re(
"([a-zA-Z0-9]|-)+(\\.([a-zA-Z0-9]|-)+)*(\\.)?",
RE2::Quiet);
382 <<
"Unquoted='" << unquoted <<
"', quoted='" << quoted <<
"'.";
393 <<
"Unquoted='" << unquoted <<
"', quoted='" << quoted <<
"'.";
410 TEST(QuoteMeta, SimpleNegative) {
456 TEST(ProgramSize, BigProgram) {
457 RE2 re_simple(
"simple regexp");
458 RE2 re_medium(
"medium.*regexp");
459 RE2 re_complex(
"complex.{1,128}regexp");
470 TEST(ProgramFanout, BigProgram) {
471 RE2 re1(
"(?:(?:(?:(?:(?:.)?){1})*)+)");
472 RE2 re10(
"(?:(?:(?:(?:(?:.)?){10})*)+)");
473 RE2 re100(
"(?:(?:(?:(?:(?:.)?){100})*)+)");
474 RE2 re1000(
"(?:(?:(?:(?:(?:.)?){1000})*)+)");
516 static const char *empties[] = {
528 TEST(EmptyCharset, BitstateAssumptions) {
530 static const char *nop_empties[] = {
531 "((((()))))" "[^\\S\\s]?",
532 "((((()))))" "([^\\S\\s])?",
533 "((((()))))" "([^\\S\\s]|[^\\S\\s])?",
534 "((((()))))" "(([^\\S\\s]|[^\\S\\s])|)"
544 RE2 re(
"(hello world)");
551 RE2 re(
"(?P<A>expr(?P<B>expr)(?P<C>expr))((expr)(?P<D>expr))");
563 RE2 re(
"directions from (?P<S>.*) to (?P<D>.*)");
572 const RE2::Arg*
const matches[4] = {&arg0, &arg1, &arg2, &arg3};
574 re, matches, num_groups));
576 EXPECT_TRUE(named_groups.find(
"S") != named_groups.end());
577 EXPECT_TRUE(named_groups.find(
"D") != named_groups.end());
580 int source_group_index = named_groups.find(
"S")->second;
581 int destination_group_index = named_groups.find(
"D")->second;
645 RE2::FullMatch(
"1234567890123456789012345678901234567890",
"(\\d+)", &
i));
755 #if defined(_POSIX_MAPPED_FILES) && _POSIX_MAPPED_FILES > 0
758 long pagesize = sysconf(_SC_PAGE_SIZE);
760 #ifndef MAP_ANONYMOUS
761 #define MAP_ANONYMOUS MAP_ANON
763 v =
static_cast<char*
>(mmap(NULL, 2*pagesize, PROT_READ|PROT_WRITE,
764 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0));
766 LOG(
INFO) <<
"Memory at " << (
void*)
v;
767 ASSERT_EQ(munmap(
v + pagesize, pagesize), 0) <<
" error " << errno;
768 v[pagesize - 1] =
'1';
787 ASSERT_EQ(c,
static_cast<unsigned char>(
'H'));
906 #if !defined(_MSC_VER) && !defined(__CYGWIN__) && !defined(__MINGW32__)
1003 &
a[2], &
a[3], &
a[4]));
1012 &
a[1], &
a[2], &
a[3], &
a[4], &
a[5]));
1022 &
a[0], &
a[1], &
a[2], &
a[3], &
a[4], &
a[5], &
a[6]));
1033 "(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)"
1034 "(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)(\\d)",
1035 &
a[0], &
a[1], &
a[2], &
a[3], &
a[4], &
a[5], &
a[6],
1036 &
a[7], &
a[8], &
a[9], &
a[10], &
a[11], &
a[12],
1037 &
a[13], &
a[14], &
a[15]));
1060 const RE2 re(kPattern);
1076 const char utf8_string[] = {
1077 (char)0xe6, (
char)0x97, (char)0xa5,
1078 (
char)0xe6, (char)0x9c, (
char)0xac,
1079 (char)0xe8, (
char)0xaa, (char)0x9e,
1082 const char utf8_pattern[] = {
1084 (char)0xe6, (
char)0x9c, (char)0xac,
1092 RE2 re_test2(
"...");
1101 RE2 re_test4(
"(.)");
1108 RE2 re_test6(utf8_string);
1114 RE2 re_test8(utf8_pattern);
1123 const char*
pattern =
"\\w+X";
1132 const char*
pattern =
"(?U)\\w+X";
1198 int bytes = 15 * 1024;
1211 RE2 re(
".{512}x", opt);
1228 RE2 re(
"((?:\\s|xx.*\n|x[*](?:\n|.)*?[*]x)*)");
1234 TEST(CaseInsensitive, MatchAndConsume) {
1248 const char* re_cstring =
".";
1265 "(\\w+)(?::((?:[^;\\\\]|\\\\.)*))?;?",
1281 {
"ab\\αcd",
"\\α" },
1282 {
"ef\\x☺01",
"\\x☺0" },
1283 {
"gh\\x1☺01",
"\\x1☺" },
1284 {
"ij\\x1",
"\\x1" },
1286 {
"uv\\x{0000☺}",
"\\x{0000☺" },
1287 {
"wx\\p{ABC",
"\\p{ABC" },
1288 {
"yz(?smiUX:abc)",
"(?smiUX" },
1289 {
"aa(?sm☺i",
"(?sm☺" },
1290 {
"bb[abc",
"[abc" },
1292 {
"mn\\x1\377",
"" },
1294 {
"st\\x{00000\377",
"" },
1295 {
"zz\\p{\377}",
"" },
1296 {
"zz\\x{00\377}",
"" },
1297 {
"zz(?P<name\377>abc)",
"" },
1313 {
"(.*)",
"abc\ndef\nghi\n",
"abc" },
1314 {
"(?s)(abc.*def)",
"abc\ndef\n", NULL },
1315 {
"(abc(.|\n)*def)",
"abc\ndef\n", NULL },
1316 {
"(abc[^x]*def)",
"abc\ndef\n", NULL },
1317 {
"(abc[^x]*def)",
"abczzzdef\ndef\n",
"abczzzdef" },
1324 RE2 re(t.regexp, opt);
1325 if (t.match == NULL) {
1349 RE2 re(
"(r)(e)", opt);
1360 RE2 re(
"(_________$)", opt);
1361 StringPiece s =
"xxxxxxxxxxxxxxxxxxxxxxxxxx_________x";
1486 RE2 re(
"(((((llx((-3)|(4)))(;(llx((-3)|(4))))*))))");
1503 RE2 re(
"((abc)(?P<G2>)|((e+)(?P<G2>.*)(?P<G1>u+)))");
1506 std::map<int, std::string> want;
1523 RE2 re(
"\\140\\440\\174\\271\\150\\656\\106\\201\\004\\332",
RE2::Latin1);
1531 (char)0x28, (
char)0x28, (char)0xfc, (
char)0xfc, (char)0x08, (
char)0x08,
1532 (char)0x26, (
char)0x26, (char)0x28, (
char)0xc2, (char)0x9b, (
char)0xc5,
1533 (char)0xc5, (
char)0xd4, (char)0x8f, (
char)0x8f, (char)0x69, (
char)0x69,
1534 (char)0xe7, (
char)0x29, (char)0x7b, (
char)0x37, (char)0x31, (
char)0x31,
1535 (char)0x7d, (
char)0xae, (char)0x7c, (
char)0x7c, (char)0xf3, (
char)0x29,
1536 (char)0xae, (
char)0xae, (char)0x2e, (
char)0x2a, (char)0x29, (
char)0x00,
1553 (char)0x28, (
char)0x05, (char)0x05, (
char)0x41, (char)0x41, (
char)0x28,
1554 (char)0x24, (
char)0x5b, (char)0x5e, (
char)0xf5, (char)0x87, (
char)0x87,
1555 (char)0x90, (
char)0x29, (char)0x5d, (
char)0x29, (char)0x29, (
char)0x00,
1566 (char)0x29, (
char)0x29, (char)0x24, (
char)0x00,
1569 (char)0x28, (
char)0x0a, (char)0x2a, (
char)0x2a, (char)0x29, (
char)0x00,
1578 RE2 re((
const char*)
b, opt);
1591 RE2 re(
"g\\p{Zl}]", opt);
1601 RE2 re(
"a\\C*?c|a\\C*?b");
1608 ASSERT_EQ(
m, s) <<
" (UNANCHORED) got m='" <<
m <<
"', want '" << s <<
"'";
1611 ASSERT_EQ(
m, s) <<
" (ANCHOR_BOTH) got m='" <<
m <<
"', want '" << s <<
"'";
#define EXPECT_FALSE(condition)
#define ASSERT_NE(val1, val2)
static Arg CRadix(short *x)
static bool FullMatch(const StringPiece &text, const RE2 &re, A &&... a)
static bool PartialMatchN(const StringPiece &text, const RE2 &re, const Arg *const args[], int n)
return memset(p, 0, total)
Span< const NamedGroup > NamedGroups()
const std::map< int, std::string > & CapturingGroupNames() const
static bool FullMatchN(const StringPiece &text, const RE2 &re, const Arg *const args[], int n)
ErrorCode error_code() const
void Consume(CordRep *rep, ConsumeFn consume_fn)
int ProgramFanout(std::map< int, int > *histogram) const
int ReverseProgramFanout(std::map< int, int > *histogram) const
static bool Extract(const StringPiece &text, const RE2 &re, const StringPiece &rewrite, std::string *out)
const std::string & error() const
void set_never_nl(bool b)
static bool Consume(StringPiece *input, const RE2 &re, A &&... a)
void set_case_sensitive(bool b)
static std::string UTF8(Rune r)
static bool Replace(std::string *str, const RE2 &re, const StringPiece &rewrite)
static bool FindAndConsume(StringPiece *input, const RE2 &re, A &&... a)
#define ASSERT_DECIMAL(type, value)
static bool FindAndConsumeN(StringPiece *input, const RE2 &re, const Arg *const args[], int n)
const std::map< std::string, int > & NamedCapturingGroups() const
std::string ToString(const grpc::string_ref &r)
bool CheckRewriteString(const StringPiece &rewrite, std::string *error) const
void set_posix_syntax(bool b)
void set_log_errors(bool b)
unsigned __int64 uint64_t
std::string StringPrintf(const char *format,...)
static void TestQuoteMeta(const std::string &unquoted, const RE2::Options &options=RE2::DefaultOptions)
static void TestCheckRewriteString(const char *regexp, const char *rewrite, bool expect_ok)
static grpc_histogram * histogram
std::string Replace(std::string s, const std::string &from, const std::string &to)
static bool PartialMatch(const StringPiece &text, const RE2 &re, A &&... a)
static struct re2::ErrorTest error_tests[]
void set_max_mem(int64_t m)
void set_never_capture(bool b)
static bool ConsumeN(StringPiece *input, const RE2 &re, const Arg *const args[], int n)
static bool Match(const upb_msgdef *m, const char *name, const upb_fielddef **f, const upb_oneofdef **o, const char *prefix, const char *suffix)
bool Match(const StringPiece &text, size_t startpos, size_t endpos, Anchor re_anchor, StringPiece *submatch, int nsubmatch) const
int ReverseProgramSize() const
static int GlobalReplace(std::string *str, const RE2 &re, const StringPiece &rewrite)
static std::string QuoteMeta(const StringPiece &unquoted)
const_pointer data() const
#define ASSERT_HEX(type, value)
void set_encoding(Encoding encoding)
const std::string & error_arg() const
void set_longest_match(bool b)
static struct re2::NeverTest never_tests[]
const std::string & pattern() const
static void NegativeTestQuoteMeta(const std::string &unquoted, const std::string &should_not_match, const RE2::Options &options=RE2::DefaultOptions)
#define ASSERT_TRUE(condition)
#define ASSERT_FALSE(condition)
#define EXPECT_TRUE(condition)
void FindAndConsume(int iters, int nbytes)
static void TestRecursion(int size, const char *pattern)
#define ASSERT_OCTAL(type, value)
TEST(TestCharClassBuilder, Adds)
#define ASSERT_GT(val1, val2)
int NumberOfCapturingGroups() const
static bool to_string(zval *from)
#define ASSERT_EQ(val1, val2)
grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:00