RegexByteCode.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "RegexByteCode.h"
  7. #include "RegexDebug.h"
  8. #include <AK/BinarySearch.h>
  9. #include <AK/CharacterTypes.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibUnicode/CharacterTypes.h>
  12. // U+2028 LINE SEPARATOR
  13. constexpr static u32 const LineSeparator { 0x2028 };
  14. // U+2029 PARAGRAPH SEPARATOR
  15. constexpr static u32 const ParagraphSeparator { 0x2029 };
  16. namespace regex {
  17. StringView OpCode::name(OpCodeId opcode_id)
  18. {
  19. switch (opcode_id) {
  20. #define __ENUMERATE_OPCODE(x) \
  21. case OpCodeId::x: \
  22. return #x##sv;
  23. ENUMERATE_OPCODES
  24. #undef __ENUMERATE_OPCODE
  25. default:
  26. VERIFY_NOT_REACHED();
  27. return "<Unknown>"sv;
  28. }
  29. }
  30. StringView OpCode::name() const
  31. {
  32. return name(opcode_id());
  33. }
  34. StringView execution_result_name(ExecutionResult result)
  35. {
  36. switch (result) {
  37. #define __ENUMERATE_EXECUTION_RESULT(x) \
  38. case ExecutionResult::x: \
  39. return #x##sv;
  40. ENUMERATE_EXECUTION_RESULTS
  41. #undef __ENUMERATE_EXECUTION_RESULT
  42. default:
  43. VERIFY_NOT_REACHED();
  44. return "<Unknown>"sv;
  45. }
  46. }
  47. StringView opcode_id_name(OpCodeId opcode)
  48. {
  49. switch (opcode) {
  50. #define __ENUMERATE_OPCODE(x) \
  51. case OpCodeId::x: \
  52. return #x##sv;
  53. ENUMERATE_OPCODES
  54. #undef __ENUMERATE_OPCODE
  55. default:
  56. VERIFY_NOT_REACHED();
  57. return "<Unknown>"sv;
  58. }
  59. }
  60. StringView boundary_check_type_name(BoundaryCheckType ty)
  61. {
  62. switch (ty) {
  63. #define __ENUMERATE_BOUNDARY_CHECK_TYPE(x) \
  64. case BoundaryCheckType::x: \
  65. return #x##sv;
  66. ENUMERATE_BOUNDARY_CHECK_TYPES
  67. #undef __ENUMERATE_BOUNDARY_CHECK_TYPE
  68. default:
  69. VERIFY_NOT_REACHED();
  70. return "<Unknown>"sv;
  71. }
  72. }
  73. StringView character_compare_type_name(CharacterCompareType ch_compare_type)
  74. {
  75. switch (ch_compare_type) {
  76. #define __ENUMERATE_CHARACTER_COMPARE_TYPE(x) \
  77. case CharacterCompareType::x: \
  78. return #x##sv;
  79. ENUMERATE_CHARACTER_COMPARE_TYPES
  80. #undef __ENUMERATE_CHARACTER_COMPARE_TYPE
  81. default:
  82. VERIFY_NOT_REACHED();
  83. return "<Unknown>"sv;
  84. }
  85. }
  86. static StringView character_class_name(CharClass ch_class)
  87. {
  88. switch (ch_class) {
  89. #define __ENUMERATE_CHARACTER_CLASS(x) \
  90. case CharClass::x: \
  91. return #x##sv;
  92. ENUMERATE_CHARACTER_CLASSES
  93. #undef __ENUMERATE_CHARACTER_CLASS
  94. default:
  95. VERIFY_NOT_REACHED();
  96. return "<Unknown>"sv;
  97. }
  98. }
  99. static void advance_string_position(MatchState& state, RegexStringView view, Optional<u32> code_point = {})
  100. {
  101. ++state.string_position;
  102. if (view.unicode()) {
  103. if (!code_point.has_value() && (state.string_position_in_code_units < view.length_in_code_units()))
  104. code_point = view[state.string_position_in_code_units];
  105. if (code_point.has_value())
  106. state.string_position_in_code_units += view.length_of_code_point(*code_point);
  107. } else {
  108. ++state.string_position_in_code_units;
  109. }
  110. }
  111. static void advance_string_position(MatchState& state, RegexStringView, RegexStringView advance_by)
  112. {
  113. state.string_position += advance_by.length();
  114. state.string_position_in_code_units += advance_by.length_in_code_units();
  115. }
  116. static void reverse_string_position(MatchState& state, RegexStringView view, size_t amount)
  117. {
  118. VERIFY(state.string_position >= amount);
  119. state.string_position -= amount;
  120. if (view.unicode())
  121. state.string_position_in_code_units = view.code_unit_offset_of(state.string_position);
  122. else
  123. state.string_position_in_code_units -= amount;
  124. }
  125. static void save_string_position(MatchInput const& input, MatchState const& state)
  126. {
  127. input.saved_positions.append(state.string_position);
  128. input.saved_forks_since_last_save.append(state.forks_since_last_save);
  129. input.saved_code_unit_positions.append(state.string_position_in_code_units);
  130. }
  131. static bool restore_string_position(MatchInput const& input, MatchState& state)
  132. {
  133. if (input.saved_positions.is_empty())
  134. return false;
  135. state.string_position = input.saved_positions.take_last();
  136. state.string_position_in_code_units = input.saved_code_unit_positions.take_last();
  137. state.forks_since_last_save = input.saved_forks_since_last_save.take_last();
  138. return true;
  139. }
  140. OwnPtr<OpCode> ByteCode::s_opcodes[(size_t)OpCodeId::Last + 1];
  141. bool ByteCode::s_opcodes_initialized { false };
  142. size_t ByteCode::s_next_checkpoint_serial_id { 0 };
  143. void ByteCode::ensure_opcodes_initialized()
  144. {
  145. if (s_opcodes_initialized)
  146. return;
  147. for (u32 i = (u32)OpCodeId::First; i <= (u32)OpCodeId::Last; ++i) {
  148. switch ((OpCodeId)i) {
  149. #define __ENUMERATE_OPCODE(OpCode) \
  150. case OpCodeId::OpCode: \
  151. s_opcodes[i] = make<OpCode_##OpCode>(); \
  152. break;
  153. ENUMERATE_OPCODES
  154. #undef __ENUMERATE_OPCODE
  155. }
  156. }
  157. s_opcodes_initialized = true;
  158. }
  159. ALWAYS_INLINE ExecutionResult OpCode_Exit::execute(MatchInput const& input, MatchState& state) const
  160. {
  161. if (state.string_position > input.view.length() || state.instruction_position >= m_bytecode->size())
  162. return ExecutionResult::Succeeded;
  163. return ExecutionResult::Failed;
  164. }
  165. ALWAYS_INLINE ExecutionResult OpCode_Save::execute(MatchInput const& input, MatchState& state) const
  166. {
  167. save_string_position(input, state);
  168. state.forks_since_last_save = 0;
  169. return ExecutionResult::Continue;
  170. }
  171. ALWAYS_INLINE ExecutionResult OpCode_Restore::execute(MatchInput const& input, MatchState& state) const
  172. {
  173. if (!restore_string_position(input, state))
  174. return ExecutionResult::Failed;
  175. return ExecutionResult::Continue;
  176. }
  177. ALWAYS_INLINE ExecutionResult OpCode_GoBack::execute(MatchInput const& input, MatchState& state) const
  178. {
  179. if (count() > state.string_position)
  180. return ExecutionResult::Failed_ExecuteLowPrioForks;
  181. reverse_string_position(state, input.view, count());
  182. return ExecutionResult::Continue;
  183. }
  184. ALWAYS_INLINE ExecutionResult OpCode_FailForks::execute(MatchInput const& input, MatchState& state) const
  185. {
  186. input.fail_counter += state.forks_since_last_save;
  187. return ExecutionResult::Failed_ExecuteLowPrioForks;
  188. }
  189. ALWAYS_INLINE ExecutionResult OpCode_Jump::execute(MatchInput const&, MatchState& state) const
  190. {
  191. state.instruction_position += offset();
  192. return ExecutionResult::Continue;
  193. }
  194. ALWAYS_INLINE ExecutionResult OpCode_ForkJump::execute(MatchInput const&, MatchState& state) const
  195. {
  196. state.fork_at_position = state.instruction_position + size() + offset();
  197. state.forks_since_last_save++;
  198. return ExecutionResult::Fork_PrioHigh;
  199. }
  200. ALWAYS_INLINE ExecutionResult OpCode_ForkReplaceJump::execute(MatchInput const& input, MatchState& state) const
  201. {
  202. state.fork_at_position = state.instruction_position + size() + offset();
  203. input.fork_to_replace = state.instruction_position;
  204. state.forks_since_last_save++;
  205. return ExecutionResult::Fork_PrioHigh;
  206. }
  207. ALWAYS_INLINE ExecutionResult OpCode_ForkStay::execute(MatchInput const&, MatchState& state) const
  208. {
  209. state.fork_at_position = state.instruction_position + size() + offset();
  210. state.forks_since_last_save++;
  211. return ExecutionResult::Fork_PrioLow;
  212. }
  213. ALWAYS_INLINE ExecutionResult OpCode_ForkReplaceStay::execute(MatchInput const& input, MatchState& state) const
  214. {
  215. state.fork_at_position = state.instruction_position + size() + offset();
  216. input.fork_to_replace = state.instruction_position;
  217. return ExecutionResult::Fork_PrioLow;
  218. }
  219. ALWAYS_INLINE ExecutionResult OpCode_CheckBegin::execute(MatchInput const& input, MatchState& state) const
  220. {
  221. auto is_at_line_boundary = [&] {
  222. if (state.string_position == 0)
  223. return true;
  224. if (input.regex_options.has_flag_set(AllFlags::Multiline) && input.regex_options.has_flag_set(AllFlags::Internal_ConsiderNewline)) {
  225. auto input_view = input.view.substring_view(state.string_position - 1, 1)[0];
  226. return input_view == '\r' || input_view == '\n' || input_view == LineSeparator || input_view == ParagraphSeparator;
  227. }
  228. return false;
  229. }();
  230. if (is_at_line_boundary && (input.regex_options & AllFlags::MatchNotBeginOfLine))
  231. return ExecutionResult::Failed_ExecuteLowPrioForks;
  232. if ((is_at_line_boundary && !(input.regex_options & AllFlags::MatchNotBeginOfLine))
  233. || (!is_at_line_boundary && (input.regex_options & AllFlags::MatchNotBeginOfLine))
  234. || (is_at_line_boundary && (input.regex_options & AllFlags::Global)))
  235. return ExecutionResult::Continue;
  236. return ExecutionResult::Failed_ExecuteLowPrioForks;
  237. }
  238. ALWAYS_INLINE ExecutionResult OpCode_CheckBoundary::execute(MatchInput const& input, MatchState& state) const
  239. {
  240. auto isword = [](auto ch) { return is_ascii_alphanumeric(ch) || ch == '_'; };
  241. auto is_word_boundary = [&] {
  242. if (state.string_position == input.view.length()) {
  243. return (state.string_position > 0 && isword(input.view[state.string_position_in_code_units - 1]));
  244. }
  245. if (state.string_position == 0) {
  246. return (isword(input.view[0]));
  247. }
  248. return !!(isword(input.view[state.string_position_in_code_units]) ^ isword(input.view[state.string_position_in_code_units - 1]));
  249. };
  250. switch (type()) {
  251. case BoundaryCheckType::Word: {
  252. if (is_word_boundary())
  253. return ExecutionResult::Continue;
  254. return ExecutionResult::Failed_ExecuteLowPrioForks;
  255. }
  256. case BoundaryCheckType::NonWord: {
  257. if (!is_word_boundary())
  258. return ExecutionResult::Continue;
  259. return ExecutionResult::Failed_ExecuteLowPrioForks;
  260. }
  261. }
  262. VERIFY_NOT_REACHED();
  263. }
  264. ALWAYS_INLINE ExecutionResult OpCode_CheckEnd::execute(MatchInput const& input, MatchState& state) const
  265. {
  266. auto is_at_line_boundary = [&] {
  267. if (state.string_position == input.view.length())
  268. return true;
  269. if (input.regex_options.has_flag_set(AllFlags::Multiline) && input.regex_options.has_flag_set(AllFlags::Internal_ConsiderNewline)) {
  270. auto input_view = input.view.substring_view(state.string_position, 1)[0];
  271. return input_view == '\r' || input_view == '\n' || input_view == LineSeparator || input_view == ParagraphSeparator;
  272. }
  273. return false;
  274. }();
  275. if (is_at_line_boundary && (input.regex_options & AllFlags::MatchNotEndOfLine))
  276. return ExecutionResult::Failed_ExecuteLowPrioForks;
  277. if ((is_at_line_boundary && !(input.regex_options & AllFlags::MatchNotEndOfLine))
  278. || (!is_at_line_boundary && (input.regex_options & AllFlags::MatchNotEndOfLine || input.regex_options & AllFlags::MatchNotBeginOfLine)))
  279. return ExecutionResult::Continue;
  280. return ExecutionResult::Failed_ExecuteLowPrioForks;
  281. }
  282. ALWAYS_INLINE ExecutionResult OpCode_ClearCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  283. {
  284. if (input.match_index < state.capture_group_matches.size()) {
  285. auto& group = state.capture_group_matches[input.match_index];
  286. auto group_id = id();
  287. if (group_id >= group.size())
  288. group.resize(group_id + 1);
  289. group[group_id].reset();
  290. }
  291. return ExecutionResult::Continue;
  292. }
  293. ALWAYS_INLINE ExecutionResult OpCode_SaveLeftCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  294. {
  295. if (input.match_index >= state.capture_group_matches.size()) {
  296. state.capture_group_matches.ensure_capacity(input.match_index);
  297. auto capacity = state.capture_group_matches.capacity();
  298. for (size_t i = state.capture_group_matches.size(); i <= capacity; ++i)
  299. state.capture_group_matches.empend();
  300. }
  301. if (id() >= state.capture_group_matches.at(input.match_index).size()) {
  302. state.capture_group_matches.at(input.match_index).ensure_capacity(id());
  303. auto capacity = state.capture_group_matches.at(input.match_index).capacity();
  304. for (size_t i = state.capture_group_matches.at(input.match_index).size(); i <= capacity; ++i)
  305. state.capture_group_matches.at(input.match_index).empend();
  306. }
  307. state.capture_group_matches.at(input.match_index).at(id()).left_column = state.string_position;
  308. return ExecutionResult::Continue;
  309. }
  310. ALWAYS_INLINE ExecutionResult OpCode_SaveRightCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  311. {
  312. auto& match = state.capture_group_matches.at(input.match_index).at(id());
  313. auto start_position = match.left_column;
  314. if (state.string_position < start_position) {
  315. dbgln("Right capture group {} is before left capture group {}!", state.string_position, start_position);
  316. return ExecutionResult::Failed_ExecuteLowPrioForks;
  317. }
  318. auto length = state.string_position - start_position;
  319. if (start_position < match.column)
  320. return ExecutionResult::Continue;
  321. VERIFY(start_position + length <= input.view.length());
  322. auto view = input.view.substring_view(start_position, length);
  323. if (input.regex_options & AllFlags::StringCopyMatches) {
  324. match = { view.to_deprecated_string(), input.line, start_position, input.global_offset + start_position }; // create a copy of the original string
  325. } else {
  326. match = { view, input.line, start_position, input.global_offset + start_position }; // take view to original string
  327. }
  328. return ExecutionResult::Continue;
  329. }
  330. ALWAYS_INLINE ExecutionResult OpCode_SaveRightNamedCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  331. {
  332. auto& match = state.capture_group_matches.at(input.match_index).at(id());
  333. auto start_position = match.left_column;
  334. if (state.string_position < start_position)
  335. return ExecutionResult::Failed_ExecuteLowPrioForks;
  336. auto length = state.string_position - start_position;
  337. if (start_position < match.column)
  338. return ExecutionResult::Continue;
  339. VERIFY(start_position + length <= input.view.length());
  340. auto view = input.view.substring_view(start_position, length);
  341. if (input.regex_options & AllFlags::StringCopyMatches) {
  342. match = { view.to_deprecated_string(), name(), input.line, start_position, input.global_offset + start_position }; // create a copy of the original string
  343. } else {
  344. match = { view, name(), input.line, start_position, input.global_offset + start_position }; // take view to original string
  345. }
  346. return ExecutionResult::Continue;
  347. }
  348. ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, MatchState& state) const
  349. {
  350. auto argument_count = arguments_count();
  351. auto has_single_argument = argument_count == 1;
  352. bool inverse { false };
  353. bool temporary_inverse { false };
  354. bool reset_temp_inverse { false };
  355. struct DisjunctionState {
  356. bool active { false };
  357. bool is_conjunction { false };
  358. bool fail { false };
  359. size_t initial_position;
  360. size_t initial_code_unit_position;
  361. Optional<size_t> last_accepted_position {};
  362. Optional<size_t> last_accepted_code_unit_position {};
  363. };
  364. Vector<DisjunctionState, 4> disjunction_states;
  365. disjunction_states.empend();
  366. auto current_disjunction_state = [&]() -> DisjunctionState& { return disjunction_states.last(); };
  367. auto current_inversion_state = [&]() -> bool { return temporary_inverse ^ inverse; };
  368. size_t string_position = state.string_position;
  369. bool inverse_matched { false };
  370. bool had_zero_length_match { false };
  371. state.string_position_before_match = state.string_position;
  372. size_t offset { state.instruction_position + 3 };
  373. for (size_t i = 0; i < argument_count; ++i) {
  374. if (state.string_position > string_position)
  375. break;
  376. if (reset_temp_inverse) {
  377. reset_temp_inverse = false;
  378. temporary_inverse = false;
  379. } else {
  380. reset_temp_inverse = true;
  381. }
  382. auto compare_type = (CharacterCompareType)m_bytecode->at(offset++);
  383. if (compare_type == CharacterCompareType::Inverse) {
  384. inverse = !inverse;
  385. continue;
  386. } else if (compare_type == CharacterCompareType::TemporaryInverse) {
  387. // If "TemporaryInverse" is given, negate the current inversion state only for the next opcode.
  388. // it follows that this cannot be the last compare element.
  389. VERIFY(i != arguments_count() - 1);
  390. temporary_inverse = true;
  391. reset_temp_inverse = false;
  392. continue;
  393. } else if (compare_type == CharacterCompareType::Char) {
  394. u32 ch = m_bytecode->at(offset++);
  395. // We want to compare a string that is longer or equal in length to the available string
  396. if (input.view.length() <= state.string_position)
  397. return ExecutionResult::Failed_ExecuteLowPrioForks;
  398. compare_char(input, state, ch, current_inversion_state(), inverse_matched);
  399. } else if (compare_type == CharacterCompareType::AnyChar) {
  400. // We want to compare a string that is definitely longer than the available string
  401. if (input.view.length() <= state.string_position)
  402. return ExecutionResult::Failed_ExecuteLowPrioForks;
  403. auto input_view = input.view.substring_view(state.string_position, 1)[0];
  404. auto is_equivalent_to_newline = input_view == '\n'
  405. || (input.regex_options.has_flag_set(AllFlags::Internal_ECMA262DotSemantics)
  406. ? (input_view == '\r' || input_view == LineSeparator || input_view == ParagraphSeparator)
  407. : false);
  408. if (!is_equivalent_to_newline || (input.regex_options.has_flag_set(AllFlags::SingleLine) && input.regex_options.has_flag_set(AllFlags::Internal_ConsiderNewline))) {
  409. if (current_inversion_state())
  410. inverse_matched = true;
  411. else
  412. advance_string_position(state, input.view, input_view);
  413. }
  414. } else if (compare_type == CharacterCompareType::String) {
  415. VERIFY(!current_inversion_state());
  416. auto const& length = m_bytecode->at(offset++);
  417. // We want to compare a string that is definitely longer than the available string
  418. if (input.view.length() < state.string_position + length)
  419. return ExecutionResult::Failed_ExecuteLowPrioForks;
  420. Optional<DeprecatedString> str;
  421. Utf16Data utf16;
  422. Vector<u32> data;
  423. data.ensure_capacity(length);
  424. for (size_t i = offset; i < offset + length; ++i)
  425. data.unchecked_append(m_bytecode->at(i));
  426. auto view = input.view.construct_as_same(data, str, utf16);
  427. offset += length;
  428. if (compare_string(input, state, view, had_zero_length_match)) {
  429. if (current_inversion_state())
  430. inverse_matched = true;
  431. }
  432. } else if (compare_type == CharacterCompareType::CharClass) {
  433. if (input.view.length() <= state.string_position_in_code_units)
  434. return ExecutionResult::Failed_ExecuteLowPrioForks;
  435. auto character_class = (CharClass)m_bytecode->at(offset++);
  436. auto ch = input.view[state.string_position_in_code_units];
  437. compare_character_class(input, state, character_class, ch, current_inversion_state(), inverse_matched);
  438. } else if (compare_type == CharacterCompareType::LookupTable) {
  439. if (input.view.length() <= state.string_position)
  440. return ExecutionResult::Failed_ExecuteLowPrioForks;
  441. auto count = m_bytecode->at(offset++);
  442. auto range_data = m_bytecode->template spans<4>().slice(offset, count);
  443. offset += count;
  444. auto ch = input.view[state.string_position_in_code_units];
  445. auto const* matching_range = binary_search(range_data, ch, nullptr, [insensitive = input.regex_options & AllFlags::Insensitive](auto needle, CharRange range) {
  446. auto upper_case_needle = needle;
  447. auto lower_case_needle = needle;
  448. if (insensitive) {
  449. upper_case_needle = to_ascii_uppercase(needle);
  450. lower_case_needle = to_ascii_lowercase(needle);
  451. }
  452. if (lower_case_needle >= range.from && lower_case_needle <= range.to)
  453. return 0;
  454. if (upper_case_needle >= range.from && upper_case_needle <= range.to)
  455. return 0;
  456. if (lower_case_needle > range.to || upper_case_needle > range.to)
  457. return 1;
  458. return -1;
  459. });
  460. if (matching_range) {
  461. if (current_inversion_state())
  462. inverse_matched = true;
  463. else
  464. advance_string_position(state, input.view, ch);
  465. }
  466. } else if (compare_type == CharacterCompareType::CharRange) {
  467. if (input.view.length() <= state.string_position)
  468. return ExecutionResult::Failed_ExecuteLowPrioForks;
  469. auto value = (CharRange)m_bytecode->at(offset++);
  470. auto from = value.from;
  471. auto to = value.to;
  472. auto ch = input.view[state.string_position_in_code_units];
  473. compare_character_range(input, state, from, to, ch, current_inversion_state(), inverse_matched);
  474. } else if (compare_type == CharacterCompareType::Reference) {
  475. auto reference_number = (size_t)m_bytecode->at(offset++);
  476. auto& groups = state.capture_group_matches.at(input.match_index);
  477. if (groups.size() <= reference_number)
  478. return ExecutionResult::Failed_ExecuteLowPrioForks;
  479. auto str = groups.at(reference_number).view;
  480. // We want to compare a string that is definitely longer than the available string
  481. if (input.view.length() < state.string_position + str.length())
  482. return ExecutionResult::Failed_ExecuteLowPrioForks;
  483. if (compare_string(input, state, str, had_zero_length_match)) {
  484. if (current_inversion_state())
  485. inverse_matched = true;
  486. }
  487. } else if (compare_type == CharacterCompareType::Property) {
  488. auto property = static_cast<Unicode::Property>(m_bytecode->at(offset++));
  489. compare_property(input, state, property, current_inversion_state(), inverse_matched);
  490. } else if (compare_type == CharacterCompareType::GeneralCategory) {
  491. auto general_category = static_cast<Unicode::GeneralCategory>(m_bytecode->at(offset++));
  492. compare_general_category(input, state, general_category, current_inversion_state(), inverse_matched);
  493. } else if (compare_type == CharacterCompareType::Script) {
  494. auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
  495. compare_script(input, state, script, current_inversion_state(), inverse_matched);
  496. } else if (compare_type == CharacterCompareType::ScriptExtension) {
  497. auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
  498. compare_script_extension(input, state, script, current_inversion_state(), inverse_matched);
  499. } else if (compare_type == CharacterCompareType::And) {
  500. disjunction_states.append({
  501. .active = true,
  502. .is_conjunction = false,
  503. .fail = false,
  504. .initial_position = state.string_position,
  505. .initial_code_unit_position = state.string_position_in_code_units,
  506. });
  507. continue;
  508. } else if (compare_type == CharacterCompareType::Or) {
  509. disjunction_states.append({
  510. .active = true,
  511. .is_conjunction = true,
  512. .fail = true,
  513. .initial_position = state.string_position,
  514. .initial_code_unit_position = state.string_position_in_code_units,
  515. });
  516. continue;
  517. } else if (compare_type == CharacterCompareType::EndAndOr) {
  518. auto disjunction_state = disjunction_states.take_last();
  519. if (!disjunction_state.fail) {
  520. state.string_position = disjunction_state.last_accepted_position.value_or(disjunction_state.initial_position);
  521. state.string_position_in_code_units = disjunction_state.last_accepted_code_unit_position.value_or(disjunction_state.initial_code_unit_position);
  522. }
  523. } else {
  524. warnln("Undefined comparison: {}", (int)compare_type);
  525. VERIFY_NOT_REACHED();
  526. break;
  527. }
  528. auto& new_disjunction_state = current_disjunction_state();
  529. if (current_inversion_state() && (!inverse || new_disjunction_state.active) && !inverse_matched) {
  530. advance_string_position(state, input.view);
  531. inverse_matched = true;
  532. }
  533. if (!has_single_argument && new_disjunction_state.active) {
  534. auto failed = (!had_zero_length_match && string_position == state.string_position) || state.string_position > input.view.length();
  535. if (!failed) {
  536. new_disjunction_state.last_accepted_position = state.string_position;
  537. new_disjunction_state.last_accepted_code_unit_position = state.string_position_in_code_units;
  538. }
  539. if (new_disjunction_state.is_conjunction)
  540. new_disjunction_state.fail = failed && new_disjunction_state.fail;
  541. else
  542. new_disjunction_state.fail = failed || new_disjunction_state.fail;
  543. state.string_position = new_disjunction_state.initial_position;
  544. state.string_position_in_code_units = new_disjunction_state.initial_code_unit_position;
  545. }
  546. }
  547. if (!has_single_argument) {
  548. auto& new_disjunction_state = current_disjunction_state();
  549. if (new_disjunction_state.active) {
  550. if (!new_disjunction_state.fail) {
  551. state.string_position = new_disjunction_state.last_accepted_position.value_or(new_disjunction_state.initial_position);
  552. state.string_position_in_code_units = new_disjunction_state.last_accepted_code_unit_position.value_or(new_disjunction_state.initial_code_unit_position);
  553. }
  554. }
  555. }
  556. if (current_inversion_state() && !inverse_matched)
  557. advance_string_position(state, input.view);
  558. if ((!had_zero_length_match && string_position == state.string_position) || state.string_position > input.view.length())
  559. return ExecutionResult::Failed_ExecuteLowPrioForks;
  560. return ExecutionResult::Continue;
  561. }
  562. ALWAYS_INLINE void OpCode_Compare::compare_char(MatchInput const& input, MatchState& state, u32 ch1, bool inverse, bool& inverse_matched)
  563. {
  564. if (state.string_position == input.view.length())
  565. return;
  566. // FIXME: Figure out how to do this if unicode() without performing a substring split first.
  567. auto input_view = input.view.unicode() ? input.view.substring_view(state.string_position, 1)[0] : input.view.code_unit_at(state.string_position_in_code_units);
  568. bool equal;
  569. if (input.regex_options & AllFlags::Insensitive)
  570. equal = to_ascii_lowercase(input_view) == to_ascii_lowercase(ch1); // FIXME: Implement case-insensitive matching for non-ascii characters
  571. else
  572. equal = input_view == ch1;
  573. if (equal) {
  574. if (inverse)
  575. inverse_matched = true;
  576. else
  577. advance_string_position(state, input.view, ch1);
  578. }
  579. }
  580. ALWAYS_INLINE bool OpCode_Compare::compare_string(MatchInput const& input, MatchState& state, RegexStringView str, bool& had_zero_length_match)
  581. {
  582. if (state.string_position + str.length() > input.view.length()) {
  583. if (str.is_empty()) {
  584. had_zero_length_match = true;
  585. return true;
  586. }
  587. return false;
  588. }
  589. if (str.length() == 0) {
  590. had_zero_length_match = true;
  591. return true;
  592. }
  593. if (str.length() == 1) {
  594. auto inverse_matched = false;
  595. compare_char(input, state, str[0], false, inverse_matched);
  596. return !inverse_matched;
  597. }
  598. auto subject = input.view.substring_view(state.string_position, str.length());
  599. bool equals;
  600. if (input.regex_options & AllFlags::Insensitive)
  601. equals = subject.equals_ignoring_case(str);
  602. else
  603. equals = subject.equals(str);
  604. if (equals)
  605. advance_string_position(state, input.view, str);
  606. return equals;
  607. }
  608. ALWAYS_INLINE void OpCode_Compare::compare_character_class(MatchInput const& input, MatchState& state, CharClass character_class, u32 ch, bool inverse, bool& inverse_matched)
  609. {
  610. if (matches_character_class(character_class, ch, input.regex_options & AllFlags::Insensitive)) {
  611. if (inverse)
  612. inverse_matched = true;
  613. else
  614. advance_string_position(state, input.view, ch);
  615. }
  616. }
  617. bool OpCode_Compare::matches_character_class(CharClass character_class, u32 ch, bool insensitive)
  618. {
  619. constexpr auto is_space_or_line_terminator = [](u32 code_point) {
  620. static auto space_separator = Unicode::general_category_from_string("Space_Separator"sv);
  621. if (!space_separator.has_value())
  622. return is_ascii_space(code_point);
  623. if ((code_point == 0x0a) || (code_point == 0x0d) || (code_point == 0x2028) || (code_point == 0x2029))
  624. return true;
  625. if ((code_point == 0x09) || (code_point == 0x0b) || (code_point == 0x0c) || (code_point == 0xfeff))
  626. return true;
  627. return Unicode::code_point_has_general_category(code_point, *space_separator);
  628. };
  629. switch (character_class) {
  630. case CharClass::Alnum:
  631. return is_ascii_alphanumeric(ch);
  632. case CharClass::Alpha:
  633. return is_ascii_alpha(ch);
  634. case CharClass::Blank:
  635. return is_ascii_blank(ch);
  636. case CharClass::Cntrl:
  637. return is_ascii_control(ch);
  638. case CharClass::Digit:
  639. return is_ascii_digit(ch);
  640. case CharClass::Graph:
  641. return is_ascii_graphical(ch);
  642. case CharClass::Lower:
  643. return is_ascii_lower_alpha(ch) || (insensitive && is_ascii_upper_alpha(ch));
  644. case CharClass::Print:
  645. return is_ascii_printable(ch);
  646. case CharClass::Punct:
  647. return is_ascii_punctuation(ch);
  648. case CharClass::Space:
  649. return is_space_or_line_terminator(ch);
  650. case CharClass::Upper:
  651. return is_ascii_upper_alpha(ch) || (insensitive && is_ascii_lower_alpha(ch));
  652. case CharClass::Word:
  653. return is_ascii_alphanumeric(ch) || ch == '_';
  654. case CharClass::Xdigit:
  655. return is_ascii_hex_digit(ch);
  656. }
  657. VERIFY_NOT_REACHED();
  658. }
  659. ALWAYS_INLINE void OpCode_Compare::compare_character_range(MatchInput const& input, MatchState& state, u32 from, u32 to, u32 ch, bool inverse, bool& inverse_matched)
  660. {
  661. if (input.regex_options & AllFlags::Insensitive) {
  662. from = to_ascii_lowercase(from);
  663. to = to_ascii_lowercase(to);
  664. ch = to_ascii_lowercase(ch);
  665. }
  666. if (ch >= from && ch <= to) {
  667. if (inverse)
  668. inverse_matched = true;
  669. else
  670. advance_string_position(state, input.view, ch);
  671. }
  672. }
  673. ALWAYS_INLINE void OpCode_Compare::compare_property(MatchInput const& input, MatchState& state, Unicode::Property property, bool inverse, bool& inverse_matched)
  674. {
  675. if (state.string_position == input.view.length())
  676. return;
  677. u32 code_point = input.view[state.string_position_in_code_units];
  678. bool equal = Unicode::code_point_has_property(code_point, property);
  679. if (equal) {
  680. if (inverse)
  681. inverse_matched = true;
  682. else
  683. advance_string_position(state, input.view, code_point);
  684. }
  685. }
  686. ALWAYS_INLINE void OpCode_Compare::compare_general_category(MatchInput const& input, MatchState& state, Unicode::GeneralCategory general_category, bool inverse, bool& inverse_matched)
  687. {
  688. if (state.string_position == input.view.length())
  689. return;
  690. u32 code_point = input.view[state.string_position_in_code_units];
  691. bool equal = Unicode::code_point_has_general_category(code_point, general_category);
  692. if (equal) {
  693. if (inverse)
  694. inverse_matched = true;
  695. else
  696. advance_string_position(state, input.view, code_point);
  697. }
  698. }
  699. ALWAYS_INLINE void OpCode_Compare::compare_script(MatchInput const& input, MatchState& state, Unicode::Script script, bool inverse, bool& inverse_matched)
  700. {
  701. if (state.string_position == input.view.length())
  702. return;
  703. u32 code_point = input.view[state.string_position_in_code_units];
  704. bool equal = Unicode::code_point_has_script(code_point, script);
  705. if (equal) {
  706. if (inverse)
  707. inverse_matched = true;
  708. else
  709. advance_string_position(state, input.view, code_point);
  710. }
  711. }
  712. ALWAYS_INLINE void OpCode_Compare::compare_script_extension(MatchInput const& input, MatchState& state, Unicode::Script script, bool inverse, bool& inverse_matched)
  713. {
  714. if (state.string_position == input.view.length())
  715. return;
  716. u32 code_point = input.view[state.string_position_in_code_units];
  717. bool equal = Unicode::code_point_has_script_extension(code_point, script);
  718. if (equal) {
  719. if (inverse)
  720. inverse_matched = true;
  721. else
  722. advance_string_position(state, input.view, code_point);
  723. }
  724. }
  725. DeprecatedString OpCode_Compare::arguments_string() const
  726. {
  727. return DeprecatedString::formatted("argc={}, args={} ", arguments_count(), arguments_size());
  728. }
  729. Vector<CompareTypeAndValuePair> OpCode_Compare::flat_compares() const
  730. {
  731. Vector<CompareTypeAndValuePair> result;
  732. size_t offset { state().instruction_position + 3 };
  733. for (size_t i = 0; i < arguments_count(); ++i) {
  734. auto compare_type = (CharacterCompareType)m_bytecode->at(offset++);
  735. if (compare_type == CharacterCompareType::Char) {
  736. auto ch = m_bytecode->at(offset++);
  737. result.append({ compare_type, ch });
  738. } else if (compare_type == CharacterCompareType::Reference) {
  739. auto ref = m_bytecode->at(offset++);
  740. result.append({ compare_type, ref });
  741. } else if (compare_type == CharacterCompareType::String) {
  742. auto& length = m_bytecode->at(offset++);
  743. if (length > 0)
  744. result.append({ compare_type, m_bytecode->at(offset) });
  745. StringBuilder str_builder;
  746. offset += length;
  747. } else if (compare_type == CharacterCompareType::CharClass) {
  748. auto character_class = m_bytecode->at(offset++);
  749. result.append({ compare_type, character_class });
  750. } else if (compare_type == CharacterCompareType::CharRange) {
  751. auto value = m_bytecode->at(offset++);
  752. result.append({ compare_type, value });
  753. } else if (compare_type == CharacterCompareType::LookupTable) {
  754. auto count = m_bytecode->at(offset++);
  755. for (size_t i = 0; i < count; ++i)
  756. result.append({ CharacterCompareType::CharRange, m_bytecode->at(offset++) });
  757. } else if (compare_type == CharacterCompareType::GeneralCategory
  758. || compare_type == CharacterCompareType::Property
  759. || compare_type == CharacterCompareType::Script
  760. || compare_type == CharacterCompareType::ScriptExtension) {
  761. auto value = m_bytecode->at(offset++);
  762. result.append({ compare_type, value });
  763. } else {
  764. result.append({ compare_type, 0 });
  765. }
  766. }
  767. return result;
  768. }
  769. Vector<DeprecatedString> OpCode_Compare::variable_arguments_to_deprecated_string(Optional<MatchInput const&> input) const
  770. {
  771. Vector<DeprecatedString> result;
  772. size_t offset { state().instruction_position + 3 };
  773. RegexStringView const& view = ((input.has_value()) ? input.value().view : StringView {});
  774. for (size_t i = 0; i < arguments_count(); ++i) {
  775. auto compare_type = (CharacterCompareType)m_bytecode->at(offset++);
  776. result.empend(DeprecatedString::formatted("type={} [{}]", (size_t)compare_type, character_compare_type_name(compare_type)));
  777. auto string_start_offset = state().string_position_before_match;
  778. if (compare_type == CharacterCompareType::Char) {
  779. auto ch = m_bytecode->at(offset++);
  780. auto is_ascii = is_ascii_printable(ch);
  781. if (is_ascii)
  782. result.empend(DeprecatedString::formatted(" value='{:c}'", static_cast<char>(ch)));
  783. else
  784. result.empend(DeprecatedString::formatted(" value={:x}", ch));
  785. if (!view.is_null() && view.length() > string_start_offset) {
  786. if (is_ascii) {
  787. result.empend(DeprecatedString::formatted(
  788. " compare against: '{}'",
  789. view.substring_view(string_start_offset, string_start_offset > view.length() ? 0 : 1).to_deprecated_string()));
  790. } else {
  791. auto str = view.substring_view(string_start_offset, string_start_offset > view.length() ? 0 : 1).to_deprecated_string();
  792. u8 buf[8] { 0 };
  793. __builtin_memcpy(buf, str.characters(), min(str.length(), sizeof(buf)));
  794. result.empend(DeprecatedString::formatted(" compare against: {:x},{:x},{:x},{:x},{:x},{:x},{:x},{:x}",
  795. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]));
  796. }
  797. }
  798. } else if (compare_type == CharacterCompareType::Reference) {
  799. auto ref = m_bytecode->at(offset++);
  800. result.empend(DeprecatedString::formatted(" number={}", ref));
  801. if (input.has_value()) {
  802. if (state().capture_group_matches.size() > input->match_index) {
  803. auto& match = state().capture_group_matches[input->match_index];
  804. if (match.size() > ref) {
  805. auto& group = match[ref];
  806. result.empend(DeprecatedString::formatted(" left={}", group.left_column));
  807. result.empend(DeprecatedString::formatted(" right={}", group.left_column + group.view.length_in_code_units()));
  808. result.empend(DeprecatedString::formatted(" contents='{}'", group.view));
  809. } else {
  810. result.empend(DeprecatedString::formatted(" (invalid ref, max={})", match.size() - 1));
  811. }
  812. } else {
  813. result.empend(DeprecatedString::formatted(" (invalid index {}, max={})", input->match_index, state().capture_group_matches.size() - 1));
  814. }
  815. }
  816. } else if (compare_type == CharacterCompareType::String) {
  817. auto& length = m_bytecode->at(offset++);
  818. StringBuilder str_builder;
  819. for (size_t i = 0; i < length; ++i)
  820. str_builder.append(m_bytecode->at(offset++));
  821. result.empend(DeprecatedString::formatted(" value=\"{}\"", str_builder.string_view().substring_view(0, length)));
  822. if (!view.is_null() && view.length() > state().string_position)
  823. result.empend(DeprecatedString::formatted(
  824. " compare against: \"{}\"",
  825. input.value().view.substring_view(string_start_offset, string_start_offset + length > view.length() ? 0 : length).to_deprecated_string()));
  826. } else if (compare_type == CharacterCompareType::CharClass) {
  827. auto character_class = (CharClass)m_bytecode->at(offset++);
  828. result.empend(DeprecatedString::formatted(" ch_class={} [{}]", (size_t)character_class, character_class_name(character_class)));
  829. if (!view.is_null() && view.length() > state().string_position)
  830. result.empend(DeprecatedString::formatted(
  831. " compare against: '{}'",
  832. input.value().view.substring_view(string_start_offset, state().string_position > view.length() ? 0 : 1).to_deprecated_string()));
  833. } else if (compare_type == CharacterCompareType::CharRange) {
  834. auto value = (CharRange)m_bytecode->at(offset++);
  835. result.empend(DeprecatedString::formatted(" ch_range={:x}-{:x}", value.from, value.to));
  836. if (!view.is_null() && view.length() > state().string_position)
  837. result.empend(DeprecatedString::formatted(
  838. " compare against: '{}'",
  839. input.value().view.substring_view(string_start_offset, state().string_position > view.length() ? 0 : 1).to_deprecated_string()));
  840. } else if (compare_type == CharacterCompareType::LookupTable) {
  841. auto count = m_bytecode->at(offset++);
  842. for (size_t j = 0; j < count; ++j) {
  843. auto range = (CharRange)m_bytecode->at(offset++);
  844. result.append(DeprecatedString::formatted(" {:x}-{:x}", range.from, range.to));
  845. }
  846. if (!view.is_null() && view.length() > state().string_position)
  847. result.empend(DeprecatedString::formatted(
  848. " compare against: '{}'",
  849. input.value().view.substring_view(string_start_offset, state().string_position > view.length() ? 0 : 1).to_deprecated_string()));
  850. } else if (compare_type == CharacterCompareType::GeneralCategory
  851. || compare_type == CharacterCompareType::Property
  852. || compare_type == CharacterCompareType::Script
  853. || compare_type == CharacterCompareType::ScriptExtension) {
  854. auto value = m_bytecode->at(offset++);
  855. result.empend(DeprecatedString::formatted(" value={}", value));
  856. }
  857. }
  858. return result;
  859. }
  860. ALWAYS_INLINE ExecutionResult OpCode_Repeat::execute(MatchInput const&, MatchState& state) const
  861. {
  862. VERIFY(count() > 0);
  863. if (id() >= state.repetition_marks.size())
  864. state.repetition_marks.resize(id() + 1);
  865. auto& repetition_mark = state.repetition_marks.at(id());
  866. if (repetition_mark == count() - 1) {
  867. repetition_mark = 0;
  868. } else {
  869. state.instruction_position -= offset() + size();
  870. ++repetition_mark;
  871. }
  872. return ExecutionResult::Continue;
  873. }
  874. ALWAYS_INLINE ExecutionResult OpCode_ResetRepeat::execute(MatchInput const&, MatchState& state) const
  875. {
  876. if (id() >= state.repetition_marks.size())
  877. state.repetition_marks.resize(id() + 1);
  878. state.repetition_marks.at(id()) = 0;
  879. return ExecutionResult::Continue;
  880. }
  881. ALWAYS_INLINE ExecutionResult OpCode_Checkpoint::execute(MatchInput const& input, MatchState& state) const
  882. {
  883. auto id = this->id();
  884. if (id >= input.checkpoints.size())
  885. input.checkpoints.resize(id + 1);
  886. input.checkpoints[id] = state.string_position + 1;
  887. return ExecutionResult::Continue;
  888. }
  889. ALWAYS_INLINE ExecutionResult OpCode_JumpNonEmpty::execute(MatchInput const& input, MatchState& state) const
  890. {
  891. u64 current_position = state.string_position;
  892. auto checkpoint_position = input.checkpoints[checkpoint()];
  893. if (checkpoint_position != 0 && checkpoint_position != current_position + 1) {
  894. auto form = this->form();
  895. if (form == OpCodeId::Jump) {
  896. state.instruction_position += offset();
  897. return ExecutionResult::Continue;
  898. }
  899. state.fork_at_position = state.instruction_position + size() + offset();
  900. if (form == OpCodeId::ForkJump) {
  901. state.forks_since_last_save++;
  902. return ExecutionResult::Fork_PrioHigh;
  903. }
  904. if (form == OpCodeId::ForkStay) {
  905. state.forks_since_last_save++;
  906. return ExecutionResult::Fork_PrioLow;
  907. }
  908. if (form == OpCodeId::ForkReplaceStay) {
  909. input.fork_to_replace = state.instruction_position;
  910. return ExecutionResult::Fork_PrioLow;
  911. }
  912. if (form == OpCodeId::ForkReplaceJump) {
  913. input.fork_to_replace = state.instruction_position;
  914. return ExecutionResult::Fork_PrioHigh;
  915. }
  916. }
  917. return ExecutionResult::Continue;
  918. }
  919. }