RegexMatch.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Forward.h"
  8. #include "RegexOptions.h"
  9. #include <AK/FlyString.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/MemMem.h>
  12. #include <AK/String.h>
  13. #include <AK/StringBuilder.h>
  14. #include <AK/StringView.h>
  15. #include <AK/Utf16View.h>
  16. #include <AK/Utf32View.h>
  17. #include <AK/Utf8View.h>
  18. #include <AK/Variant.h>
  19. #include <AK/Vector.h>
  20. namespace regex {
  21. class RegexStringView {
  22. public:
  23. RegexStringView(char const* chars)
  24. : m_view(StringView { chars })
  25. {
  26. }
  27. RegexStringView(String const& string)
  28. : m_view(string.view())
  29. {
  30. }
  31. RegexStringView(StringView const view)
  32. : m_view(view)
  33. {
  34. }
  35. RegexStringView(Utf32View view)
  36. : m_view(view)
  37. {
  38. }
  39. RegexStringView(Utf16View view)
  40. : m_view(view)
  41. {
  42. }
  43. RegexStringView(Utf8View view)
  44. : m_view(view)
  45. {
  46. }
  47. explicit RegexStringView(String&&) = delete;
  48. StringView const& string_view() const
  49. {
  50. return m_view.get<StringView>();
  51. }
  52. Utf32View const& u32_view() const
  53. {
  54. return m_view.get<Utf32View>();
  55. }
  56. Utf16View const& u16_view() const
  57. {
  58. return m_view.get<Utf16View>();
  59. }
  60. Utf8View const& u8_view() const
  61. {
  62. return m_view.get<Utf8View>();
  63. }
  64. bool unicode() const { return m_unicode; }
  65. void set_unicode(bool unicode) { m_unicode = unicode; }
  66. bool is_empty() const
  67. {
  68. return m_view.visit([](auto& view) { return view.is_empty(); });
  69. }
  70. bool is_null() const
  71. {
  72. return m_view.visit([](auto& view) { return view.is_null(); });
  73. }
  74. size_t length() const
  75. {
  76. if (unicode()) {
  77. return m_view.visit(
  78. [](Utf16View const& view) { return view.length_in_code_points(); },
  79. [](auto const& view) { return view.length(); });
  80. }
  81. return length_in_code_units();
  82. }
  83. size_t length_in_code_units() const
  84. {
  85. return m_view.visit(
  86. [](Utf16View const& view) { return view.length_in_code_units(); },
  87. [](Utf8View const& view) { return view.byte_length(); },
  88. [](auto const& view) { return view.length(); });
  89. }
  90. size_t length_of_code_point(u32 code_point) const
  91. {
  92. return m_view.visit(
  93. [](Utf32View const&) { return 1; },
  94. [&](Utf16View const&) {
  95. if (code_point < 0x10000)
  96. return 1;
  97. return 2;
  98. },
  99. [&](auto const&) {
  100. if (code_point <= 0x7f)
  101. return 1;
  102. else if (code_point <= 0x07ff)
  103. return 2;
  104. else if (code_point <= 0xffff)
  105. return 3;
  106. return 4;
  107. });
  108. }
  109. RegexStringView typed_null_view()
  110. {
  111. auto view = m_view.visit(
  112. [&]<typename T>(T const&) {
  113. return RegexStringView { T {} };
  114. });
  115. view.set_unicode(unicode());
  116. return view;
  117. }
  118. RegexStringView construct_as_same(Span<u32> data, Optional<String>& optional_string_storage, Vector<u16>& optional_utf16_storage) const
  119. {
  120. auto view = m_view.visit(
  121. [&]<typename T>(T const&) {
  122. StringBuilder builder;
  123. for (auto ch : data)
  124. builder.append(ch); // Note: The type conversion is intentional.
  125. optional_string_storage = builder.build();
  126. return RegexStringView { T { *optional_string_storage } };
  127. },
  128. [&](Utf32View) {
  129. return RegexStringView { Utf32View { data.data(), data.size() } };
  130. },
  131. [&](Utf16View) {
  132. optional_utf16_storage = AK::utf32_to_utf16(Utf32View { data.data(), data.size() });
  133. return RegexStringView { Utf16View { optional_utf16_storage } };
  134. });
  135. view.set_unicode(unicode());
  136. return view;
  137. }
  138. Vector<RegexStringView> lines() const
  139. {
  140. return m_view.visit(
  141. [](StringView view) {
  142. auto views = view.lines(false);
  143. Vector<RegexStringView> new_views;
  144. for (auto& view : views)
  145. new_views.empend(view);
  146. return new_views;
  147. },
  148. [](Utf32View view) {
  149. Vector<RegexStringView> views;
  150. u32 newline = '\n';
  151. while (!view.is_empty()) {
  152. auto position = AK::memmem_optional(view.code_points(), view.length() * sizeof(u32), &newline, sizeof(u32));
  153. if (!position.has_value())
  154. break;
  155. auto offset = position.value() / sizeof(u32);
  156. views.empend(view.substring_view(0, offset));
  157. view = view.substring_view(offset + 1, view.length() - offset - 1);
  158. }
  159. if (!view.is_empty())
  160. views.empend(view);
  161. return views;
  162. },
  163. [](Utf16View view) {
  164. Vector<RegexStringView> views;
  165. u16 newline = '\n';
  166. while (!view.is_empty()) {
  167. auto position = AK::memmem_optional(view.data(), view.length_in_code_units() * sizeof(u16), &newline, sizeof(u16));
  168. if (!position.has_value())
  169. break;
  170. auto offset = position.value() / sizeof(u16);
  171. views.empend(view.substring_view(0, offset));
  172. view = view.substring_view(offset + 1, view.length_in_code_units() - offset - 1);
  173. }
  174. if (!view.is_empty())
  175. views.empend(view);
  176. return views;
  177. },
  178. [](Utf8View& view) {
  179. Vector<RegexStringView> views;
  180. auto it = view.begin();
  181. auto previous_newline_position_it = it;
  182. for (;;) {
  183. if (*it == '\n') {
  184. auto previous_offset = view.byte_offset_of(previous_newline_position_it);
  185. auto new_offset = view.byte_offset_of(it);
  186. auto slice = view.substring_view(previous_offset, new_offset - previous_offset);
  187. views.empend(slice);
  188. ++it;
  189. previous_newline_position_it = it;
  190. }
  191. if (it.done())
  192. break;
  193. ++it;
  194. }
  195. if (it != previous_newline_position_it) {
  196. auto previous_offset = view.byte_offset_of(previous_newline_position_it);
  197. auto new_offset = view.byte_offset_of(it);
  198. auto slice = view.substring_view(previous_offset, new_offset - previous_offset);
  199. views.empend(slice);
  200. }
  201. return views;
  202. });
  203. }
  204. RegexStringView substring_view(size_t offset, size_t length) const
  205. {
  206. if (unicode()) {
  207. auto view = m_view.visit(
  208. [&](auto view) { return RegexStringView { view.substring_view(offset, length) }; },
  209. [&](Utf16View const& view) { return RegexStringView { view.unicode_substring_view(offset, length) }; },
  210. [&](Utf8View const& view) { return RegexStringView { view.unicode_substring_view(offset, length) }; });
  211. view.set_unicode(unicode());
  212. return view;
  213. }
  214. auto view = m_view.visit([&](auto view) { return RegexStringView { view.substring_view(offset, length) }; });
  215. view.set_unicode(unicode());
  216. return view;
  217. }
  218. String to_string() const
  219. {
  220. return m_view.visit(
  221. [](StringView view) { return view.to_string(); },
  222. [](Utf16View view) { return view.to_utf8(Utf16View::AllowInvalidCodeUnits::Yes); },
  223. [](auto& view) {
  224. StringBuilder builder;
  225. for (auto it = view.begin(); it != view.end(); ++it)
  226. builder.append_code_point(*it);
  227. return builder.to_string();
  228. });
  229. }
  230. // Note: index must always be the code unit offset to return.
  231. u32 operator[](size_t index) const
  232. {
  233. return m_view.visit(
  234. [&](StringView view) -> u32 {
  235. auto ch = view[index];
  236. if (ch < 0)
  237. return 256u + ch;
  238. return ch;
  239. },
  240. [&](Utf32View const& view) -> u32 { return view[index]; },
  241. [&](Utf16View const& view) -> u32 { return view.code_point_at(index); },
  242. [&](Utf8View const& view) -> u32 {
  243. auto it = view.iterator_at_byte_offset(index);
  244. VERIFY(it != view.end());
  245. return *it;
  246. });
  247. }
  248. size_t code_unit_offset_of(size_t code_point_index) const
  249. {
  250. return m_view.visit(
  251. [&](StringView const& view) -> u32 {
  252. Utf8View utf8_view { view };
  253. return utf8_view.byte_offset_of(code_point_index);
  254. },
  255. [&](Utf32View const&) -> u32 { return code_point_index; },
  256. [&](Utf16View const& view) -> u32 {
  257. return view.code_unit_offset_of(code_point_index);
  258. },
  259. [&](Utf8View const& view) -> u32 {
  260. return view.byte_offset_of(code_point_index);
  261. });
  262. }
  263. bool operator==(char const* cstring) const
  264. {
  265. return m_view.visit(
  266. [&](Utf32View) { return to_string() == cstring; },
  267. [&](Utf16View) { return to_string() == cstring; },
  268. [&](Utf8View const& view) { return view.as_string() == cstring; },
  269. [&](StringView view) { return view == cstring; });
  270. }
  271. bool operator!=(char const* cstring) const
  272. {
  273. return !(*this == cstring);
  274. }
  275. bool operator==(String const& string) const
  276. {
  277. return m_view.visit(
  278. [&](Utf32View) { return to_string() == string; },
  279. [&](Utf16View) { return to_string() == string; },
  280. [&](Utf8View const& view) { return view.as_string() == string; },
  281. [&](StringView view) { return view == string; });
  282. }
  283. bool operator==(StringView const& string) const
  284. {
  285. return m_view.visit(
  286. [&](Utf32View) { return to_string() == string; },
  287. [&](Utf16View) { return to_string() == string; },
  288. [&](Utf8View const& view) { return view.as_string() == string; },
  289. [&](StringView view) { return view == string; });
  290. }
  291. bool operator!=(StringView const& other) const
  292. {
  293. return !(*this == other);
  294. }
  295. bool operator==(Utf32View const& other) const
  296. {
  297. return m_view.visit(
  298. [&](Utf32View view) {
  299. return view.length() == other.length() && __builtin_memcmp(view.code_points(), other.code_points(), view.length() * sizeof(u32)) == 0;
  300. },
  301. [&](Utf16View) { return to_string() == RegexStringView { other }.to_string(); },
  302. [&](Utf8View const& view) { return view.as_string() == RegexStringView { other }.to_string(); },
  303. [&](StringView view) { return view == RegexStringView { other }.to_string(); });
  304. }
  305. bool operator!=(Utf32View const& other) const
  306. {
  307. return !(*this == other);
  308. }
  309. bool operator==(Utf16View const& other) const
  310. {
  311. return m_view.visit(
  312. [&](Utf32View) { return to_string() == RegexStringView { other }.to_string(); },
  313. [&](Utf16View const& view) { return view == other; },
  314. [&](Utf8View const& view) { return view.as_string() == RegexStringView { other }.to_string(); },
  315. [&](StringView view) { return view == RegexStringView { other }.to_string(); });
  316. }
  317. bool operator!=(Utf16View const& other) const
  318. {
  319. return !(*this == other);
  320. }
  321. bool operator==(Utf8View const& other) const
  322. {
  323. return m_view.visit(
  324. [&](Utf32View) { return to_string() == other.as_string(); },
  325. [&](Utf16View) { return to_string() == other.as_string(); },
  326. [&](Utf8View const& view) { return view.as_string() == other.as_string(); },
  327. [&](StringView view) { return other.as_string() == view; });
  328. }
  329. bool operator!=(Utf8View const& other) const
  330. {
  331. return !(*this == other);
  332. }
  333. bool equals(RegexStringView const& other) const
  334. {
  335. return other.m_view.visit([&](auto const& view) { return operator==(view); });
  336. }
  337. bool equals_ignoring_case(RegexStringView const& other) const
  338. {
  339. // FIXME: Implement equals_ignoring_case() for unicode.
  340. return m_view.visit(
  341. [&](StringView view) {
  342. return other.m_view.visit(
  343. [&](StringView other_view) { return view.equals_ignoring_case(other_view); },
  344. [](auto&) -> bool { TODO(); });
  345. },
  346. [&](Utf16View view) {
  347. return other.m_view.visit(
  348. [&](Utf16View other_view) { return view.equals_ignoring_case(other_view); },
  349. [](auto&) -> bool { TODO(); });
  350. },
  351. [](auto&) -> bool { TODO(); });
  352. }
  353. bool starts_with(StringView const& str) const
  354. {
  355. return m_view.visit(
  356. [&](Utf32View) -> bool {
  357. TODO();
  358. },
  359. [&](Utf16View) -> bool {
  360. TODO();
  361. },
  362. [&](Utf8View const& view) { return view.as_string().starts_with(str); },
  363. [&](StringView view) { return view.starts_with(str); });
  364. }
  365. bool starts_with(Utf32View const& str) const
  366. {
  367. return m_view.visit(
  368. [&](Utf32View view) -> bool {
  369. if (str.length() > view.length())
  370. return false;
  371. if (str.length() == view.length())
  372. return operator==(str);
  373. for (size_t i = 0; i < str.length(); ++i) {
  374. if (str.at(i) != view.at(i))
  375. return false;
  376. }
  377. return true;
  378. },
  379. [&](Utf16View) -> bool { TODO(); },
  380. [&](Utf8View const& view) {
  381. auto it = view.begin();
  382. for (auto code_point : str) {
  383. if (it.done())
  384. return false;
  385. if (code_point != *it)
  386. return false;
  387. ++it;
  388. }
  389. return true;
  390. },
  391. [&](StringView) -> bool { TODO(); });
  392. }
  393. private:
  394. Variant<StringView, Utf8View, Utf16View, Utf32View> m_view;
  395. bool m_unicode { false };
  396. };
  397. class Match final {
  398. private:
  399. Optional<FlyString> string;
  400. public:
  401. Match() = default;
  402. ~Match() = default;
  403. Match(RegexStringView const view_, size_t const line_, size_t const column_, size_t const global_offset_)
  404. : view(view_)
  405. , line(line_)
  406. , column(column_)
  407. , global_offset(global_offset_)
  408. , left_column(column_)
  409. {
  410. }
  411. Match(String const string_, size_t const line_, size_t const column_, size_t const global_offset_)
  412. : string(move(string_))
  413. , view(string.value().view())
  414. , line(line_)
  415. , column(column_)
  416. , global_offset(global_offset_)
  417. {
  418. }
  419. Match(RegexStringView const view_, StringView capture_group_name_, size_t const line_, size_t const column_, size_t const global_offset_)
  420. : view(view_)
  421. , capture_group_name(capture_group_name_)
  422. , line(line_)
  423. , column(column_)
  424. , global_offset(global_offset_)
  425. , left_column(column_)
  426. {
  427. }
  428. void reset()
  429. {
  430. view = view.typed_null_view();
  431. capture_group_name.clear();
  432. line = 0;
  433. column = 0;
  434. global_offset = 0;
  435. left_column = 0;
  436. }
  437. RegexStringView view { nullptr };
  438. Optional<FlyString> capture_group_name {};
  439. size_t line { 0 };
  440. size_t column { 0 };
  441. size_t global_offset { 0 };
  442. // ugly, as not usable by user, but needed to prevent to create extra vectors that are
  443. // able to store the column when the left paren has been found
  444. size_t left_column { 0 };
  445. };
  446. struct MatchInput {
  447. RegexStringView view { nullptr };
  448. AllOptions regex_options {};
  449. size_t start_offset { 0 }; // For Stateful matches, saved and restored from Regex::start_offset.
  450. size_t match_index { 0 };
  451. size_t line { 0 };
  452. size_t column { 0 };
  453. size_t global_offset { 0 }; // For multiline matching, knowing the offset from start could be important
  454. mutable size_t fail_counter { 0 };
  455. mutable Vector<size_t> saved_positions;
  456. mutable Vector<size_t> saved_code_unit_positions;
  457. mutable HashMap<u64, u64> checkpoints;
  458. mutable Optional<size_t> fork_to_replace;
  459. };
  460. struct MatchState {
  461. size_t string_position_before_match { 0 };
  462. size_t string_position { 0 };
  463. size_t string_position_in_code_units { 0 };
  464. size_t instruction_position { 0 };
  465. size_t fork_at_position { 0 };
  466. Optional<size_t> initiating_fork;
  467. Vector<Match> matches;
  468. Vector<Vector<Match>> capture_group_matches;
  469. Vector<u64> repetition_marks;
  470. };
  471. }
  472. using regex::RegexStringView;
  473. template<>
  474. struct AK::Formatter<regex::RegexStringView> : Formatter<StringView> {
  475. void format(FormatBuilder& builder, regex::RegexStringView const& value)
  476. {
  477. auto string = value.to_string();
  478. return Formatter<StringView>::format(builder, string);
  479. }
  480. };