RegexByteCode.cpp 29 KB

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