RegexByteCode.cpp 34 KB

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