RegexByteCode.cpp 32 KB

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