Regex.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibTest/TestCase.h> // import first, to prevent warning of VERIFY* redefinition
  7. #include <AK/Debug.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Tuple.h>
  10. #include <LibRegex/Regex.h>
  11. #include <LibRegex/RegexDebug.h>
  12. #include <LibRegex/RegexMatcher.h>
  13. #include <stdio.h>
  14. static ECMAScriptOptions match_test_api_options(ECMAScriptOptions const options)
  15. {
  16. return options;
  17. }
  18. static PosixOptions match_test_api_options(PosixOptions const options)
  19. {
  20. return options;
  21. }
  22. template<typename... Flags>
  23. static constexpr ECMAScriptFlags combine_flags(Flags&&... flags)
  24. requires((IsSame<Flags, ECMAScriptFlags> && ...))
  25. {
  26. return static_cast<ECMAScriptFlags>((static_cast<regex::FlagsUnderlyingType>(flags) | ...));
  27. }
  28. TEST_CASE(regex_options_ecmascript)
  29. {
  30. ECMAScriptOptions eo;
  31. eo |= ECMAScriptFlags::Global;
  32. EXPECT(eo.has_flag_set(ECMAScriptFlags::Global));
  33. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Insensitive));
  34. eo = match_test_api_options(ECMAScriptFlags::Global | ECMAScriptFlags::Insensitive | ECMAScriptFlags::Sticky);
  35. EXPECT(eo.has_flag_set(ECMAScriptFlags::Global));
  36. EXPECT(eo.has_flag_set(ECMAScriptFlags::Insensitive));
  37. EXPECT(eo.has_flag_set(ECMAScriptFlags::Sticky));
  38. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Unicode));
  39. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Multiline));
  40. EXPECT(!eo.has_flag_set(ECMAScriptFlags::SingleLine));
  41. eo &= ECMAScriptFlags::Insensitive;
  42. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Global));
  43. EXPECT(eo.has_flag_set(ECMAScriptFlags::Insensitive));
  44. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Multiline));
  45. eo &= ECMAScriptFlags::Sticky;
  46. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Global));
  47. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Insensitive));
  48. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Multiline));
  49. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Sticky));
  50. eo = ~ECMAScriptFlags::Insensitive;
  51. EXPECT(eo.has_flag_set(ECMAScriptFlags::Global));
  52. EXPECT(!eo.has_flag_set(ECMAScriptFlags::Insensitive));
  53. EXPECT(eo.has_flag_set(ECMAScriptFlags::Multiline));
  54. EXPECT(eo.has_flag_set(ECMAScriptFlags::Sticky));
  55. }
  56. TEST_CASE(regex_options_posix)
  57. {
  58. PosixOptions eo;
  59. eo |= PosixFlags::Global;
  60. EXPECT(eo.has_flag_set(PosixFlags::Global));
  61. EXPECT(!eo.has_flag_set(PosixFlags::Insensitive));
  62. eo = match_test_api_options(PosixFlags::Global | PosixFlags::Insensitive | PosixFlags::MatchNotBeginOfLine);
  63. EXPECT(eo.has_flag_set(PosixFlags::Global));
  64. EXPECT(eo.has_flag_set(PosixFlags::Insensitive));
  65. EXPECT(eo.has_flag_set(PosixFlags::MatchNotBeginOfLine));
  66. EXPECT(!eo.has_flag_set(PosixFlags::Unicode));
  67. EXPECT(!eo.has_flag_set(PosixFlags::Multiline));
  68. eo &= PosixFlags::Insensitive;
  69. EXPECT(!eo.has_flag_set(PosixFlags::Global));
  70. EXPECT(eo.has_flag_set(PosixFlags::Insensitive));
  71. EXPECT(!eo.has_flag_set(PosixFlags::Multiline));
  72. eo &= PosixFlags::MatchNotBeginOfLine;
  73. EXPECT(!eo.has_flag_set(PosixFlags::Global));
  74. EXPECT(!eo.has_flag_set(PosixFlags::Insensitive));
  75. EXPECT(!eo.has_flag_set(PosixFlags::Multiline));
  76. eo = ~PosixFlags::Insensitive;
  77. EXPECT(eo.has_flag_set(PosixFlags::Global));
  78. EXPECT(!eo.has_flag_set(PosixFlags::Insensitive));
  79. EXPECT(eo.has_flag_set(PosixFlags::Multiline));
  80. }
  81. TEST_CASE(regex_lexer)
  82. {
  83. Lexer l("/[.*+?^${}()|[\\]\\\\]/g"sv);
  84. EXPECT(l.next().type() == regex::TokenType::Slash);
  85. EXPECT(l.next().type() == regex::TokenType::LeftBracket);
  86. EXPECT(l.next().type() == regex::TokenType::Period);
  87. EXPECT(l.next().type() == regex::TokenType::Asterisk);
  88. EXPECT(l.next().type() == regex::TokenType::Plus);
  89. EXPECT(l.next().type() == regex::TokenType::Questionmark);
  90. EXPECT(l.next().type() == regex::TokenType::Circumflex);
  91. EXPECT(l.next().type() == regex::TokenType::Dollar);
  92. EXPECT(l.next().type() == regex::TokenType::LeftCurly);
  93. EXPECT(l.next().type() == regex::TokenType::RightCurly);
  94. EXPECT(l.next().type() == regex::TokenType::LeftParen);
  95. EXPECT(l.next().type() == regex::TokenType::RightParen);
  96. EXPECT(l.next().type() == regex::TokenType::Pipe);
  97. EXPECT(l.next().type() == regex::TokenType::LeftBracket);
  98. EXPECT(l.next().type() == regex::TokenType::EscapeSequence);
  99. EXPECT(l.next().type() == regex::TokenType::EscapeSequence);
  100. EXPECT(l.next().type() == regex::TokenType::RightBracket);
  101. EXPECT(l.next().type() == regex::TokenType::Slash);
  102. EXPECT(l.next().type() == regex::TokenType::Char);
  103. }
  104. TEST_CASE(parser_error_parens)
  105. {
  106. ByteString pattern = "test()test";
  107. Lexer l(pattern);
  108. PosixExtendedParser p(l);
  109. p.parse();
  110. EXPECT(p.has_error());
  111. EXPECT(p.error() == regex::Error::EmptySubExpression);
  112. }
  113. TEST_CASE(parser_error_special_characters_used_at_wrong_place)
  114. {
  115. ByteString pattern;
  116. Vector<char, 5> chars = { '*', '+', '?', '{' };
  117. StringBuilder b;
  118. Lexer l;
  119. PosixExtended p(l);
  120. for (auto& ch : chars) {
  121. // First in ere
  122. b.clear();
  123. b.append(ch);
  124. pattern = b.to_byte_string();
  125. l.set_source(pattern);
  126. p.parse();
  127. EXPECT(p.has_error());
  128. EXPECT(p.error() == regex::Error::InvalidRepetitionMarker);
  129. // After vertical line
  130. b.clear();
  131. b.append("a|"sv);
  132. b.append(ch);
  133. pattern = b.to_byte_string();
  134. l.set_source(pattern);
  135. p.parse();
  136. EXPECT(p.has_error());
  137. EXPECT(p.error() == regex::Error::InvalidRepetitionMarker);
  138. // After circumflex
  139. b.clear();
  140. b.append('^');
  141. b.append(ch);
  142. pattern = b.to_byte_string();
  143. l.set_source(pattern);
  144. p.parse();
  145. EXPECT(p.has_error());
  146. EXPECT(p.error() == regex::Error::InvalidRepetitionMarker);
  147. // After dollar
  148. b.clear();
  149. b.append('$');
  150. b.append(ch);
  151. pattern = b.to_byte_string();
  152. l.set_source(pattern);
  153. p.parse();
  154. EXPECT(p.has_error());
  155. EXPECT(p.error() == regex::Error::InvalidRepetitionMarker);
  156. // After left parens
  157. b.clear();
  158. b.append('(');
  159. b.append(ch);
  160. b.append(')');
  161. pattern = b.to_byte_string();
  162. l.set_source(pattern);
  163. p.parse();
  164. EXPECT(p.has_error());
  165. EXPECT(p.error() == regex::Error::InvalidRepetitionMarker);
  166. }
  167. }
  168. TEST_CASE(parser_error_vertical_line_used_at_wrong_place)
  169. {
  170. Lexer l;
  171. PosixExtended p(l);
  172. // First in ere
  173. l.set_source("|asdf"sv);
  174. p.parse();
  175. EXPECT(p.has_error());
  176. EXPECT(p.error() == regex::Error::EmptySubExpression);
  177. // Last in ere
  178. l.set_source("asdf|"sv);
  179. p.parse();
  180. EXPECT(p.has_error());
  181. EXPECT(p.error() == regex::Error::EmptySubExpression);
  182. // After left parens
  183. l.set_source("(|asdf)"sv);
  184. p.parse();
  185. EXPECT(p.has_error());
  186. EXPECT(p.error() == regex::Error::EmptySubExpression);
  187. // Proceed right parens
  188. l.set_source("(asdf)|"sv);
  189. p.parse();
  190. EXPECT(p.has_error());
  191. EXPECT(p.error() == regex::Error::EmptySubExpression);
  192. }
  193. TEST_CASE(catch_all_first)
  194. {
  195. Regex<PosixExtended> re("^.*$");
  196. RegexResult m;
  197. re.match("Hello World"sv, m);
  198. EXPECT(m.count == 1);
  199. EXPECT(re.match("Hello World"sv, m));
  200. }
  201. TEST_CASE(catch_all)
  202. {
  203. Regex<PosixExtended> re("^.*$", PosixFlags::Global);
  204. EXPECT(re.has_match("Hello World"sv));
  205. EXPECT(re.match("Hello World"sv).success);
  206. EXPECT(re.match("Hello World"sv).count == 1);
  207. EXPECT(has_match("Hello World"sv, re));
  208. auto res = match("Hello World"sv, re);
  209. EXPECT(res.success);
  210. EXPECT(res.count == 1);
  211. EXPECT(res.matches.size() == 1);
  212. EXPECT(res.matches.first().view == "Hello World");
  213. }
  214. TEST_CASE(catch_all_again)
  215. {
  216. Regex<PosixExtended> re("^.*$", PosixFlags::Extra);
  217. EXPECT_EQ(has_match("Hello World"sv, re), true);
  218. }
  219. TEST_CASE(char_utf8)
  220. {
  221. Regex<PosixExtended> re("😀");
  222. RegexResult result;
  223. EXPECT_EQ((result = match(Utf8View { "Привет, мир! 😀 γειά σου κόσμος 😀 こんにちは世界"sv }, re, PosixFlags::Global)).success, true);
  224. EXPECT_EQ(result.count, 2u);
  225. }
  226. TEST_CASE(catch_all_newline)
  227. {
  228. Regex<PosixExtended> re("^.*$", PosixFlags::Multiline | PosixFlags::StringCopyMatches);
  229. RegexResult result;
  230. auto lambda = [&result, &re]() {
  231. ByteString aaa = "Hello World\nTest\n1234\n";
  232. result = match(aaa, re);
  233. EXPECT_EQ(result.success, true);
  234. };
  235. lambda();
  236. EXPECT_EQ(result.count, 3u);
  237. EXPECT_EQ(result.matches.at(0).view, "Hello World");
  238. EXPECT_EQ(result.matches.at(1).view, "Test");
  239. EXPECT_EQ(result.matches.at(2).view, "1234");
  240. }
  241. TEST_CASE(catch_all_newline_view)
  242. {
  243. Regex<PosixExtended> re("^.*$", PosixFlags::Multiline);
  244. RegexResult result;
  245. ByteString aaa = "Hello World\nTest\n1234\n";
  246. result = match(aaa, re);
  247. EXPECT_EQ(result.success, true);
  248. EXPECT_EQ(result.count, 3u);
  249. ByteString str = "Hello World";
  250. EXPECT_EQ(result.matches.at(0).view, str.view());
  251. EXPECT_EQ(result.matches.at(1).view, "Test");
  252. EXPECT_EQ(result.matches.at(2).view, "1234");
  253. }
  254. TEST_CASE(catch_all_newline_2)
  255. {
  256. Regex<PosixExtended> re("^.*$");
  257. RegexResult result;
  258. result = match("Hello World\nTest\n1234\n"sv, re, PosixFlags::Multiline | PosixFlags::StringCopyMatches);
  259. EXPECT_EQ(result.success, true);
  260. EXPECT_EQ(result.count, 3u);
  261. EXPECT_EQ(result.matches.at(0).view, "Hello World");
  262. EXPECT_EQ(result.matches.at(1).view, "Test");
  263. EXPECT_EQ(result.matches.at(2).view, "1234");
  264. result = match("Hello World\nTest\n1234\n"sv, re);
  265. EXPECT_EQ(result.success, true);
  266. EXPECT_EQ(result.count, 1u);
  267. EXPECT_EQ(result.matches.at(0).view, "Hello World\nTest\n1234\n");
  268. }
  269. TEST_CASE(match_all_character_class)
  270. {
  271. Regex<PosixExtended> re("[[:alpha:]]");
  272. ByteString str = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  273. RegexResult result = match(str, re, PosixFlags::Global | PosixFlags::StringCopyMatches);
  274. EXPECT_EQ(result.success, true);
  275. EXPECT_EQ(result.count, 24u);
  276. EXPECT_EQ(result.matches.at(0).view, "W");
  277. EXPECT_EQ(result.matches.at(1).view, "i");
  278. EXPECT_EQ(result.matches.at(2).view, "n");
  279. }
  280. TEST_CASE(match_character_class_with_assertion)
  281. {
  282. Regex<PosixExtended> re("[[:alpha:]]+$");
  283. ByteString str = "abcdef";
  284. RegexResult result = match(str, re);
  285. EXPECT_EQ(result.success, true);
  286. EXPECT_EQ(result.count, 1u);
  287. }
  288. TEST_CASE(example_for_git_commit)
  289. {
  290. Regex<PosixExtended> re("^.*$");
  291. auto result = re.match("Well, hello friends!\nHello World!"sv);
  292. EXPECT(result.success);
  293. EXPECT(result.count == 1);
  294. EXPECT(result.matches.at(0).view.starts_with("Well"sv));
  295. EXPECT(result.matches.at(0).view.length() == 33);
  296. EXPECT(re.has_match("Well,...."sv));
  297. result = re.match("Well, hello friends!\nHello World!"sv, PosixFlags::Multiline);
  298. EXPECT(result.success);
  299. EXPECT(result.count == 2);
  300. EXPECT(result.matches.at(0).view == "Well, hello friends!");
  301. EXPECT(result.matches.at(1).view == "Hello World!");
  302. }
  303. TEST_CASE(email_address)
  304. {
  305. Regex<PosixExtended> re("^[A-Z0-9a-z._%+-]{1,64}@([A-Za-z0-9-]{1,63}\\.){1,125}[A-Za-z]{2,63}$");
  306. EXPECT(re.has_match("hello.world@domain.tld"sv));
  307. EXPECT(re.has_match("this.is.a.very_long_email_address@world.wide.web"sv));
  308. }
  309. TEST_CASE(ini_file_entries)
  310. {
  311. Regex<PosixExtended> re("[[:alpha:]]*=([[:digit:]]*)|\\[(.*)\\]");
  312. RegexResult result;
  313. if constexpr (REGEX_DEBUG) {
  314. RegexDebug regex_dbg(stderr);
  315. regex_dbg.print_raw_bytecode(re);
  316. regex_dbg.print_header();
  317. regex_dbg.print_bytecode(re);
  318. }
  319. ByteString haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  320. EXPECT_EQ(re.search(haystack.view(), result, PosixFlags::Multiline), true);
  321. EXPECT_EQ(result.count, 3u);
  322. if constexpr (REGEX_DEBUG) {
  323. for (auto& v : result.matches)
  324. fprintf(stderr, "%s\n", v.view.to_byte_string().characters());
  325. }
  326. EXPECT_EQ(result.matches.at(0).view, "[Window]");
  327. EXPECT_EQ(result.capture_group_matches.at(0).at(0).view, "Window");
  328. EXPECT_EQ(result.matches.at(1).view, "Opacity=255");
  329. EXPECT_EQ(result.matches.at(1).line, 1u);
  330. EXPECT_EQ(result.matches.at(1).column, 0u);
  331. EXPECT_EQ(result.capture_group_matches.at(1).at(0).view, "255");
  332. EXPECT_EQ(result.capture_group_matches.at(1).at(0).line, 1u);
  333. EXPECT_EQ(result.capture_group_matches.at(1).at(0).column, 8u);
  334. EXPECT_EQ(result.matches.at(2).view, "AudibleBeep=0");
  335. EXPECT_EQ(result.capture_group_matches.at(2).at(0).view, "0");
  336. EXPECT_EQ(result.capture_group_matches.at(2).at(0).line, 2u);
  337. EXPECT_EQ(result.capture_group_matches.at(2).at(0).column, 12u);
  338. }
  339. TEST_CASE(ini_file_entries2)
  340. {
  341. Regex<PosixExtended> re("[[:alpha:]]*=([[:digit:]]*)");
  342. RegexResult result;
  343. ByteString haystack = "ViewMode=Icon";
  344. EXPECT_EQ(re.match(haystack.view(), result), false);
  345. EXPECT_EQ(result.count, 0u);
  346. EXPECT_EQ(re.search(haystack.view(), result), true);
  347. EXPECT_EQ(result.count, 1u);
  348. }
  349. TEST_CASE(named_capture_group)
  350. {
  351. Regex<PosixExtended> re("[[:alpha:]]*=(?<Test>[[:digit:]]*)");
  352. RegexResult result;
  353. if constexpr (REGEX_DEBUG) {
  354. RegexDebug regex_dbg(stderr);
  355. regex_dbg.print_raw_bytecode(re);
  356. regex_dbg.print_header();
  357. regex_dbg.print_bytecode(re);
  358. }
  359. ByteString haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  360. EXPECT_EQ(re.search(haystack, result, PosixFlags::Multiline), true);
  361. EXPECT_EQ(result.count, 2u);
  362. EXPECT_EQ(result.matches.at(0).view, "Opacity=255");
  363. EXPECT_EQ(result.capture_group_matches.at(0).at(0).view, "255");
  364. EXPECT_EQ(result.capture_group_matches.at(0).at(0).capture_group_name, "Test");
  365. EXPECT_EQ(result.matches.at(1).view, "AudibleBeep=0");
  366. EXPECT_EQ(result.capture_group_matches.at(1).at(0).view, "0");
  367. EXPECT_EQ(result.capture_group_matches.at(1).at(0).capture_group_name, "Test");
  368. }
  369. TEST_CASE(ecma262_named_capture_group_with_dollar_sign)
  370. {
  371. Regex<ECMA262> re("[a-zA-Z]*=(?<$Test$>[0-9]*)");
  372. RegexResult result;
  373. if constexpr (REGEX_DEBUG) {
  374. RegexDebug regex_dbg(stderr);
  375. regex_dbg.print_raw_bytecode(re);
  376. regex_dbg.print_header();
  377. regex_dbg.print_bytecode(re);
  378. }
  379. ByteString haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  380. EXPECT_EQ(re.search(haystack, result, ECMAScriptFlags::Multiline), true);
  381. EXPECT_EQ(result.count, 2u);
  382. EXPECT_EQ(result.matches.at(0).view, "Opacity=255");
  383. EXPECT_EQ(result.capture_group_matches.at(0).at(0).view, "255");
  384. EXPECT_EQ(result.capture_group_matches.at(0).at(0).capture_group_name, "$Test$");
  385. EXPECT_EQ(result.matches.at(1).view, "AudibleBeep=0");
  386. EXPECT_EQ(result.capture_group_matches.at(1).at(0).view, "0");
  387. EXPECT_EQ(result.capture_group_matches.at(1).at(0).capture_group_name, "$Test$");
  388. }
  389. TEST_CASE(a_star)
  390. {
  391. Regex<PosixExtended> re("a*");
  392. RegexResult result;
  393. if constexpr (REGEX_DEBUG) {
  394. RegexDebug regex_dbg(stderr);
  395. regex_dbg.print_raw_bytecode(re);
  396. regex_dbg.print_header();
  397. regex_dbg.print_bytecode(re);
  398. }
  399. ByteString haystack = "[Window]\nOpacity=255\nAudibleBeep=0\n";
  400. EXPECT_EQ(re.search(haystack.view(), result, PosixFlags::Multiline), true);
  401. EXPECT_EQ(result.count, 32u);
  402. if (result.count == 32u) {
  403. EXPECT_EQ(result.matches.at(0).view.length(), 0u);
  404. EXPECT_EQ(result.matches.at(10).view.length(), 1u);
  405. EXPECT_EQ(result.matches.at(10).view, "a");
  406. EXPECT_EQ(result.matches.at(31).view.length(), 0u);
  407. }
  408. }
  409. TEST_CASE(simple_period_end_benchmark)
  410. {
  411. Regex<PosixExtended> re("hello.$");
  412. RegexResult m;
  413. EXPECT_EQ(re.search("Hello1"sv, m), false);
  414. EXPECT_EQ(re.search("hello1hello1"sv, m), true);
  415. EXPECT_EQ(re.search("hello2hell"sv, m), false);
  416. EXPECT_EQ(re.search("hello?"sv, m), true);
  417. }
  418. TEST_CASE(posix_extended_nested_capture_group)
  419. {
  420. Regex<PosixExtended> re("(h(e(?<llo>llo)))"); // group 0 -> "hello", group 1 -> "ello", group 2/"llo" -> "llo"
  421. auto result = re.match("hello"sv);
  422. EXPECT(result.success);
  423. EXPECT_EQ(result.capture_group_matches.size(), 1u);
  424. EXPECT_EQ(result.capture_group_matches[0].size(), 3u);
  425. EXPECT_EQ(result.capture_group_matches[0][0].view, "hello"sv);
  426. EXPECT_EQ(result.capture_group_matches[0][1].view, "ello"sv);
  427. EXPECT_EQ(result.capture_group_matches[0][2].view, "llo"sv);
  428. }
  429. auto parse_test_case_long_disjunction_chain = ByteString::repeated("a|"sv, 100000);
  430. TEST_CASE(ECMA262_parse)
  431. {
  432. struct _test {
  433. StringView pattern;
  434. regex::Error expected_error { regex::Error::NoError };
  435. regex::ECMAScriptFlags flags {};
  436. };
  437. _test const tests[] {
  438. { "^hello.$"sv },
  439. { "^(hello.)$"sv },
  440. { "^h{0,1}ello.$"sv },
  441. { "^hello\\W$"sv },
  442. { "^hell\\w.$"sv },
  443. { "^hell\\x6f1$"sv }, // ^hello1$
  444. { "^hel(?:l\\w).$"sv },
  445. { "^hel(?<LO>l\\w).$"sv },
  446. { "^[-a-zA-Z\\w\\s]+$"sv },
  447. { "\\bhello\\B"sv },
  448. { "^[\\w+/_-]+[=]{0,2}$"sv }, // #4189
  449. { "^(?:[^<]*(<[\\w\\W]+>)[^>]*$|#([\\w\\-]*)$)"sv }, // #4189
  450. { "\\/"sv }, // #4189
  451. { ",/=-:"sv }, // #4243
  452. { "\\x"sv }, // Even invalid escapes are allowed if ~unicode.
  453. { "\\x1"sv }, // Even invalid escapes are allowed if ~unicode.
  454. { "\\x1"sv, regex::Error::InvalidPattern, regex::ECMAScriptFlags::Unicode },
  455. { "\\x11"sv },
  456. { "\\x11"sv, regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
  457. { "\\"sv, regex::Error::InvalidTrailingEscape },
  458. { "(?"sv, regex::Error::InvalidCaptureGroup },
  459. { "\\u1234"sv, regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
  460. { "[\\u1234]"sv, regex::Error::NoError, regex::ECMAScriptFlags::Unicode },
  461. { "\\u1"sv, regex::Error::InvalidPattern, regex::ECMAScriptFlags::Unicode },
  462. { "[\\u1]"sv, regex::Error::InvalidPattern, regex::ECMAScriptFlags::Unicode },
  463. { ",(?"sv, regex::Error::InvalidCaptureGroup }, // #4583
  464. { "{1}"sv, regex::Error::InvalidPattern },
  465. { "{1,2}"sv, regex::Error::InvalidPattern },
  466. { "\\uxxxx"sv, regex::Error::NoError },
  467. { "\\uxxxx"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  468. { "\\ud83d"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  469. { "\\ud83d\\uxxxx"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  470. { "\\u{0}"sv },
  471. { "\\u{0}"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  472. { "\\u{10ffff}"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  473. { "\\u{10ffff"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  474. { "\\u{10ffffx"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  475. { "\\u{110000}"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  476. { "\\p"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  477. { "\\p{"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  478. { "\\p{}"sv, regex::Error::InvalidNameForProperty, ECMAScriptFlags::Unicode },
  479. { "\\p{AsCiI}"sv, regex::Error::InvalidNameForProperty, ECMAScriptFlags::Unicode },
  480. { "\\p{hello friends}"sv, regex::Error::InvalidNameForProperty, ECMAScriptFlags::Unicode },
  481. { "\\p{Prepended_Concatenation_Mark}"sv, regex::Error::InvalidNameForProperty, ECMAScriptFlags::Unicode },
  482. { "\\p{ASCII}"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  483. { "\\\\p{1}"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  484. { "\\\\p{AsCiI}"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  485. { "\\\\p{ASCII}"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  486. { "\\c"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  487. { "\\c"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  488. { "[\\c]"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  489. { "[\\c]"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  490. { "\\c`"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  491. { "\\c`"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  492. { "[\\c`]"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  493. { "[\\c`]"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  494. { "\\A"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  495. { "\\A"sv, regex::Error::InvalidCharacterClass, ECMAScriptFlags::Unicode },
  496. { "[\\A]"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  497. { "[\\A]"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  498. { "\\0"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  499. { "\\0"sv, regex::Error::NoError, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) },
  500. { "\\00"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  501. { "\\00"sv, regex::Error::InvalidCharacterClass, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) },
  502. { "[\\0]"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  503. { "[\\0]"sv, regex::Error::NoError, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) },
  504. { "[\\00]"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  505. { "[\\00]"sv, regex::Error::InvalidPattern, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) },
  506. { "\\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\/"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  507. { "[\\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\/]"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  508. { "]"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  509. { "]"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  510. { "\\]"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  511. { "}"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended },
  512. { "}"sv, regex::Error::InvalidPattern, ECMAScriptFlags::Unicode },
  513. { "\\}"sv, regex::Error::NoError, ECMAScriptFlags::Unicode },
  514. { "a{9007199254740991}"sv }, // 2^53 - 1
  515. { "a{9007199254740991,}"sv },
  516. { "a{9007199254740991,9007199254740991}"sv },
  517. { "a{9007199254740992}"sv, regex::Error::InvalidBraceContent },
  518. { "a{9007199254740992,}"sv, regex::Error::InvalidBraceContent },
  519. { "a{9007199254740991,9007199254740992}"sv, regex::Error::InvalidBraceContent },
  520. { "a{9007199254740992,9007199254740991}"sv, regex::Error::InvalidBraceContent },
  521. { "a{9007199254740992,9007199254740992}"sv, regex::Error::InvalidBraceContent },
  522. { "(?<a>a)(?<a>b)"sv, regex::Error::DuplicateNamedCapture },
  523. { "(?<a>a)(?<b>b)(?<a>c)"sv, regex::Error::DuplicateNamedCapture },
  524. { "(?<1a>a)"sv, regex::Error::InvalidNameForCaptureGroup },
  525. { "(?<\\a>a)"sv, regex::Error::InvalidNameForCaptureGroup },
  526. { "(?<\ta>a)"sv, regex::Error::InvalidNameForCaptureGroup },
  527. { "(?<$$_$$>a)"sv },
  528. { "(?<ÿ>a)"sv },
  529. { "(?<𝓑𝓻𝓸𝔀𝓷>a)"sv },
  530. { "((?=lg)?[vl]k\\-?\\d{3}) bui| 3\\.[-\\w; ]{10}lg?-([06cv9]{3,4})"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended }, // #12373, quantifiable assertions.
  531. { parse_test_case_long_disjunction_chain.view() }, // A whole lot of disjunctions, should not overflow the stack.
  532. { "(\"|')(?:(?!\\2)[^\\\\\\r\\n]|\\\\.)*\\2"sv, regex::Error::NoError, ECMAScriptFlags::BrowserExtended }, // LegacyOctalEscapeSequence should not consume too many chars (and should not crash)
  533. // #18324, Capture group counter skipped past EOF.
  534. { "\\1[\\"sv, regex::Error::InvalidNumber },
  535. };
  536. for (auto& test : tests) {
  537. Regex<ECMA262> re(test.pattern, test.flags);
  538. EXPECT_EQ(re.parser_result.error, test.expected_error);
  539. if constexpr (REGEX_DEBUG) {
  540. dbgln("\n");
  541. RegexDebug regex_dbg(stderr);
  542. regex_dbg.print_raw_bytecode(re);
  543. regex_dbg.print_header();
  544. regex_dbg.print_bytecode(re);
  545. dbgln("\n");
  546. }
  547. }
  548. }
  549. TEST_CASE(ECMA262_match)
  550. {
  551. constexpr auto global_multiline = ECMAScriptFlags::Global | ECMAScriptFlags::Multiline;
  552. struct _test {
  553. StringView pattern;
  554. StringView subject;
  555. bool matches { true };
  556. ECMAScriptFlags options {};
  557. };
  558. // clang-format off
  559. constexpr _test tests[] {
  560. { "^hello.$"sv, "hello1"sv },
  561. { "^(hello.)$"sv, "hello1"sv },
  562. { "^h{0,1}ello.$"sv, "ello1"sv },
  563. { "^hello\\W$"sv, "hello!"sv },
  564. { "^hell\\w.$"sv, "hellx!"sv },
  565. { "^hell\\x6f1$"sv, "hello1"sv },
  566. { "^hel(?<LO>l.)1$"sv, "hello1"sv },
  567. { "^hel(?<LO>l.)1*\\k<LO>.$"sv, "hello1lo1"sv },
  568. { "^[-a-z1-3\\s]+$"sv, "hell2 o1"sv },
  569. { "^[\\0-\\x1f]$"sv, "\n"sv },
  570. { .pattern = "\\bhello\\B"sv, .subject = "hello1"sv, .options = ECMAScriptFlags::Global },
  571. { "\\b.*\\b"sv, "hello1"sv },
  572. { "[^\\D\\S]{2}"sv, "1 "sv, false },
  573. { "bar(?=f.)foo"sv, "barfoo"sv },
  574. { "bar(?=foo)bar"sv, "barbar"sv, false },
  575. { "bar(?!foo)bar"sv, "barbar"sv, true },
  576. { "bar(?!bar)bar"sv, "barbar"sv, false },
  577. { "bar.*(?<=foo)"sv, "barbar"sv, false },
  578. { "bar.*(?<!foo)"sv, "barbar"sv, true },
  579. { "((...)X)+"sv, "fooXbarXbazX"sv, true },
  580. { "(?:)"sv, ""sv, true },
  581. { "\\^"sv, "^"sv },
  582. { "\\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\/"sv, "^$\\.*+?()[]{}|/"sv, true, ECMAScriptFlags::Unicode },
  583. { "[\\^\\$\\\\\\.\\*\\+\\?\\(\\)\\[\\]\\{\\}\\|\\/]{15}"sv, "^$\\.*+?()[]{}|/"sv, true, ECMAScriptFlags::Unicode },
  584. { "(a{2}){3}"sv, "aaaaaa"sv },
  585. { "(a{2}){3}"sv, "aaaabaa"sv, false },
  586. { "(a{2}){4}"sv, "aaaaaaaa"sv },
  587. { "(a{2}){4}"sv, "aaaaaabaa"sv, false },
  588. { "(a{3}){2}"sv, "aaaaaa"sv },
  589. { "(a{3}){2}"sv, "aaaabaa"sv, false },
  590. { "(a{4}){2}"sv, "aaaaaaaa"sv },
  591. { "(a{4}){2}"sv, "aaaaaabaa"sv, false },
  592. { "\\u{4}"sv, "uuuu"sv },
  593. { "(?<=.{3})f"sv, "abcdef"sv, true, (ECMAScriptFlags)regex::AllFlags::Global },
  594. { "(?<=.{3})f"sv, "abc😀ef"sv, true, (ECMAScriptFlags)regex::AllFlags::Global },
  595. // ECMA262, B.1.4. Regular Expression Pattern extensions for browsers
  596. { "{"sv, "{"sv, true, ECMAScriptFlags::BrowserExtended },
  597. { "\\5"sv, "\5"sv, true, ECMAScriptFlags::BrowserExtended },
  598. { "\\05"sv, "\5"sv, true, ECMAScriptFlags::BrowserExtended },
  599. { "\\455"sv, "\45""5"sv, true, ECMAScriptFlags::BrowserExtended },
  600. { "\\314"sv, "\314"sv, true, ECMAScriptFlags::BrowserExtended },
  601. { "\\c"sv, "\\c"sv, true, ECMAScriptFlags::BrowserExtended },
  602. { "\\cf"sv, "\06"sv, true, ECMAScriptFlags::BrowserExtended },
  603. { "\\c1"sv, "\\c1"sv, true, ECMAScriptFlags::BrowserExtended },
  604. { "[\\c1]"sv, "\x11"sv, true, ECMAScriptFlags::BrowserExtended },
  605. { "[\\w-\\d]"sv, "-"sv, true, ECMAScriptFlags::BrowserExtended },
  606. { "^(?:^^\\.?|[!+-]|!=|!==|#|%|%=|&|&&|&&=|&=|\\(|\\*|\\*=|\\+=|,|-=|->|\\/|\\/=|:|::|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|[?@[^]|\\^=|\\^\\^|\\^\\^=|{|\\||\\|=|\\|\\||\\|\\|=|~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*(\\/(?=[^*/])(?:[^/[\\\\]|\\\\[\\S\\s]|\\[(?:[^\\\\\\]]|\\\\[\\S\\s])*(?:]|$))+\\/)"sv,
  607. "return /xx/"sv, true, ECMAScriptFlags::BrowserExtended
  608. }, // #5517, appears to be matching JS expressions that involve regular expressions...
  609. { "a{2,}"sv, "aaaa"sv }, // #5518
  610. { "\\0"sv, "\0"sv, true, ECMAScriptFlags::BrowserExtended },
  611. { "\\0"sv, "\0"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) },
  612. { "\\01"sv, "\1"sv, true, ECMAScriptFlags::BrowserExtended },
  613. { "[\\0]"sv, "\0"sv, true, ECMAScriptFlags::BrowserExtended },
  614. { "[\\0]"sv, "\0"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::BrowserExtended) },
  615. { "[\\01]"sv, "\1"sv, true, ECMAScriptFlags::BrowserExtended },
  616. { "(\0|a)"sv, "a"sv, true }, // #9686, Should allow null bytes in pattern
  617. { "(.*?)a(?!(a+)b\\2c)\\2(.*)"sv, "baaabaac"sv, true }, // #6042, Groups inside lookarounds may be referenced outside, but their contents appear empty if the pattern in the lookaround fails.
  618. { "a|$"sv, "x"sv, true, (ECMAScriptFlags)regex::AllFlags::Global }, // #11940, Global (not the 'g' flag) regexps should attempt to match the zero-length end of the string too.
  619. { "foo\nbar"sv, "foo\nbar"sv, true }, // #12126, ECMA262 regexp should match literal newlines without the 's' flag.
  620. { "foo[^]bar"sv, "foo\nbar"sv, true }, // #12126, ECMA262 regexp should match newline with [^].
  621. { "^[_A-Z]+$"sv, "_aA"sv, true, ECMAScriptFlags::Insensitive }, // Insensitive lookup table: characters in a range do not necessarily lie in the same range after being converted to lowercase.
  622. { "^[a-sy-z]$"sv, "b"sv, true, ECMAScriptFlags::Insensitive },
  623. { "^[a-sy-z]$"sv, "y"sv, true, ECMAScriptFlags::Insensitive },
  624. { "^[a-sy-z]$"sv, "u"sv, false, ECMAScriptFlags::Insensitive },
  625. { "."sv, "\n\r\u2028\u2029"sv, false }, // Dot should not match any of CR/LF/LS/PS in ECMA262 mode without DotAll.
  626. { "a$"sv, "a\r\n"sv, true, global_multiline.value() }, // $ should accept all LineTerminators in ECMA262 mode with Multiline.
  627. { "^a"sv, "\ra"sv, true, global_multiline.value() },
  628. { "^(.*?):[ \\t]*([^\\r\\n]*)$"sv, "content-length: 488\r\ncontent-type: application/json; charset=utf-8\r\n"sv, true, global_multiline.value() },
  629. { "^\\?((&?category=[0-9]+)?(&?shippable=1)?(&?ad_type=demand)?(&?page=[0-9]+)?(&?locations=(r|d)_[0-9]+)?)+$"sv,
  630. "?category=54&shippable=1&baby_age=p,0,1,3"sv, false }, // ladybird#968, ?+ should not loop forever.
  631. };
  632. // clang-format on
  633. for (auto& test : tests) {
  634. Regex<ECMA262> re(test.pattern, test.options);
  635. if constexpr (REGEX_DEBUG) {
  636. dbgln("\n");
  637. RegexDebug regex_dbg(stderr);
  638. regex_dbg.print_raw_bytecode(re);
  639. regex_dbg.print_header();
  640. regex_dbg.print_bytecode(re);
  641. dbgln("\n");
  642. }
  643. EXPECT_EQ(re.parser_result.error, regex::Error::NoError);
  644. EXPECT_EQ(re.match(test.subject).success, test.matches);
  645. }
  646. }
  647. TEST_CASE(ECMA262_unicode_match)
  648. {
  649. constexpr auto space_and_line_terminator_code_points = Array { 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x0020, 0x00A0, 0x1680, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000, 0xFEFF };
  650. StringBuilder builder;
  651. for (u32 code_point : space_and_line_terminator_code_points)
  652. builder.append_code_point(code_point);
  653. auto space_and_line_terminators = builder.to_byte_string();
  654. struct _test {
  655. StringView pattern;
  656. StringView subject;
  657. bool matches { true };
  658. ECMAScriptFlags options {};
  659. };
  660. _test tests[] {
  661. { "\xf0\x9d\x8c\x86"sv, "abcdef"sv, false, ECMAScriptFlags::Unicode },
  662. { "[\xf0\x9d\x8c\x86]"sv, "abcdef"sv, false, ECMAScriptFlags::Unicode },
  663. { "\\ud83d"sv, "😀"sv, true },
  664. { "\\ud83d"sv, "😀"sv, false, ECMAScriptFlags::Unicode },
  665. { "\\ude00"sv, "😀"sv, true },
  666. { "\\ude00"sv, "😀"sv, false, ECMAScriptFlags::Unicode },
  667. { "\\ud83d\\ude00"sv, "😀"sv, true },
  668. { "\\ud83d\\ude00"sv, "😀"sv, true, ECMAScriptFlags::Unicode },
  669. { "\\u{1f600}"sv, "😀"sv, true, ECMAScriptFlags::Unicode },
  670. { "\\ud83d\\ud83d"sv, "\xed\xa0\xbd\xed\xa0\xbd"sv, true },
  671. { "\\ud83d\\ud83d"sv, "\xed\xa0\xbd\xed\xa0\xbd"sv, true, ECMAScriptFlags::Unicode },
  672. { "(?<=.{3})f"sv, "abcdef"sv, true, ECMAScriptFlags::Unicode },
  673. { "(?<=.{3})f"sv, "abc😀ef"sv, true, ECMAScriptFlags::Unicode },
  674. { "(?<𝓑𝓻𝓸𝔀𝓷>brown)"sv, "brown"sv, true, ECMAScriptFlags::Unicode },
  675. { "(?<\\u{1d4d1}\\u{1d4fb}\\u{1d4f8}\\u{1d500}\\u{1d4f7}>brown)"sv, "brown"sv, true, ECMAScriptFlags::Unicode },
  676. { "(?<\\ud835\\udcd1\\ud835\\udcfb\\ud835\\udcf8\\ud835\\udd00\\ud835\\udcf7>brown)"sv, "brown"sv, true, ECMAScriptFlags::Unicode },
  677. { "^\\s+$"sv, space_and_line_terminators },
  678. { "^\\s+$"sv, space_and_line_terminators, true, ECMAScriptFlags::Unicode },
  679. { "[\\u0390]"sv, "\u1fd3"sv, false, ECMAScriptFlags::Unicode },
  680. { "[\\u1fd3]"sv, "\u0390"sv, false, ECMAScriptFlags::Unicode },
  681. { "[\\u0390]"sv, "\u1fd3"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::Insensitive) },
  682. { "[\\u1fd3]"sv, "\u0390"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::Insensitive) },
  683. { "[\\u03b0]"sv, "\u1fe3"sv, false, ECMAScriptFlags::Unicode },
  684. { "[\\u1fe3]"sv, "\u03b0"sv, false, ECMAScriptFlags::Unicode },
  685. { "[\\u03b0]"sv, "\u1fe3"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::Insensitive) },
  686. { "[\\u1fe3]"sv, "\u03b0"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::Insensitive) },
  687. { "[\\ufb05]"sv, "\ufb06"sv, false, ECMAScriptFlags::Unicode },
  688. { "[\\ufb06]"sv, "\ufb05"sv, false, ECMAScriptFlags::Unicode },
  689. { "[\\ufb05]"sv, "\ufb06"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::Insensitive) },
  690. { "[\\ufb06]"sv, "\ufb05"sv, true, combine_flags(ECMAScriptFlags::Unicode, ECMAScriptFlags::Insensitive) },
  691. };
  692. for (auto& test : tests) {
  693. Regex<ECMA262> re(test.pattern, (ECMAScriptFlags)regex::AllFlags::Global | test.options);
  694. auto subject = MUST(AK::utf8_to_utf16(test.subject));
  695. Utf16View view { subject };
  696. if constexpr (REGEX_DEBUG) {
  697. dbgln("\n");
  698. RegexDebug regex_dbg(stderr);
  699. regex_dbg.print_raw_bytecode(re);
  700. regex_dbg.print_header();
  701. regex_dbg.print_bytecode(re);
  702. dbgln("\n");
  703. }
  704. EXPECT_EQ(re.parser_result.error, regex::Error::NoError);
  705. EXPECT_EQ(re.match(view).success, test.matches);
  706. }
  707. }
  708. TEST_CASE(ECMA262_unicode_sets_parser_error)
  709. {
  710. struct _test {
  711. StringView pattern;
  712. regex::Error error;
  713. };
  714. constexpr _test tests[] {
  715. { "[[]"sv, regex::Error::InvalidPattern },
  716. { "[[x[]]]"sv, regex::Error::NoError }, // #23691, should not crash on empty charclass within AndOr.
  717. };
  718. for (auto test : tests) {
  719. Regex<ECMA262> re(test.pattern, (ECMAScriptFlags)regex::AllFlags::UnicodeSets);
  720. EXPECT_EQ(re.parser_result.error, test.error);
  721. }
  722. }
  723. TEST_CASE(ECMA262_unicode_sets_match)
  724. {
  725. struct _test {
  726. StringView pattern;
  727. StringView subject;
  728. bool matches { true };
  729. ECMAScriptFlags options {};
  730. };
  731. constexpr _test tests[] {
  732. { "[\\w--x]"sv, "x"sv, false },
  733. { "[\\w&&x]"sv, "y"sv, false },
  734. { "[\\w--x]"sv, "y"sv, true },
  735. { "[\\w&&x]"sv, "x"sv, true },
  736. { "[[0-9\\w]--x--6]"sv, "6"sv, false },
  737. { "[[0-9\\w]--x--6]"sv, "x"sv, false },
  738. { "[[0-9\\w]--x--6]"sv, "y"sv, true },
  739. { "[[0-9\\w]--x--6]"sv, "9"sv, true },
  740. { "[\\w&&\\d]"sv, "a"sv, false },
  741. { "[\\w&&\\d]"sv, "4"sv, true },
  742. };
  743. for (auto& test : tests) {
  744. Regex<ECMA262> re(test.pattern, (ECMAScriptFlags)regex::AllFlags::UnicodeSets | test.options);
  745. if constexpr (REGEX_DEBUG) {
  746. dbgln("\n");
  747. RegexDebug regex_dbg(stderr);
  748. regex_dbg.print_raw_bytecode(re);
  749. regex_dbg.print_header();
  750. regex_dbg.print_bytecode(re);
  751. dbgln("\n");
  752. }
  753. EXPECT_EQ(re.parser_result.error, regex::Error::NoError);
  754. auto result = re.match(test.subject).success;
  755. EXPECT_EQ(result, test.matches);
  756. }
  757. }
  758. TEST_CASE(ECMA262_property_match)
  759. {
  760. struct _test {
  761. StringView pattern;
  762. StringView subject;
  763. bool matches { true };
  764. ECMAScriptFlags options {};
  765. };
  766. constexpr _test tests[] {
  767. { "\\p{ASCII}"sv, "a"sv, false },
  768. { "\\p{ASCII}"sv, "p{ASCII}"sv, true },
  769. { "\\p{ASCII}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  770. { "\\p{ASCII}"sv, "😀"sv, false, ECMAScriptFlags::Unicode },
  771. { "\\P{ASCII}"sv, "a"sv, false, ECMAScriptFlags::Unicode },
  772. { "\\P{ASCII}"sv, "😀"sv, true, ECMAScriptFlags::Unicode },
  773. { "\\p{ASCII_Hex_Digit}"sv, "1"sv, true, ECMAScriptFlags::Unicode },
  774. { "\\p{ASCII_Hex_Digit}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  775. { "\\p{ASCII_Hex_Digit}"sv, "x"sv, false, ECMAScriptFlags::Unicode },
  776. { "\\P{ASCII_Hex_Digit}"sv, "1"sv, false, ECMAScriptFlags::Unicode },
  777. { "\\P{ASCII_Hex_Digit}"sv, "a"sv, false, ECMAScriptFlags::Unicode },
  778. { "\\P{ASCII_Hex_Digit}"sv, "x"sv, true, ECMAScriptFlags::Unicode },
  779. { "\\p{Any}"sv, "\xcd\xb8"sv, true, ECMAScriptFlags::Unicode }, // U+0378, which is an unassigned code point.
  780. { "\\P{Any}"sv, "\xcd\xb8"sv, false, ECMAScriptFlags::Unicode }, // U+0378, which is an unassigned code point.
  781. { "\\p{Assigned}"sv, "\xcd\xb8"sv, false, ECMAScriptFlags::Unicode }, // U+0378, which is an unassigned code point.
  782. { "\\P{Assigned}"sv, "\xcd\xb8"sv, true, ECMAScriptFlags::Unicode }, // U+0378, which is an unassigned code point.
  783. { "\\p{Lu}"sv, "a"sv, false, ECMAScriptFlags::Unicode },
  784. { "\\p{Lu}"sv, "A"sv, true, ECMAScriptFlags::Unicode },
  785. { "\\p{Lu}"sv, "9"sv, false, ECMAScriptFlags::Unicode },
  786. { "\\p{Cased_Letter}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  787. { "\\p{Cased_Letter}"sv, "A"sv, true, ECMAScriptFlags::Unicode },
  788. { "\\p{Cased_Letter}"sv, "9"sv, false, ECMAScriptFlags::Unicode },
  789. { "\\P{Cased_Letter}"sv, "a"sv, false, ECMAScriptFlags::Unicode },
  790. { "\\P{Cased_Letter}"sv, "A"sv, false, ECMAScriptFlags::Unicode },
  791. { "\\P{Cased_Letter}"sv, "9"sv, true, ECMAScriptFlags::Unicode },
  792. { "\\p{General_Category=Cased_Letter}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  793. { "\\p{General_Category=Cased_Letter}"sv, "A"sv, true, ECMAScriptFlags::Unicode },
  794. { "\\p{General_Category=Cased_Letter}"sv, "9"sv, false, ECMAScriptFlags::Unicode },
  795. { "\\p{gc=Cased_Letter}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  796. { "\\p{gc=Cased_Letter}"sv, "A"sv, true, ECMAScriptFlags::Unicode },
  797. { "\\p{gc=Cased_Letter}"sv, "9"sv, false, ECMAScriptFlags::Unicode },
  798. { "\\p{Script=Latin}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  799. { "\\p{Script=Latin}"sv, "A"sv, true, ECMAScriptFlags::Unicode },
  800. { "\\p{Script=Latin}"sv, "9"sv, false, ECMAScriptFlags::Unicode },
  801. { "\\p{sc=Latin}"sv, "a"sv, true, ECMAScriptFlags::Unicode },
  802. { "\\p{sc=Latin}"sv, "A"sv, true, ECMAScriptFlags::Unicode },
  803. { "\\p{sc=Latin}"sv, "9"sv, false, ECMAScriptFlags::Unicode },
  804. { "\\p{Script_Extensions=Deva}"sv, "a"sv, false, ECMAScriptFlags::Unicode },
  805. { "\\p{Script_Extensions=Beng}"sv, "\xe1\xb3\x95"sv, true, ECMAScriptFlags::Unicode }, // U+01CD5
  806. { "\\p{Script_Extensions=Deva}"sv, "\xe1\xb3\x95"sv, true, ECMAScriptFlags::Unicode }, // U+01CD5
  807. { "\\p{scx=Deva}"sv, "a"sv, false, ECMAScriptFlags::Unicode },
  808. { "\\p{scx=Beng}"sv, "\xe1\xb3\x95"sv, true, ECMAScriptFlags::Unicode }, // U+01CD5
  809. { "\\p{scx=Deva}"sv, "\xe1\xb3\x95"sv, true, ECMAScriptFlags::Unicode }, // U+01CD5
  810. };
  811. for (auto& test : tests) {
  812. Regex<ECMA262> re(test.pattern, (ECMAScriptFlags)regex::AllFlags::Global | regex::ECMAScriptFlags::BrowserExtended | test.options);
  813. auto subject = MUST(AK::utf8_to_utf16(test.subject));
  814. Utf16View view { subject };
  815. if constexpr (REGEX_DEBUG) {
  816. dbgln("\n");
  817. RegexDebug regex_dbg(stderr);
  818. regex_dbg.print_raw_bytecode(re);
  819. regex_dbg.print_header();
  820. regex_dbg.print_bytecode(re);
  821. dbgln("\n");
  822. }
  823. EXPECT_EQ(re.parser_result.error, regex::Error::NoError);
  824. EXPECT_EQ(re.match(view).success, test.matches);
  825. }
  826. }
  827. TEST_CASE(replace)
  828. {
  829. struct _test {
  830. StringView pattern;
  831. StringView replacement;
  832. StringView subject;
  833. StringView expected;
  834. ECMAScriptFlags options {};
  835. };
  836. constexpr _test tests[] {
  837. { "foo(.+)"sv, "aaa"sv, "test"sv, "test"sv },
  838. { "foo(.+)"sv, "test\\1"sv, "foobar"sv, "testbar"sv },
  839. { "foo(.+)"sv, "\\2\\1"sv, "foobar"sv, "\\2bar"sv },
  840. { "foo(.+)"sv, "\\\\\\1"sv, "foobar"sv, "\\bar"sv },
  841. { "foo(.)"sv, "a\\1"sv, "fooxfooy"sv, "axay"sv, ECMAScriptFlags::Multiline },
  842. };
  843. for (auto& test : tests) {
  844. Regex<ECMA262> re(test.pattern, test.options);
  845. if constexpr (REGEX_DEBUG) {
  846. dbgln("\n");
  847. RegexDebug regex_dbg(stderr);
  848. regex_dbg.print_raw_bytecode(re);
  849. regex_dbg.print_header();
  850. regex_dbg.print_bytecode(re);
  851. dbgln("\n");
  852. }
  853. EXPECT_EQ(re.parser_result.error, regex::Error::NoError);
  854. EXPECT_EQ(re.replace(test.subject, test.replacement), test.expected);
  855. }
  856. }
  857. TEST_CASE(case_insensitive_match)
  858. {
  859. Regex<PosixExtended> re("cd", PosixFlags::Insensitive | PosixFlags::Global);
  860. auto result = re.match("AEKFCD"sv);
  861. EXPECT_EQ(result.success, true);
  862. if (result.success) {
  863. EXPECT_EQ(result.matches.at(0).column, 4ul);
  864. }
  865. }
  866. TEST_CASE(extremely_long_fork_chain)
  867. {
  868. Regex<ECMA262> re("(?:aa)*");
  869. auto result = re.match(ByteString::repeated('a', 1000));
  870. EXPECT_EQ(result.success, true);
  871. }
  872. TEST_CASE(theoretically_infinite_loop)
  873. {
  874. Array patterns {
  875. "(a*)*"sv, // Infinitely matching empty substrings, the outer loop should short-circuit.
  876. "(a*?)*"sv, // Infinitely matching empty substrings, the outer loop should short-circuit.
  877. "(a*)*?"sv, // Should match exactly nothing.
  878. "(?:)*?"sv, // Should not generate an infinite fork loop.
  879. "(a?)+$"sv, // Infinitely matching empty strings, but with '+' instead of '*'.
  880. };
  881. for (auto& pattern : patterns) {
  882. Regex<ECMA262> re(pattern);
  883. auto result = re.match(""sv);
  884. EXPECT_EQ(result.success, true);
  885. }
  886. }
  887. static auto g_lots_of_a_s = ByteString::repeated('a', 10'000'000);
  888. BENCHMARK_CASE(fork_performance)
  889. {
  890. Regex<ECMA262> re("(?:aa)*");
  891. auto result = re.match(g_lots_of_a_s);
  892. EXPECT_EQ(result.success, true);
  893. }
  894. BENCHMARK_CASE(anchor_performance)
  895. {
  896. Regex<ECMA262> re("^b");
  897. for (auto i = 0; i < 100'000; i++) {
  898. auto result = re.match(g_lots_of_a_s);
  899. EXPECT_EQ(result.success, false);
  900. }
  901. }
  902. TEST_CASE(optimizer_atomic_groups)
  903. {
  904. Array tests {
  905. // Fork -> ForkReplace
  906. Tuple { "a*b"sv, "aaaaa"sv, false },
  907. Tuple { "a+b"sv, "aaaaa"sv, false },
  908. Tuple { "\\\\(\\d+)"sv, "\\\\"sv, false }, // Rewrite bug turning a+ to a*, see #10952.
  909. Tuple { "[a-z.]+\\."sv, "..."sv, true }, // Rewrite bug, incorrect interpretation of Compare.
  910. Tuple { "[.-]+\\."sv, ".-."sv, true },
  911. // Alternative fuse
  912. Tuple { "(abcfoo|abcbar|abcbaz).*x"sv, "abcbarx"sv, true },
  913. Tuple { "(a|a)"sv, "a"sv, true },
  914. Tuple { "(a|)"sv, ""sv, true }, // Ensure that empty alternatives are not outright removed
  915. Tuple { "a{2,3}|a{5,8}"sv, "abc"sv, false }, // Optimizer should not mess up the instruction stream by ignoring inter-insn dependencies, see #11247.
  916. Tuple { "^(a{2,3}|a{5,8})$"sv, "aaaa"sv, false }, // Optimizer should not mess up the instruction stream by ignoring inter-insn dependencies, see #11247.
  917. // Optimizer should not chop off *half* of an instruction when fusing instructions.
  918. Tuple { "cubic-bezier\\(\\s*(-?\\d+\\.?\\d*|-?\\.\\d+)\\s*,\\s*(-?\\d+\\.?\\d*|-?\\.\\d+)\\s*,\\s*(-?\\d+\\.?\\d*|-?\\.\\d+)\\s*,\\s*(-?\\d+\\.?\\d*|-?\\.\\d+)\\s*\\)"sv, "cubic-bezier(.05, 0, 0, 1)"sv, true },
  919. // ForkReplace shouldn't be applied where it would change the semantics
  920. Tuple { "(1+)\\1"sv, "11"sv, true },
  921. Tuple { "(1+)1"sv, "11"sv, true },
  922. Tuple { "(1+)0"sv, "10"sv, true },
  923. // Rewrite should not skip over first required iteration of <x>+.
  924. Tuple { "a+"sv, ""sv, false },
  925. // 'y' and [^x] have an overlap ('y'), the loop should not be rewritten here.
  926. Tuple { "[^x]+y"sv, "ay"sv, true },
  927. // .+ should not be rewritten here, as it's followed by something that would be matched by `.`.
  928. Tuple { ".+(a|b|c)"sv, "xxa"sv, true },
  929. // (b+)(b+) produces an intermediate block with no matching ops, the optimiser should ignore that block when looking for following matches and correctly detect the overlap between (b+) and (b+).
  930. // note that the second loop may be rewritten to a ForkReplace, but the first loop should not be rewritten.
  931. Tuple { "(b+)(b+)"sv, "bbb"sv, true },
  932. };
  933. for (auto& test : tests) {
  934. Regex<ECMA262> re(test.get<0>());
  935. auto result = re.match(test.get<1>());
  936. EXPECT_EQ(result.success, test.get<2>());
  937. }
  938. }
  939. TEST_CASE(optimizer_char_class_lut)
  940. {
  941. Regex<ECMA262> re(R"([\f\n\r\t\v\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+$)");
  942. if constexpr (REGEX_DEBUG) {
  943. dbgln("\n");
  944. RegexDebug regex_dbg(stderr);
  945. regex_dbg.print_raw_bytecode(re);
  946. regex_dbg.print_header();
  947. regex_dbg.print_bytecode(re);
  948. dbgln("\n");
  949. }
  950. // This will go through _all_ alternatives in the character class, and then fail.
  951. for (size_t i = 0; i < 1'000'000; ++i)
  952. EXPECT_EQ(re.match("1635488940000"sv).success, false);
  953. }
  954. TEST_CASE(optimizer_alternation)
  955. {
  956. Array tests {
  957. // Pattern, Subject, Expected length
  958. Tuple { "a|"sv, "a"sv, 1u },
  959. Tuple { "a|a|a|a|a|a|a|a|a|b"sv, "a"sv, 1u },
  960. Tuple { "ab|ac|ad|bc"sv, "bc"sv, 2u },
  961. // Should not crash on backwards jumps introduced by '.*'.
  962. Tuple { "\\bDroid\\b.*Build|XT912|XT928|XT926|XT915|XT919|XT925|XT1021|\\bMoto E\\b|XT1068|XT1092|XT1052"sv, "XT1068"sv, 6u },
  963. // Backwards jumps to IP 0 are normal jumps too.
  964. Tuple { "^(\\d+|x)"sv, "42"sv, 2u },
  965. // `Repeat' does not add its insn size to the jump target.
  966. Tuple { "[0-9]{2}|[0-9]"sv, "92"sv, 2u },
  967. };
  968. for (auto& test : tests) {
  969. Regex<ECMA262> re(test.get<0>());
  970. auto result = re.match(test.get<1>());
  971. EXPECT(result.success);
  972. EXPECT_EQ(result.matches.first().view.length(), test.get<2>());
  973. }
  974. }
  975. TEST_CASE(start_anchor)
  976. {
  977. // Ensure that a circumflex at the start only matches the start of the line.
  978. {
  979. Regex<PosixBasic> re("^abc");
  980. EXPECT_EQ(re.match("123abcdef"sv, PosixFlags::Global).success, false);
  981. EXPECT_EQ(re.match("abc123"sv, PosixFlags::Global).success, true);
  982. EXPECT_EQ(re.match("123^abcdef"sv, PosixFlags::Global).success, false);
  983. EXPECT_EQ(re.match("^abc123"sv, PosixFlags::Global).success, false);
  984. // Multiple lines
  985. EXPECT_EQ(re.match("123\nabc"sv, PosixFlags::Multiline).success, true);
  986. }
  987. }
  988. TEST_CASE(posix_basic_dollar_is_end_anchor)
  989. {
  990. // Ensure that a dollar sign at the end only matches the end of the line.
  991. {
  992. Regex<PosixBasic> re("abc$");
  993. EXPECT_EQ(re.match("123abcdef"sv, PosixFlags::Global).success, false);
  994. EXPECT_EQ(re.match("123abc"sv, PosixFlags::Global).success, true);
  995. EXPECT_EQ(re.match("123abc$def"sv, PosixFlags::Global).success, false);
  996. EXPECT_EQ(re.match("123abc$"sv, PosixFlags::Global).success, false);
  997. }
  998. }
  999. TEST_CASE(posix_basic_dollar_is_literal)
  1000. {
  1001. // Ensure that a dollar sign in the middle is treated as a literal.
  1002. {
  1003. Regex<PosixBasic> re("abc$d");
  1004. EXPECT_EQ(re.match("123abcdef"sv, PosixFlags::Global).success, false);
  1005. EXPECT_EQ(re.match("123abc"sv, PosixFlags::Global).success, false);
  1006. EXPECT_EQ(re.match("123abc$def"sv, PosixFlags::Global).success, true);
  1007. EXPECT_EQ(re.match("123abc$"sv, PosixFlags::Global).success, false);
  1008. }
  1009. // Ensure that a dollar sign is always treated as a literal if escaped, even if at the end of the pattern.
  1010. {
  1011. Regex<PosixBasic> re("abc\\$");
  1012. EXPECT_EQ(re.match("123abcdef"sv, PosixFlags::Global).success, false);
  1013. EXPECT_EQ(re.match("123abc"sv, PosixFlags::Global).success, false);
  1014. EXPECT_EQ(re.match("123abc$def"sv, PosixFlags::Global).success, true);
  1015. EXPECT_EQ(re.match("123abc$"sv, PosixFlags::Global).success, true);
  1016. }
  1017. }
  1018. TEST_CASE(negative_lookahead)
  1019. {
  1020. {
  1021. // Negative lookahead with more than 2 forks difference between lookahead init and finish.
  1022. auto options = ECMAScriptOptions { ECMAScriptFlags::Global };
  1023. options.reset_flag((ECMAScriptFlags)regex::AllFlags::Internal_Stateful);
  1024. Regex<ECMA262> re(":(?!\\^\\)|1)", options);
  1025. EXPECT_EQ(re.match(":^)"sv).success, false);
  1026. EXPECT_EQ(re.match(":1"sv).success, false);
  1027. EXPECT_EQ(re.match(":foobar"sv).success, true);
  1028. }
  1029. {
  1030. // Correctly count forks with nested groups and optimised loops
  1031. Regex<ECMA262> re("^((?:[^\\n]|\\n(?! *\\n))+)(?:\\n *)+\\n");
  1032. EXPECT_EQ(re.match("foo\n\n"sv).success, true);
  1033. EXPECT_EQ(re.match("foo\n"sv).success, false);
  1034. }
  1035. }
  1036. TEST_CASE(single_match_flag)
  1037. {
  1038. {
  1039. // Ensure that only a single match is produced and nothing past that.
  1040. Regex<ECMA262> re("[\\u0008-\\uffff]"sv, ECMAScriptFlags::Global | (ECMAScriptFlags)regex::AllFlags::SingleMatch);
  1041. auto result = re.match("ABC"sv);
  1042. EXPECT_EQ(result.success, true);
  1043. EXPECT_EQ(result.matches.size(), 1u);
  1044. EXPECT_EQ(result.matches.first().view.to_byte_string(), "A"sv);
  1045. }
  1046. }
  1047. TEST_CASE(empty_string_wildcard_match)
  1048. {
  1049. {
  1050. // Ensure that the wildcard ".*" matches the empty string exactly once
  1051. Regex<ECMA262> re(".*"sv, ECMAScriptFlags::Global);
  1052. auto result = re.match(""sv);
  1053. EXPECT_EQ(result.success, true);
  1054. EXPECT_EQ(result.matches.size(), 1u);
  1055. EXPECT_EQ(result.matches.first().view.to_byte_string(), ""sv);
  1056. }
  1057. }
  1058. TEST_CASE(inversion_state_in_char_class)
  1059. {
  1060. {
  1061. // #13755, /[\S\s]/.exec("hello") should be [ "h" ], not null.
  1062. Regex<ECMA262> re("[\\S\\s]", ECMAScriptFlags::Global | (ECMAScriptFlags)regex::AllFlags::SingleMatch);
  1063. auto result = re.match("hello"sv);
  1064. EXPECT_EQ(result.success, true);
  1065. EXPECT_EQ(result.matches.size(), 1u);
  1066. EXPECT_EQ(result.matches.first().view.to_byte_string(), "h"sv);
  1067. }
  1068. {
  1069. Regex<ECMA262> re("^(?:([^\\s!\"#%-,\\./;->@\\[-\\^`\\{-~]+(?=([=~}\\s/.)|]))))"sv, ECMAScriptFlags::Global);
  1070. auto result = re.match("slideNumbers}}"sv);
  1071. EXPECT_EQ(result.success, true);
  1072. EXPECT_EQ(result.matches.size(), 1u);
  1073. EXPECT_EQ(result.matches.first().view.to_byte_string(), "slideNumbers"sv);
  1074. EXPECT_EQ(result.capture_group_matches.first()[0].view.to_byte_string(), "slideNumbers"sv);
  1075. EXPECT_EQ(result.capture_group_matches.first()[1].view.to_byte_string(), "}"sv);
  1076. }
  1077. {
  1078. // #21786, /[^\S\n]/.exec("\n") should be null, not [ "\n" ].
  1079. // This was a general confusion between the inversion state and the negation state (temp inverse).
  1080. Regex<ECMA262> re("[^\\S\\n]", ECMAScriptFlags::Global | (ECMAScriptFlags)regex::AllFlags::SingleMatch);
  1081. auto result = re.match("\n"sv);
  1082. EXPECT_EQ(result.success, false);
  1083. }
  1084. }
  1085. TEST_CASE(mismatching_brackets)
  1086. {
  1087. auto const test_cases = Array {
  1088. "["sv,
  1089. "[ -"sv,
  1090. };
  1091. for (auto const& test_case : test_cases) {
  1092. Regex<ECMA262> re(test_case);
  1093. EXPECT_EQ(re.parser_result.error, regex::Error::MismatchingBracket);
  1094. }
  1095. }