RegexByteCode.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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/Debug.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibUnicode/CharacterTypes.h>
  13. namespace regex {
  14. StringView OpCode::name(OpCodeId opcode_id)
  15. {
  16. switch (opcode_id) {
  17. #define __ENUMERATE_OPCODE(x) \
  18. case OpCodeId::x: \
  19. return #x##sv;
  20. ENUMERATE_OPCODES
  21. #undef __ENUMERATE_OPCODE
  22. default:
  23. VERIFY_NOT_REACHED();
  24. return "<Unknown>"sv;
  25. }
  26. }
  27. StringView OpCode::name() const
  28. {
  29. return name(opcode_id());
  30. }
  31. StringView execution_result_name(ExecutionResult result)
  32. {
  33. switch (result) {
  34. #define __ENUMERATE_EXECUTION_RESULT(x) \
  35. case ExecutionResult::x: \
  36. return #x##sv;
  37. ENUMERATE_EXECUTION_RESULTS
  38. #undef __ENUMERATE_EXECUTION_RESULT
  39. default:
  40. VERIFY_NOT_REACHED();
  41. return "<Unknown>"sv;
  42. }
  43. }
  44. StringView opcode_id_name(OpCodeId opcode)
  45. {
  46. switch (opcode) {
  47. #define __ENUMERATE_OPCODE(x) \
  48. case OpCodeId::x: \
  49. return #x##sv;
  50. ENUMERATE_OPCODES
  51. #undef __ENUMERATE_OPCODE
  52. default:
  53. VERIFY_NOT_REACHED();
  54. return "<Unknown>"sv;
  55. }
  56. }
  57. StringView boundary_check_type_name(BoundaryCheckType ty)
  58. {
  59. switch (ty) {
  60. #define __ENUMERATE_BOUNDARY_CHECK_TYPE(x) \
  61. case BoundaryCheckType::x: \
  62. return #x##sv;
  63. ENUMERATE_BOUNDARY_CHECK_TYPES
  64. #undef __ENUMERATE_BOUNDARY_CHECK_TYPE
  65. default:
  66. VERIFY_NOT_REACHED();
  67. return "<Unknown>"sv;
  68. }
  69. }
  70. StringView character_compare_type_name(CharacterCompareType ch_compare_type)
  71. {
  72. switch (ch_compare_type) {
  73. #define __ENUMERATE_CHARACTER_COMPARE_TYPE(x) \
  74. case CharacterCompareType::x: \
  75. return #x##sv;
  76. ENUMERATE_CHARACTER_COMPARE_TYPES
  77. #undef __ENUMERATE_CHARACTER_COMPARE_TYPE
  78. default:
  79. VERIFY_NOT_REACHED();
  80. return "<Unknown>"sv;
  81. }
  82. }
  83. static StringView character_class_name(CharClass ch_class)
  84. {
  85. switch (ch_class) {
  86. #define __ENUMERATE_CHARACTER_CLASS(x) \
  87. case CharClass::x: \
  88. return #x##sv;
  89. ENUMERATE_CHARACTER_CLASSES
  90. #undef __ENUMERATE_CHARACTER_CLASS
  91. default:
  92. VERIFY_NOT_REACHED();
  93. return "<Unknown>"sv;
  94. }
  95. }
  96. static void advance_string_position(MatchState& state, RegexStringView view, Optional<u32> code_point = {})
  97. {
  98. ++state.string_position;
  99. if (view.unicode()) {
  100. if (!code_point.has_value() && (state.string_position_in_code_units < view.length_in_code_units()))
  101. code_point = view[state.string_position_in_code_units];
  102. if (code_point.has_value())
  103. state.string_position_in_code_units += view.length_of_code_point(*code_point);
  104. } else {
  105. ++state.string_position_in_code_units;
  106. }
  107. }
  108. static void advance_string_position(MatchState& state, RegexStringView, RegexStringView advance_by)
  109. {
  110. state.string_position += advance_by.length();
  111. state.string_position_in_code_units += advance_by.length_in_code_units();
  112. }
  113. static void reverse_string_position(MatchState& state, RegexStringView view, size_t amount)
  114. {
  115. VERIFY(state.string_position >= amount);
  116. state.string_position -= amount;
  117. if (view.unicode())
  118. state.string_position_in_code_units = view.code_unit_offset_of(state.string_position);
  119. else
  120. state.string_position_in_code_units -= amount;
  121. }
  122. static void save_string_position(MatchInput const& input, MatchState const& state)
  123. {
  124. input.saved_positions.append(state.string_position);
  125. input.saved_forks_since_last_save.append(state.forks_since_last_save);
  126. input.saved_code_unit_positions.append(state.string_position_in_code_units);
  127. }
  128. static bool restore_string_position(MatchInput const& input, MatchState& state)
  129. {
  130. if (input.saved_positions.is_empty())
  131. return false;
  132. state.string_position = input.saved_positions.take_last();
  133. state.string_position_in_code_units = input.saved_code_unit_positions.take_last();
  134. state.forks_since_last_save = input.saved_forks_since_last_save.take_last();
  135. return true;
  136. }
  137. OwnPtr<OpCode> ByteCode::s_opcodes[(size_t)OpCodeId::Last + 1];
  138. bool ByteCode::s_opcodes_initialized { false };
  139. void ByteCode::ensure_opcodes_initialized()
  140. {
  141. if (s_opcodes_initialized)
  142. return;
  143. for (u32 i = (u32)OpCodeId::First; i <= (u32)OpCodeId::Last; ++i) {
  144. switch ((OpCodeId)i) {
  145. #define __ENUMERATE_OPCODE(OpCode) \
  146. case OpCodeId::OpCode: \
  147. s_opcodes[i] = make<OpCode_##OpCode>(); \
  148. break;
  149. ENUMERATE_OPCODES
  150. #undef __ENUMERATE_OPCODE
  151. }
  152. }
  153. s_opcodes_initialized = true;
  154. }
  155. ALWAYS_INLINE OpCode& ByteCode::get_opcode_by_id(OpCodeId id) const
  156. {
  157. VERIFY(id >= OpCodeId::First && id <= OpCodeId::Last);
  158. auto& opcode = s_opcodes[(u32)id];
  159. opcode->set_bytecode(*const_cast<ByteCode*>(this));
  160. return *opcode;
  161. }
  162. OpCode& ByteCode::get_opcode(MatchState& state) const
  163. {
  164. OpCodeId opcode_id;
  165. if (auto opcode_ptr = static_cast<DisjointChunks<ByteCodeValueType> const&>(*this).find(state.instruction_position))
  166. opcode_id = (OpCodeId)*opcode_ptr;
  167. else
  168. opcode_id = OpCodeId::Exit;
  169. auto& opcode = get_opcode_by_id(opcode_id);
  170. opcode.set_state(state);
  171. return opcode;
  172. }
  173. ALWAYS_INLINE ExecutionResult OpCode_Exit::execute(MatchInput const& input, MatchState& state) const
  174. {
  175. if (state.string_position > input.view.length() || state.instruction_position >= m_bytecode->size())
  176. return ExecutionResult::Succeeded;
  177. return ExecutionResult::Failed;
  178. }
  179. ALWAYS_INLINE ExecutionResult OpCode_Save::execute(MatchInput const& input, MatchState& state) const
  180. {
  181. save_string_position(input, state);
  182. state.forks_since_last_save = 0;
  183. return ExecutionResult::Continue;
  184. }
  185. ALWAYS_INLINE ExecutionResult OpCode_Restore::execute(MatchInput const& input, MatchState& state) const
  186. {
  187. if (!restore_string_position(input, state))
  188. return ExecutionResult::Failed;
  189. return ExecutionResult::Continue;
  190. }
  191. ALWAYS_INLINE ExecutionResult OpCode_GoBack::execute(MatchInput const& input, MatchState& state) const
  192. {
  193. if (count() > state.string_position)
  194. return ExecutionResult::Failed_ExecuteLowPrioForks;
  195. reverse_string_position(state, input.view, count());
  196. return ExecutionResult::Continue;
  197. }
  198. ALWAYS_INLINE ExecutionResult OpCode_FailForks::execute(MatchInput const& input, MatchState& state) const
  199. {
  200. input.fail_counter += state.forks_since_last_save;
  201. return ExecutionResult::Failed_ExecuteLowPrioForks;
  202. }
  203. ALWAYS_INLINE ExecutionResult OpCode_Jump::execute(MatchInput const&, MatchState& state) const
  204. {
  205. state.instruction_position += offset();
  206. return ExecutionResult::Continue;
  207. }
  208. ALWAYS_INLINE ExecutionResult OpCode_ForkJump::execute(MatchInput const&, MatchState& state) const
  209. {
  210. state.fork_at_position = state.instruction_position + size() + offset();
  211. state.forks_since_last_save++;
  212. return ExecutionResult::Fork_PrioHigh;
  213. }
  214. ALWAYS_INLINE ExecutionResult OpCode_ForkReplaceJump::execute(MatchInput const& input, MatchState& state) const
  215. {
  216. state.fork_at_position = state.instruction_position + size() + offset();
  217. input.fork_to_replace = state.instruction_position;
  218. state.forks_since_last_save++;
  219. return ExecutionResult::Fork_PrioHigh;
  220. }
  221. ALWAYS_INLINE ExecutionResult OpCode_ForkStay::execute(MatchInput const&, MatchState& state) const
  222. {
  223. state.fork_at_position = state.instruction_position + size() + offset();
  224. state.forks_since_last_save++;
  225. return ExecutionResult::Fork_PrioLow;
  226. }
  227. ALWAYS_INLINE ExecutionResult OpCode_ForkReplaceStay::execute(MatchInput const& input, MatchState& state) const
  228. {
  229. state.fork_at_position = state.instruction_position + size() + offset();
  230. input.fork_to_replace = state.instruction_position;
  231. return ExecutionResult::Fork_PrioLow;
  232. }
  233. ALWAYS_INLINE ExecutionResult OpCode_CheckBegin::execute(MatchInput const& input, MatchState& state) const
  234. {
  235. auto is_at_line_boundary = [&] {
  236. if (state.string_position == 0)
  237. return true;
  238. if (input.regex_options.has_flag_set(AllFlags::Multiline) && input.regex_options.has_flag_set(AllFlags::Internal_ConsiderNewline)) {
  239. auto input_view = input.view.substring_view(state.string_position - 1, 1)[0];
  240. return input_view == '\n';
  241. }
  242. return false;
  243. }();
  244. if (is_at_line_boundary && (input.regex_options & AllFlags::MatchNotBeginOfLine))
  245. return ExecutionResult::Failed_ExecuteLowPrioForks;
  246. if ((is_at_line_boundary && !(input.regex_options & AllFlags::MatchNotBeginOfLine))
  247. || (!is_at_line_boundary && (input.regex_options & AllFlags::MatchNotBeginOfLine))
  248. || (is_at_line_boundary && (input.regex_options & AllFlags::Global)))
  249. return ExecutionResult::Continue;
  250. return ExecutionResult::Failed_ExecuteLowPrioForks;
  251. }
  252. ALWAYS_INLINE ExecutionResult OpCode_CheckBoundary::execute(MatchInput const& input, MatchState& state) const
  253. {
  254. auto isword = [](auto ch) { return is_ascii_alphanumeric(ch) || ch == '_'; };
  255. auto is_word_boundary = [&] {
  256. if (state.string_position == input.view.length()) {
  257. return (state.string_position > 0 && isword(input.view[state.string_position_in_code_units - 1]));
  258. }
  259. if (state.string_position == 0) {
  260. return (isword(input.view[0]));
  261. }
  262. return !!(isword(input.view[state.string_position_in_code_units]) ^ isword(input.view[state.string_position_in_code_units - 1]));
  263. };
  264. switch (type()) {
  265. case BoundaryCheckType::Word: {
  266. if (is_word_boundary())
  267. return ExecutionResult::Continue;
  268. return ExecutionResult::Failed_ExecuteLowPrioForks;
  269. }
  270. case BoundaryCheckType::NonWord: {
  271. if (!is_word_boundary())
  272. return ExecutionResult::Continue;
  273. return ExecutionResult::Failed_ExecuteLowPrioForks;
  274. }
  275. }
  276. VERIFY_NOT_REACHED();
  277. }
  278. ALWAYS_INLINE ExecutionResult OpCode_CheckEnd::execute(MatchInput const& input, MatchState& state) const
  279. {
  280. auto is_at_line_boundary = [&] {
  281. if (state.string_position == input.view.length())
  282. return true;
  283. if (input.regex_options.has_flag_set(AllFlags::Multiline) && input.regex_options.has_flag_set(AllFlags::Internal_ConsiderNewline)) {
  284. auto input_view = input.view.substring_view(state.string_position, 1)[0];
  285. return input_view == '\n';
  286. }
  287. return false;
  288. }();
  289. if (is_at_line_boundary && (input.regex_options & AllFlags::MatchNotEndOfLine))
  290. return ExecutionResult::Failed_ExecuteLowPrioForks;
  291. if ((is_at_line_boundary && !(input.regex_options & AllFlags::MatchNotEndOfLine))
  292. || (!is_at_line_boundary && (input.regex_options & AllFlags::MatchNotEndOfLine || input.regex_options & AllFlags::MatchNotBeginOfLine)))
  293. return ExecutionResult::Continue;
  294. return ExecutionResult::Failed_ExecuteLowPrioForks;
  295. }
  296. ALWAYS_INLINE ExecutionResult OpCode_ClearCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  297. {
  298. if (input.match_index < state.capture_group_matches.size()) {
  299. auto& group = state.capture_group_matches[input.match_index];
  300. auto group_id = id();
  301. if (group_id >= group.size())
  302. group.resize(group_id + 1);
  303. group[group_id].reset();
  304. }
  305. return ExecutionResult::Continue;
  306. }
  307. ALWAYS_INLINE ExecutionResult OpCode_SaveLeftCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  308. {
  309. if (input.match_index >= state.capture_group_matches.size()) {
  310. state.capture_group_matches.ensure_capacity(input.match_index);
  311. auto capacity = state.capture_group_matches.capacity();
  312. for (size_t i = state.capture_group_matches.size(); i <= capacity; ++i)
  313. state.capture_group_matches.empend();
  314. }
  315. if (id() >= state.capture_group_matches.at(input.match_index).size()) {
  316. state.capture_group_matches.at(input.match_index).ensure_capacity(id());
  317. auto capacity = state.capture_group_matches.at(input.match_index).capacity();
  318. for (size_t i = state.capture_group_matches.at(input.match_index).size(); i <= capacity; ++i)
  319. state.capture_group_matches.at(input.match_index).empend();
  320. }
  321. state.capture_group_matches.at(input.match_index).at(id()).left_column = state.string_position;
  322. return ExecutionResult::Continue;
  323. }
  324. ALWAYS_INLINE ExecutionResult OpCode_SaveRightCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  325. {
  326. auto& match = state.capture_group_matches.at(input.match_index).at(id());
  327. auto start_position = match.left_column;
  328. if (state.string_position < start_position) {
  329. dbgln("Right capture group {} is before left capture group {}!", state.string_position, start_position);
  330. return ExecutionResult::Failed_ExecuteLowPrioForks;
  331. }
  332. auto length = state.string_position - start_position;
  333. if (start_position < match.column)
  334. return ExecutionResult::Continue;
  335. VERIFY(start_position + length <= input.view.length());
  336. auto view = input.view.substring_view(start_position, length);
  337. if (input.regex_options & AllFlags::StringCopyMatches) {
  338. match = { view.to_deprecated_string(), input.line, start_position, input.global_offset + start_position }; // create a copy of the original string
  339. } else {
  340. match = { view, input.line, start_position, input.global_offset + start_position }; // take view to original string
  341. }
  342. return ExecutionResult::Continue;
  343. }
  344. ALWAYS_INLINE ExecutionResult OpCode_SaveRightNamedCaptureGroup::execute(MatchInput const& input, MatchState& state) const
  345. {
  346. auto& match = state.capture_group_matches.at(input.match_index).at(id());
  347. auto start_position = match.left_column;
  348. if (state.string_position < start_position)
  349. return ExecutionResult::Failed_ExecuteLowPrioForks;
  350. auto length = state.string_position - start_position;
  351. if (start_position < match.column)
  352. return ExecutionResult::Continue;
  353. VERIFY(start_position + length <= input.view.length());
  354. auto view = input.view.substring_view(start_position, length);
  355. if (input.regex_options & AllFlags::StringCopyMatches) {
  356. match = { view.to_deprecated_string(), name(), input.line, start_position, input.global_offset + start_position }; // create a copy of the original string
  357. } else {
  358. match = { view, name(), input.line, start_position, input.global_offset + start_position }; // take view to original string
  359. }
  360. return ExecutionResult::Continue;
  361. }
  362. ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, MatchState& state) const
  363. {
  364. bool inverse { false };
  365. bool temporary_inverse { false };
  366. bool reset_temp_inverse { false };
  367. struct DisjunctionState {
  368. bool active { false };
  369. bool is_conjunction { false };
  370. bool fail { false };
  371. size_t initial_position;
  372. size_t initial_code_unit_position;
  373. Optional<size_t> last_accepted_position {};
  374. Optional<size_t> last_accepted_code_unit_position {};
  375. };
  376. Vector<DisjunctionState, 4> disjunction_states;
  377. disjunction_states.empend();
  378. auto current_disjunction_state = [&]() -> DisjunctionState& { return disjunction_states.last(); };
  379. auto current_inversion_state = [&]() -> bool { return temporary_inverse ^ inverse; };
  380. size_t string_position = state.string_position;
  381. bool inverse_matched { false };
  382. bool had_zero_length_match { false };
  383. state.string_position_before_match = state.string_position;
  384. size_t offset { state.instruction_position + 3 };
  385. for (size_t i = 0; i < arguments_count(); ++i) {
  386. if (state.string_position > string_position)
  387. break;
  388. if (reset_temp_inverse) {
  389. reset_temp_inverse = false;
  390. temporary_inverse = false;
  391. } else {
  392. reset_temp_inverse = true;
  393. }
  394. auto compare_type = (CharacterCompareType)m_bytecode->at(offset++);
  395. if (compare_type == CharacterCompareType::Inverse) {
  396. inverse = !inverse;
  397. continue;
  398. } else if (compare_type == CharacterCompareType::TemporaryInverse) {
  399. // If "TemporaryInverse" is given, negate the current inversion state only for the next opcode.
  400. // it follows that this cannot be the last compare element.
  401. VERIFY(i != arguments_count() - 1);
  402. temporary_inverse = true;
  403. reset_temp_inverse = false;
  404. continue;
  405. } else if (compare_type == CharacterCompareType::Char) {
  406. u32 ch = m_bytecode->at(offset++);
  407. // We want to compare a string that is longer or equal in length to the available string
  408. if (input.view.length() <= state.string_position)
  409. return ExecutionResult::Failed_ExecuteLowPrioForks;
  410. compare_char(input, state, ch, current_inversion_state(), inverse_matched);
  411. } else if (compare_type == CharacterCompareType::AnyChar) {
  412. // We want to compare a string that is definitely longer than the available string
  413. if (input.view.length() <= state.string_position)
  414. return ExecutionResult::Failed_ExecuteLowPrioForks;
  415. auto input_view = input.view.substring_view(state.string_position, 1)[0];
  416. if (input_view != '\n' || (input.regex_options.has_flag_set(AllFlags::SingleLine) && input.regex_options.has_flag_set(AllFlags::Internal_ConsiderNewline))) {
  417. if (current_inversion_state())
  418. inverse_matched = true;
  419. else
  420. advance_string_position(state, input.view, input_view);
  421. }
  422. } else if (compare_type == CharacterCompareType::String) {
  423. VERIFY(!current_inversion_state());
  424. auto const& length = m_bytecode->at(offset++);
  425. // We want to compare a string that is definitely longer than the available string
  426. if (input.view.length() < state.string_position + length)
  427. return ExecutionResult::Failed_ExecuteLowPrioForks;
  428. Optional<DeprecatedString> str;
  429. Vector<u16, 1> utf16;
  430. Vector<u32> data;
  431. data.ensure_capacity(length);
  432. for (size_t i = offset; i < offset + length; ++i)
  433. data.unchecked_append(m_bytecode->at(i));
  434. auto view = input.view.construct_as_same(data, str, utf16);
  435. offset += length;
  436. if (compare_string(input, state, view, had_zero_length_match)) {
  437. if (current_inversion_state())
  438. inverse_matched = true;
  439. }
  440. } else if (compare_type == CharacterCompareType::CharClass) {
  441. if (input.view.length() <= state.string_position_in_code_units)
  442. return ExecutionResult::Failed_ExecuteLowPrioForks;
  443. auto character_class = (CharClass)m_bytecode->at(offset++);
  444. auto ch = input.view[state.string_position_in_code_units];
  445. compare_character_class(input, state, character_class, ch, current_inversion_state(), inverse_matched);
  446. } else if (compare_type == CharacterCompareType::LookupTable) {
  447. if (input.view.length() <= state.string_position)
  448. return ExecutionResult::Failed_ExecuteLowPrioForks;
  449. auto count = m_bytecode->at(offset++);
  450. auto range_data = m_bytecode->template spans<4>().slice(offset, count);
  451. offset += count;
  452. auto ch = input.view.substring_view(state.string_position, 1)[0];
  453. auto const* matching_range = binary_search(range_data, ch, nullptr, [insensitive = input.regex_options & AllFlags::Insensitive](auto needle, CharRange range) {
  454. auto upper_case_needle = needle;
  455. auto lower_case_needle = needle;
  456. if (insensitive) {
  457. upper_case_needle = to_ascii_uppercase(needle);
  458. lower_case_needle = to_ascii_lowercase(needle);
  459. }
  460. if (lower_case_needle >= range.from && lower_case_needle <= range.to)
  461. return 0;
  462. if (upper_case_needle >= range.from && upper_case_needle <= range.to)
  463. return 0;
  464. if (lower_case_needle > range.to || upper_case_needle > range.to)
  465. return 1;
  466. return -1;
  467. });
  468. if (matching_range) {
  469. if (current_inversion_state())
  470. inverse_matched = true;
  471. else
  472. advance_string_position(state, input.view, ch);
  473. }
  474. } else if (compare_type == CharacterCompareType::CharRange) {
  475. if (input.view.length() <= state.string_position)
  476. return ExecutionResult::Failed_ExecuteLowPrioForks;
  477. auto value = (CharRange)m_bytecode->at(offset++);
  478. auto from = value.from;
  479. auto to = value.to;
  480. auto ch = input.view[state.string_position_in_code_units];
  481. compare_character_range(input, state, from, to, ch, current_inversion_state(), inverse_matched);
  482. } else if (compare_type == CharacterCompareType::Reference) {
  483. auto reference_number = (size_t)m_bytecode->at(offset++);
  484. auto& groups = state.capture_group_matches.at(input.match_index);
  485. if (groups.size() <= reference_number)
  486. return ExecutionResult::Failed_ExecuteLowPrioForks;
  487. auto str = groups.at(reference_number).view;
  488. // We want to compare a string that is definitely longer than the available string
  489. if (input.view.length() < state.string_position + str.length())
  490. return ExecutionResult::Failed_ExecuteLowPrioForks;
  491. if (compare_string(input, state, str, had_zero_length_match)) {
  492. if (current_inversion_state())
  493. inverse_matched = true;
  494. }
  495. } else if (compare_type == CharacterCompareType::Property) {
  496. auto property = static_cast<Unicode::Property>(m_bytecode->at(offset++));
  497. compare_property(input, state, property, current_inversion_state(), inverse_matched);
  498. } else if (compare_type == CharacterCompareType::GeneralCategory) {
  499. auto general_category = static_cast<Unicode::GeneralCategory>(m_bytecode->at(offset++));
  500. compare_general_category(input, state, general_category, current_inversion_state(), inverse_matched);
  501. } else if (compare_type == CharacterCompareType::Script) {
  502. auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
  503. compare_script(input, state, script, current_inversion_state(), inverse_matched);
  504. } else if (compare_type == CharacterCompareType::ScriptExtension) {
  505. auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
  506. compare_script_extension(input, state, script, current_inversion_state(), inverse_matched);
  507. } else if (compare_type == CharacterCompareType::And) {
  508. disjunction_states.append({
  509. .active = true,
  510. .is_conjunction = false,
  511. .fail = false,
  512. .initial_position = state.string_position,
  513. .initial_code_unit_position = state.string_position_in_code_units,
  514. });
  515. continue;
  516. } else if (compare_type == CharacterCompareType::Or) {
  517. disjunction_states.append({
  518. .active = true,
  519. .is_conjunction = true,
  520. .fail = true,
  521. .initial_position = state.string_position,
  522. .initial_code_unit_position = state.string_position_in_code_units,
  523. });
  524. continue;
  525. } else if (compare_type == CharacterCompareType::EndAndOr) {
  526. auto disjunction_state = disjunction_states.take_last();
  527. if (!disjunction_state.fail) {
  528. state.string_position = disjunction_state.last_accepted_position.value_or(disjunction_state.initial_position);
  529. state.string_position_in_code_units = disjunction_state.last_accepted_code_unit_position.value_or(disjunction_state.initial_code_unit_position);
  530. }
  531. } else {
  532. warnln("Undefined comparison: {}", (int)compare_type);
  533. VERIFY_NOT_REACHED();
  534. break;
  535. }
  536. auto& new_disjunction_state = current_disjunction_state();
  537. if (current_inversion_state() && (!inverse || new_disjunction_state.active) && !inverse_matched) {
  538. advance_string_position(state, input.view);
  539. inverse_matched = true;
  540. }
  541. if (new_disjunction_state.active) {
  542. auto failed = (!had_zero_length_match && string_position == state.string_position) || state.string_position > input.view.length();
  543. if (!failed) {
  544. new_disjunction_state.last_accepted_position = state.string_position;
  545. new_disjunction_state.last_accepted_code_unit_position = state.string_position_in_code_units;
  546. }
  547. if (new_disjunction_state.is_conjunction)
  548. new_disjunction_state.fail = failed && new_disjunction_state.fail;
  549. else
  550. new_disjunction_state.fail = failed || new_disjunction_state.fail;
  551. state.string_position = new_disjunction_state.initial_position;
  552. state.string_position_in_code_units = new_disjunction_state.initial_code_unit_position;
  553. }
  554. }
  555. auto& new_disjunction_state = current_disjunction_state();
  556. if (new_disjunction_state.active) {
  557. if (!new_disjunction_state.fail) {
  558. state.string_position = new_disjunction_state.last_accepted_position.value_or(new_disjunction_state.initial_position);
  559. state.string_position_in_code_units = new_disjunction_state.last_accepted_code_unit_position.value_or(new_disjunction_state.initial_code_unit_position);
  560. }
  561. }
  562. if (current_inversion_state() && !inverse_matched)
  563. advance_string_position(state, input.view);
  564. if ((!had_zero_length_match && string_position == state.string_position) || state.string_position > input.view.length())
  565. return ExecutionResult::Failed_ExecuteLowPrioForks;
  566. return ExecutionResult::Continue;
  567. }
  568. ALWAYS_INLINE void OpCode_Compare::compare_char(MatchInput const& input, MatchState& state, u32 ch1, bool inverse, bool& inverse_matched)
  569. {
  570. if (state.string_position == input.view.length())
  571. return;
  572. auto input_view = input.view.substring_view(state.string_position, 1)[0];
  573. bool equal;
  574. if (input.regex_options & AllFlags::Insensitive)
  575. equal = to_ascii_lowercase(input_view) == to_ascii_lowercase(ch1); // FIXME: Implement case-insensitive matching for non-ascii characters
  576. else
  577. equal = input_view == ch1;
  578. if (equal) {
  579. if (inverse)
  580. inverse_matched = true;
  581. else
  582. advance_string_position(state, input.view, ch1);
  583. }
  584. }
  585. ALWAYS_INLINE bool OpCode_Compare::compare_string(MatchInput const& input, MatchState& state, RegexStringView str, bool& had_zero_length_match)
  586. {
  587. if (state.string_position + str.length() > input.view.length()) {
  588. if (str.is_empty()) {
  589. had_zero_length_match = true;
  590. return true;
  591. }
  592. return false;
  593. }
  594. if (str.length() == 0) {
  595. had_zero_length_match = true;
  596. return true;
  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. input.checkpoints.set(state.instruction_position, state.string_position);
  884. return ExecutionResult::Continue;
  885. }
  886. ALWAYS_INLINE ExecutionResult OpCode_JumpNonEmpty::execute(MatchInput const& input, MatchState& state) const
  887. {
  888. u64 current_position = state.string_position;
  889. auto checkpoint_ip = state.instruction_position + size() + checkpoint();
  890. auto checkpoint_position = input.checkpoints.find(checkpoint_ip);
  891. if (checkpoint_position != input.checkpoints.end() && checkpoint_position->value != current_position) {
  892. auto form = this->form();
  893. if (form == OpCodeId::Jump) {
  894. state.instruction_position += offset();
  895. return ExecutionResult::Continue;
  896. }
  897. state.fork_at_position = state.instruction_position + size() + offset();
  898. if (form == OpCodeId::ForkJump) {
  899. state.forks_since_last_save++;
  900. return ExecutionResult::Fork_PrioHigh;
  901. }
  902. if (form == OpCodeId::ForkStay) {
  903. state.forks_since_last_save++;
  904. return ExecutionResult::Fork_PrioLow;
  905. }
  906. if (form == OpCodeId::ForkReplaceStay) {
  907. input.fork_to_replace = state.instruction_position;
  908. return ExecutionResult::Fork_PrioLow;
  909. }
  910. if (form == OpCodeId::ForkReplaceJump) {
  911. input.fork_to_replace = state.instruction_position;
  912. return ExecutionResult::Fork_PrioHigh;
  913. }
  914. }
  915. return ExecutionResult::Continue;
  916. }
  917. }