RegexByteCode.cpp 43 KB

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