Regex.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "LibRegex/RegexMatcher.h"
  7. #include <LibTest/TestCase.h> // import first, to prevent warning of VERIFY* redefinition
  8. #include <AK/StringBuilder.h>
  9. #include <LibRegex/Regex.h>
  10. #include <LibRegex/RegexDebug.h>
  11. #include <stdio.h>
  12. static ECMAScriptOptions match_test_api_options(const ECMAScriptOptions options)
  13. {
  14. return options;
  15. }
  16. static PosixOptions match_test_api_options(const PosixOptions options)
  17. {
  18. return options;
  19. }
  20. TEST_CASE(regex_options_ecmascript)
  21. {
  22. ECMAScriptOptions eo;
  23. eo |= ECMAScriptFlags::Global;
  24. EXPECT(eo & ECMAScriptFlags::Global);
  25. EXPECT(!(eo & ECMAScriptFlags::Insensitive));
  26. eo = match_test_api_options(ECMAScriptFlags::Global | ECMAScriptFlags::Insensitive | ECMAScriptFlags::Sticky);
  27. EXPECT(eo & ECMAScriptFlags::Global);
  28. EXPECT(eo & ECMAScriptFlags::Insensitive);
  29. EXPECT(eo & ECMAScriptFlags::Sticky);
  30. EXPECT(!(eo & ECMAScriptFlags::Unicode));
  31. EXPECT(!(eo & ECMAScriptFlags::Multiline));
  32. EXPECT(!(eo & ECMAScriptFlags::SingleLine));
  33. eo &= ECMAScriptFlags::Insensitive;
  34. EXPECT(!(eo & ECMAScriptFlags::Global));
  35. EXPECT(eo & ECMAScriptFlags::Insensitive);
  36. EXPECT(!(eo & ECMAScriptFlags::Multiline));
  37. eo &= ECMAScriptFlags::Sticky;
  38. EXPECT(!(eo & ECMAScriptFlags::Global));
  39. EXPECT(!(eo & ECMAScriptFlags::Insensitive));
  40. EXPECT(!(eo & ECMAScriptFlags::Multiline));
  41. EXPECT(!(eo & ECMAScriptFlags::Sticky));
  42. eo = ~ECMAScriptFlags::Insensitive;
  43. EXPECT(eo & ECMAScriptFlags::Global);
  44. EXPECT(!(eo & ECMAScriptFlags::Insensitive));
  45. EXPECT(eo & ECMAScriptFlags::Multiline);
  46. EXPECT(eo & ECMAScriptFlags::Sticky);
  47. }
  48. TEST_CASE(regex_options_posix)
  49. {
  50. PosixOptions eo;
  51. eo |= PosixFlags::Global;
  52. EXPECT(eo & PosixFlags::Global);
  53. EXPECT(!(eo & PosixFlags::Insensitive));
  54. eo = match_test_api_options(PosixFlags::Global | PosixFlags::Insensitive | PosixFlags::MatchNotBeginOfLine);
  55. EXPECT(eo & PosixFlags::Global);
  56. EXPECT(eo & PosixFlags::Insensitive);
  57. EXPECT(eo & PosixFlags::MatchNotBeginOfLine);
  58. EXPECT(!(eo & PosixFlags::Unicode));
  59. EXPECT(!(eo & PosixFlags::Multiline));
  60. eo &= PosixFlags::Insensitive;
  61. EXPECT(!(eo & PosixFlags::Global));
  62. EXPECT(eo & PosixFlags::Insensitive);
  63. EXPECT(!(eo & PosixFlags::Multiline));
  64. eo &= PosixFlags::MatchNotBeginOfLine;
  65. EXPECT(!(eo & PosixFlags::Global));
  66. EXPECT(!(eo & PosixFlags::Insensitive));
  67. EXPECT(!(eo & PosixFlags::Multiline));
  68. eo = ~PosixFlags::Insensitive;
  69. EXPECT(eo & PosixFlags::Global);
  70. EXPECT(!(eo & PosixFlags::Insensitive));
  71. EXPECT(eo & PosixFlags::Multiline);
  72. }
  73. TEST_CASE(regex_lexer)
  74. {
  75. Lexer l("/[.*+?^${}()|[\\]\\\\]/g");
  76. EXPECT(l.next().type() == regex::TokenType::Slash);
  77. EXPECT(l.next().type() == regex::TokenType::LeftBracket);
  78. EXPECT(l.next().type() == regex::TokenType::Period);
  79. EXPECT(l.next().type() == regex::TokenType::Asterisk);
  80. EXPECT(l.next().type() == regex::TokenType::Plus);
  81. EXPECT(l.next().type() == regex::TokenType::Questionmark);
  82. EXPECT(l.next().type() == regex::TokenType::Circumflex);
  83. EXPECT(l.next().type() == regex::TokenType::Dollar);
  84. EXPECT(l.next().type() == regex::TokenType::LeftCurly);
  85. EXPECT(l.next().type() == regex::TokenType::RightCurly);
  86. EXPECT(l.next().type() == regex::TokenType::LeftParen);
  87. EXPECT(l.next().type() == regex::TokenType::RightParen);
  88. EXPECT(l.next().type() == regex::TokenType::Pipe);
  89. EXPECT(l.next().type() == regex::TokenType::LeftBracket);
  90. EXPECT(l.next().type() == regex::TokenType::EscapeSequence);
  91. EXPECT(l.next().type() == regex::TokenType::EscapeSequence);
  92. EXPECT(l.next().type() == regex::TokenType::RightBracket);
  93. EXPECT(l.next().type() == regex::TokenType::Slash);
  94. EXPECT(l.next().type() == regex::TokenType::Char);
  95. }
  96. TEST_CASE(parser_error_parens)
  97. {
  98. String pattern = "test()test";
  99. Lexer l(pattern);
  100. PosixExtendedParser p(l);
  101. p.parse();
  102. EXPECT(p.has_error());
  103. EXPECT(p.error() == Error::EmptySubExpression);
  104. }
  105. TEST_CASE(parser_error_special_characters_used_at_wrong_place)
  106. {
  107. String pattern;
  108. Vector<char, 5> chars = { '*', '+', '?', '{' };
  109. StringBuilder b;
  110. Lexer l;
  111. PosixExtended p(l);
  112. for (auto& ch : chars) {
  113. // First in ere
  114. b.clear();
  115. b.append(ch);
  116. pattern = b.build();
  117. l.set_source(pattern);
  118. p.parse();
  119. EXPECT(p.has_error());
  120. EXPECT(p.error() == Error::InvalidRepetitionMarker);
  121. // After vertical line
  122. b.clear();
  123. b.append("a|");
  124. b.append(ch);
  125. pattern = b.build();
  126. l.set_source(pattern);
  127. p.parse();
  128. EXPECT(p.has_error());
  129. EXPECT(p.error() == Error::InvalidRepetitionMarker);
  130. // After circumflex
  131. b.clear();
  132. b.append("^");
  133. b.append(ch);
  134. pattern = b.build();
  135. l.set_source(pattern);
  136. p.parse();
  137. EXPECT(p.has_error());
  138. EXPECT(p.error() == Error::InvalidRepetitionMarker);
  139. // After dollar
  140. b.clear();
  141. b.append("$");
  142. b.append(ch);
  143. pattern = b.build();
  144. l.set_source(pattern);
  145. p.parse();
  146. EXPECT(p.has_error());
  147. EXPECT(p.error() == Error::InvalidRepetitionMarker);
  148. // After left parens
  149. b.clear();
  150. b.append("(");
  151. b.append(ch);
  152. b.append(")");
  153. pattern = b.build();
  154. l.set_source(pattern);
  155. p.parse();
  156. EXPECT(p.has_error());
  157. EXPECT(p.error() == Error::InvalidRepetitionMarker);
  158. }
  159. }
  160. TEST_CASE(parser_error_vertical_line_used_at_wrong_place)
  161. {
  162. Lexer l;
  163. PosixExtended p(l);
  164. // First in ere
  165. l.set_source("|asdf");
  166. p.parse();
  167. EXPECT(p.has_error());
  168. EXPECT(p.error() == Error::EmptySubExpression);
  169. // Last in ere
  170. l.set_source("asdf|");
  171. p.parse();
  172. EXPECT(p.has_error());
  173. EXPECT(p.error() == Error::EmptySubExpression);
  174. // After left parens
  175. l.set_source("(|asdf)");
  176. p.parse();
  177. EXPECT(p.has_error());
  178. EXPECT(p.error() == Error::EmptySubExpression);
  179. // Proceed right parens
  180. l.set_source("(asdf)|");
  181. p.parse();
  182. EXPECT(p.has_error());
  183. EXPECT(p.error() == Error::EmptySubExpression);
  184. }
  185. TEST_CASE(catch_all_first)
  186. {
  187. Regex<PosixExtended> re("^.*$");
  188. RegexResult m;
  189. re.match("Hello World", m);
  190. EXPECT(m.count == 1);
  191. EXPECT(re.match("Hello World", m));
  192. }
  193. TEST_CASE(catch_all)
  194. {
  195. Regex<PosixExtended> re("^.*$", PosixFlags::Global);
  196. EXPECT(re.has_match("Hello World"));
  197. EXPECT(re.match("Hello World").success);
  198. EXPECT(re.match("Hello World").count == 1);
  199. EXPECT(has_match("Hello World", re));
  200. auto res = match("Hello World", re);
  201. EXPECT(res.success);
  202. EXPECT(res.count == 1);
  203. EXPECT(res.matches.size() == 1);
  204. EXPECT(res.matches.first().view == "Hello World");
  205. }
  206. TEST_CASE(catch_all_again)
  207. {
  208. Regex<PosixExtended> re("^.*$", PosixFlags::Extra);
  209. EXPECT_EQ(has_match("Hello World", re), true);
  210. }
  211. TEST_CASE(char_utf8)
  212. {
  213. Regex<PosixExtended> re("😀");
  214. RegexResult result;
  215. EXPECT_EQ((result = match("Привет, мир! 😀 γειά σου κόσμος 😀 こんにちは世界", re, PosixFlags::Global)).success, true);
  216. EXPECT_EQ(result.count, 2u);
  217. }
  218. TEST_CASE(catch_all_newline)
  219. {
  220. Regex<PosixExtended> re("^.*$", PosixFlags::Multiline | PosixFlags::StringCopyMatches);
  221. RegexResult result;
  222. auto lambda = [&result, &re]() {
  223. String aaa = "Hello World\nTest\n1234\n";
  224. result = match(aaa, re);
  225. EXPECT_EQ(result.success, true);
  226. };
  227. lambda();
  228. EXPECT_EQ(result.count, 3u);
  229. EXPECT_EQ(result.matches.at(0).view, "Hello World");
  230. EXPECT_EQ(result.matches.at(1).view, "Test");
  231. EXPECT_EQ(result.matches.at(2).view, "1234");
  232. }
  233. TEST_CASE(catch_all_newline_view)
  234. {
  235. Regex<PosixExtended> re("^.*$", PosixFlags::Multiline);
  236. RegexResult result;
  237. String aaa = "Hello World\nTest\n1234\n";
  238. result = match(aaa, re);
  239. EXPECT_EQ(result.success, true);
  240. EXPECT_EQ(result.count, 3u);
  241. String str = "Hello World";
  242. EXPECT_EQ(result.matches.at(0).view, str.view());
  243. EXPECT_EQ(result.matches.at(1).view, "Test");
  244. EXPECT_EQ(result.matches.at(2).view, "1234");
  245. }
  246. TEST_CASE(catch_all_newline_2)
  247. {
  248. Regex<PosixExtended> re("^.*$");
  249. RegexResult result;
  250. result = match("Hello World\nTest\n1234\n", re, PosixFlags::Multiline | PosixFlags::StringCopyMatches);
  251. EXPECT_EQ(result.success, true);
  252. EXPECT_EQ(result.count, 3u);
  253. EXPECT_EQ(result.matches.at(0).view, "Hello World");
  254. EXPECT_EQ(result.matches.at(1).view, "Test");
  255. EXPECT_EQ(result.matches.at(2).view, "1234");
  256. result = match("Hello World\nTest\n1234\n", re);
  257. EXPECT_EQ(result.success, true);
  258. EXPECT_EQ(result.count, 1u);
  259. EXPECT_EQ(result.matches.at(0).view, "Hello World\nTest\n1234\n");
  260. }
  261. TEST_CASE(match_all_character_class)
  262. {
  263. Regex<PosixExtended> re("[[:alpha:]]");
  264. String str = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  265. RegexResult result = match(str, re, PosixFlags::Global | PosixFlags::StringCopyMatches);
  266. EXPECT_EQ(result.success, true);
  267. EXPECT_EQ(result.count, 24u);
  268. EXPECT_EQ(result.matches.at(0).view, "W");
  269. EXPECT_EQ(result.matches.at(1).view, "i");
  270. EXPECT_EQ(result.matches.at(2).view, "n");
  271. EXPECT(&result.matches.at(0).view.characters_without_null_termination()[0] != &str.view().characters_without_null_termination()[1]);
  272. }
  273. TEST_CASE(match_character_class_with_assertion)
  274. {
  275. Regex<PosixExtended> re("[[:alpha:]]+$");
  276. String str = "abcdef";
  277. RegexResult result = match(str, re);
  278. EXPECT_EQ(result.success, true);
  279. EXPECT_EQ(result.count, 1u);
  280. }
  281. TEST_CASE(example_for_git_commit)
  282. {
  283. Regex<PosixExtended> re("^.*$");
  284. auto result = re.match("Well, hello friends!\nHello World!");
  285. EXPECT(result.success);
  286. EXPECT(result.count == 1);
  287. EXPECT(result.matches.at(0).view.starts_with("Well"));
  288. EXPECT(result.matches.at(0).view.length() == 33);
  289. EXPECT(re.has_match("Well,...."));
  290. result = re.match("Well, hello friends!\nHello World!", PosixFlags::Multiline);
  291. EXPECT(result.success);
  292. EXPECT(result.count == 2);
  293. EXPECT(result.matches.at(0).view == "Well, hello friends!");
  294. EXPECT(result.matches.at(1).view == "Hello World!");
  295. }
  296. TEST_CASE(email_address)
  297. {
  298. Regex<PosixExtended> re("^[A-Z0-9a-z._%+-]{1,64}@([A-Za-z0-9-]{1,63}\\.){1,125}[A-Za-z]{2,63}$");
  299. EXPECT(re.has_match("hello.world@domain.tld"));
  300. EXPECT(re.has_match("this.is.a.very_long_email_address@world.wide.web"));
  301. }
  302. TEST_CASE(ini_file_entries)
  303. {
  304. Regex<PosixExtended> re("[[:alpha:]]*=([[:digit:]]*)|\\[(.*)\\]");
  305. RegexResult result;
  306. if constexpr (REGEX_DEBUG) {
  307. RegexDebug regex_dbg(stderr);
  308. regex_dbg.print_raw_bytecode(re);
  309. regex_dbg.print_header();
  310. regex_dbg.print_bytecode(re);
  311. }
  312. String haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  313. EXPECT_EQ(re.search(haystack.view(), result, PosixFlags::Multiline), true);
  314. EXPECT_EQ(result.count, 3u);
  315. if constexpr (REGEX_DEBUG) {
  316. for (auto& v : result.matches)
  317. fprintf(stderr, "%s\n", v.view.to_string().characters());
  318. }
  319. EXPECT_EQ(result.matches.at(0).view, "[Window]");
  320. EXPECT_EQ(result.capture_group_matches.at(0).at(0).view, "Window");
  321. EXPECT_EQ(result.matches.at(1).view, "Opacity=255");
  322. EXPECT_EQ(result.matches.at(1).line, 1u);
  323. EXPECT_EQ(result.matches.at(1).column, 0u);
  324. EXPECT_EQ(result.capture_group_matches.at(1).at(0).view, "255");
  325. EXPECT_EQ(result.capture_group_matches.at(1).at(0).line, 1u);
  326. EXPECT_EQ(result.capture_group_matches.at(1).at(0).column, 8u);
  327. EXPECT_EQ(result.matches.at(2).view, "AudibleBeep=0");
  328. EXPECT_EQ(result.capture_group_matches.at(2).at(0).view, "0");
  329. EXPECT_EQ(result.capture_group_matches.at(2).at(0).line, 2u);
  330. EXPECT_EQ(result.capture_group_matches.at(2).at(0).column, 12u);
  331. }
  332. TEST_CASE(ini_file_entries2)
  333. {
  334. Regex<PosixExtended> re("[[:alpha:]]*=([[:digit:]]*)");
  335. RegexResult result;
  336. String haystack = "ViewMode=Icon";
  337. EXPECT_EQ(re.match(haystack.view(), result), false);
  338. EXPECT_EQ(result.count, 0u);
  339. EXPECT_EQ(re.search(haystack.view(), result), true);
  340. EXPECT_EQ(result.count, 1u);
  341. }
  342. TEST_CASE(named_capture_group)
  343. {
  344. Regex<PosixExtended> re("[[:alpha:]]*=(?<Test>[[:digit:]]*)");
  345. RegexResult result;
  346. if constexpr (REGEX_DEBUG) {
  347. RegexDebug regex_dbg(stderr);
  348. regex_dbg.print_raw_bytecode(re);
  349. regex_dbg.print_header();
  350. regex_dbg.print_bytecode(re);
  351. }
  352. String haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  353. EXPECT_EQ(re.search(haystack, result, PosixFlags::Multiline), true);
  354. EXPECT_EQ(result.count, 2u);
  355. EXPECT_EQ(result.matches.at(0).view, "Opacity=255");
  356. EXPECT_EQ(result.named_capture_group_matches.at(0).ensure("Test").view, "255");
  357. EXPECT_EQ(result.matches.at(1).view, "AudibleBeep=0");
  358. EXPECT_EQ(result.named_capture_group_matches.at(1).ensure("Test").view, "0");
  359. }
  360. TEST_CASE(a_star)
  361. {
  362. Regex<PosixExtended> re("a*");
  363. RegexResult result;
  364. if constexpr (REGEX_DEBUG) {
  365. RegexDebug regex_dbg(stderr);
  366. regex_dbg.print_raw_bytecode(re);
  367. regex_dbg.print_header();
  368. regex_dbg.print_bytecode(re);
  369. }
  370. String haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  371. EXPECT_EQ(re.search(haystack.view(), result, PosixFlags::Multiline), true);
  372. EXPECT_EQ(result.count, 32u);
  373. EXPECT_EQ(result.matches.at(0).view.length(), 0u);
  374. EXPECT_EQ(result.matches.at(10).view.length(), 1u);
  375. EXPECT_EQ(result.matches.at(10).view, "a");
  376. EXPECT_EQ(result.matches.at(31).view.length(), 0u);
  377. }
  378. TEST_CASE(simple_period_end_benchmark)
  379. {
  380. Regex<PosixExtended> re("hello.$");
  381. RegexResult m;
  382. EXPECT_EQ(re.search("Hello1", m), false);
  383. EXPECT_EQ(re.search("hello1hello1", m), true);
  384. EXPECT_EQ(re.search("hello2hell", m), false);
  385. EXPECT_EQ(re.search("hello?", m), true);
  386. }
  387. TEST_CASE(ECMA262_parse)
  388. {
  389. struct _test {
  390. const char* pattern;
  391. regex::Error expected_error { regex::Error::NoError };
  392. regex::ECMAScriptFlags flags {};
  393. };
  394. constexpr _test tests[] {
  395. { "^hello.$" },
  396. { "^(hello.)$" },
  397. { "^h{0,1}ello.$" },
  398. { "^hello\\W$" },
  399. { "^hell\\w.$" },
  400. { "^hell\\x6f1$" }, // ^hello1$
  401. { "^hel(?:l\\w).$" },
  402. { "^hel(?<LO>l\\w).$" },
  403. { "^[-a-zA-Z\\w\\s]+$" },
  404. { "\\bhello\\B" },
  405. { "^[\\w+/_-]+[=]{0,2}$" }, // #4189
  406. { "^(?:[^<]*(<[\\w\\W]+>)[^>]*$|#([\\w\\-]*)$)" }, // #4189
  407. { "\\/" }, // #4189
  408. { ",/=-:" }, // #4243
  409. { "\\x" }, // Even invalid escapes are allowed if ~unicode.
  410. { "\\", regex::Error::InvalidTrailingEscape },
  411. { "(?", regex::Error::InvalidCaptureGroup },
  412. { "\\u1234", regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
  413. { "[\\u1234]", regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
  414. { ",(?", regex::Error::InvalidCaptureGroup }, // #4583
  415. { "{1}", regex::Error::InvalidPattern },
  416. { "{1,2}", regex::Error::InvalidPattern },
  417. };
  418. for (auto& test : tests) {
  419. Regex<ECMA262> re(test.pattern);
  420. EXPECT_EQ(re.parser_result.error, test.expected_error);
  421. if constexpr (REGEX_DEBUG) {
  422. dbgln("\n");
  423. RegexDebug regex_dbg(stderr);
  424. regex_dbg.print_raw_bytecode(re);
  425. regex_dbg.print_header();
  426. regex_dbg.print_bytecode(re);
  427. dbgln("\n");
  428. }
  429. }
  430. }
  431. TEST_CASE(ECMA262_match)
  432. {
  433. struct _test {
  434. const char* pattern;
  435. const char* subject;
  436. bool matches { true };
  437. ECMAScriptFlags options {};
  438. };
  439. // clang-format off
  440. constexpr _test tests[] {
  441. { "^hello.$", "hello1" },
  442. { "^(hello.)$", "hello1" },
  443. { "^h{0,1}ello.$", "ello1" },
  444. { "^hello\\W$", "hello!" },
  445. { "^hell\\w.$", "hellx!" },
  446. { "^hell\\x6f1$", "hello1" },
  447. { "^hel(?<LO>l.)1$", "hello1" },
  448. { "^hel(?<LO>l.)1*\\k<LO>.$", "hello1lo1" },
  449. { "^[-a-z1-3\\s]+$", "hell2 o1" },
  450. { "^[\\0-\\x1f]$", "\n" },
  451. { .pattern = "\\bhello\\B", .subject = "hello1", .options = ECMAScriptFlags::Global },
  452. { "\\b.*\\b", "hello1" },
  453. { "[^\\D\\S]{2}", "1 " },
  454. { "bar(?=f.)foo", "barfoo" },
  455. { "bar(?=foo)bar", "barbar", false },
  456. { "bar(?!foo)bar", "barbar", true },
  457. { "bar(?!bar)bar", "barbar", false },
  458. { "bar.*(?<=foo)", "barbar", false },
  459. { "bar.*(?<!foo)", "barbar", true },
  460. { "((...)X)+", "fooXbarXbazX", true },
  461. { "(?:)", "", true },
  462. { "\\^", "^" },
  463. // ECMA262, B.1.4. Regular Expression Pattern extensions for browsers
  464. { "{", "{", true, ECMAScriptFlags::BrowserExtended },
  465. { "\\5", "\5", true, ECMAScriptFlags::BrowserExtended },
  466. { "\\05", "\5", true, ECMAScriptFlags::BrowserExtended },
  467. { "\\455", "\45""5", true, ECMAScriptFlags::BrowserExtended },
  468. { "\\314", "\314", true, ECMAScriptFlags::BrowserExtended },
  469. { "\\cf", "\06", true, ECMAScriptFlags::BrowserExtended },
  470. { "\\c1", "\\c1", true, ECMAScriptFlags::BrowserExtended },
  471. { "[\\c1]", "\x11", true, ECMAScriptFlags::BrowserExtended },
  472. { "[\\w-\\d]", "-", true, ECMAScriptFlags::BrowserExtended },
  473. { "^(?:^^\\.?|[!+-]|!=|!==|#|%|%=|&|&&|&&=|&=|\\(|\\*|\\*=|\\+=|,|-=|->|\\/|\\/=|:|::|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|[?@[^]|\\^=|\\^\\^|\\^\\^=|{|\\||\\|=|\\|\\||\\|\\|=|~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*(\\/(?=[^*/])(?:[^/[\\\\]|\\\\[\\S\\s]|\\[(?:[^\\\\\\]]|\\\\[\\S\\s])*(?:]|$))+\\/)",
  474. "return /xx/", true, ECMAScriptFlags::BrowserExtended
  475. }, // #5517, appears to be matching JS expressions that involve regular expressions...
  476. { "a{2,}", "aaaa" }, // #5518
  477. };
  478. // clang-format on
  479. for (auto& test : tests) {
  480. Regex<ECMA262> re(test.pattern, test.options);
  481. if constexpr (REGEX_DEBUG) {
  482. dbgln("\n");
  483. RegexDebug regex_dbg(stderr);
  484. regex_dbg.print_raw_bytecode(re);
  485. regex_dbg.print_header();
  486. regex_dbg.print_bytecode(re);
  487. dbgln("\n");
  488. }
  489. EXPECT_EQ(re.parser_result.error, Error::NoError);
  490. EXPECT_EQ(re.match(test.subject).success, test.matches);
  491. }
  492. }
  493. TEST_CASE(replace)
  494. {
  495. struct _test {
  496. const char* pattern;
  497. const char* replacement;
  498. const char* subject;
  499. const char* expected;
  500. ECMAScriptFlags options {};
  501. };
  502. constexpr _test tests[] {
  503. { "foo(.+)", "aaa", "test", "test" },
  504. { "foo(.+)", "test\\1", "foobar", "testbar" },
  505. { "foo(.+)", "\\2\\1", "foobar", "\\2bar" },
  506. { "foo(.+)", "\\\\\\1", "foobar", "\\bar" },
  507. { "foo(.)", "a\\1", "fooxfooy", "axay", ECMAScriptFlags::Multiline },
  508. };
  509. for (auto& test : tests) {
  510. Regex<ECMA262> re(test.pattern, test.options);
  511. if constexpr (REGEX_DEBUG) {
  512. dbgln("\n");
  513. RegexDebug regex_dbg(stderr);
  514. regex_dbg.print_raw_bytecode(re);
  515. regex_dbg.print_header();
  516. regex_dbg.print_bytecode(re);
  517. dbgln("\n");
  518. }
  519. EXPECT_EQ(re.parser_result.error, Error::NoError);
  520. EXPECT_EQ(re.replace(test.subject, test.replacement), test.expected);
  521. }
  522. }
  523. TEST_CASE(case_insensitive_match)
  524. {
  525. Regex<PosixExtended> re("cd", PosixFlags::Insensitive | PosixFlags::Global);
  526. auto result = re.match("AEKFCD");
  527. EXPECT_EQ(result.success, true);
  528. EXPECT_EQ(result.matches.at(0).column, 4ul);
  529. }