diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-06-13 22:18:54 +0200 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-06-14 16:09:58 +0430 |
commit | 281f39073d87a080db1ac670f956fb81a4c00a76 (patch) | |
tree | 3093c4164d323affbed22ccdc0f0907c2487eeb5 /Userland/Libraries/LibRegex | |
parent | cd49fb02296b3474b3b7a3008e47e7959d621ccf (diff) | |
download | serenity-281f39073d87a080db1ac670f956fb81a4c00a76.zip |
LibRegex: Make get_opcode() return a reference
Previously this would return a pointer which could be null if the
requested opcode was invalid. This should never be the case though
so let's VERIFY() that instead.
Diffstat (limited to 'Userland/Libraries/LibRegex')
-rw-r--r-- | Userland/Libraries/LibRegex/RegexByteCode.cpp | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibRegex/RegexByteCode.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibRegex/RegexDebug.h | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibRegex/RegexMatcher.cpp | 15 |
4 files changed, 25 insertions, 38 deletions
diff --git a/Userland/Libraries/LibRegex/RegexByteCode.cpp b/Userland/Libraries/LibRegex/RegexByteCode.cpp index 2f05f61dde..feb20cf743 100644 --- a/Userland/Libraries/LibRegex/RegexByteCode.cpp +++ b/Userland/Libraries/LibRegex/RegexByteCode.cpp @@ -90,7 +90,7 @@ static const char* character_class_name(CharClass ch_class) OwnPtr<OpCode> ByteCode::s_opcodes[(size_t)OpCodeId::Last + 1]; bool ByteCode::s_opcodes_initialized { false }; -ALWAYS_INLINE OpCode* ByteCode::get_opcode_by_id(OpCodeId id) const +ALWAYS_INLINE OpCode& ByteCode::get_opcode_by_id(OpCodeId id) const { if (!s_opcodes_initialized) { for (u32 i = (u32)OpCodeId::First; i <= (u32)OpCodeId::Last; ++i) { @@ -148,27 +148,24 @@ ALWAYS_INLINE OpCode* ByteCode::get_opcode_by_id(OpCodeId id) const s_opcodes_initialized = true; } - if (id > OpCodeId::Last) - return nullptr; + VERIFY(id >= OpCodeId::First && id <= OpCodeId::Last); - auto* opcode = &*s_opcodes[(u32)id]; + auto& opcode = s_opcodes[(u32)id]; opcode->set_bytecode(*const_cast<ByteCode*>(this)); - return opcode; + return *opcode; } -OpCode* ByteCode::get_opcode(MatchState& state) const +OpCode& ByteCode::get_opcode(MatchState& state) const { - OpCode* op_code; - - if (state.instruction_position >= size()) { - op_code = get_opcode_by_id(OpCodeId::Exit); - } else - op_code = get_opcode_by_id((OpCodeId)at(state.instruction_position)); - - if (op_code) - op_code->set_state(state); - - return op_code; + OpCodeId opcode_id; + if (state.instruction_position >= size()) + opcode_id = OpCodeId::Exit; + else + opcode_id = (OpCodeId)at(state.instruction_position); + + auto& opcode = get_opcode_by_id(opcode_id); + opcode.set_state(state); + return opcode; } ALWAYS_INLINE ExecutionResult OpCode_Exit::execute(const MatchInput& input, MatchState& state, MatchOutput&) const diff --git a/Userland/Libraries/LibRegex/RegexByteCode.h b/Userland/Libraries/LibRegex/RegexByteCode.h index c7d610b584..7d4cb43007 100644 --- a/Userland/Libraries/LibRegex/RegexByteCode.h +++ b/Userland/Libraries/LibRegex/RegexByteCode.h @@ -439,7 +439,7 @@ public: bytecode_to_repeat = move(bytecode); } - OpCode* get_opcode(MatchState& state) const; + OpCode& get_opcode(MatchState& state) const; private: void insert_string(const StringView& view) @@ -449,7 +449,7 @@ private: empend((ByteCodeValueType)view[i]); } - ALWAYS_INLINE OpCode* get_opcode_by_id(OpCodeId id) const; + ALWAYS_INLINE OpCode& get_opcode_by_id(OpCodeId id) const; static OwnPtr<OpCode> s_opcodes[(size_t)OpCodeId::Last + 1]; static bool s_opcodes_initialized; }; diff --git a/Userland/Libraries/LibRegex/RegexDebug.h b/Userland/Libraries/LibRegex/RegexDebug.h index 821870242b..ebe25d8ddb 100644 --- a/Userland/Libraries/LibRegex/RegexDebug.h +++ b/Userland/Libraries/LibRegex/RegexDebug.h @@ -39,19 +39,14 @@ public: auto& bytecode = regex.parser_result.bytecode; for (;;) { - auto* opcode = bytecode.get_opcode(state); - if (!opcode) { - dbgln("Wrong opcode... failed!"); - return; - } - - print_opcode("PrintBytecode", *opcode, state); + auto& opcode = bytecode.get_opcode(state); + print_opcode("PrintBytecode", opcode, state); out(m_file, "{}", m_debug_stripline); - if (is<OpCode_Exit>(*opcode)) + if (is<OpCode_Exit>(opcode)) break; - state.instruction_position += opcode->size(); + state.instruction_position += opcode.size(); } fflush(m_file); diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp index d294a01c83..b402442220 100644 --- a/Userland/Libraries/LibRegex/RegexMatcher.cpp +++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp @@ -301,15 +301,10 @@ Optional<bool> Matcher<Parser>::execute(const MatchInput& input, MatchState& sta for (;;) { ++output.operations; - auto* opcode = bytecode.get_opcode(state); - - if (!opcode) { - dbgln("Wrong opcode... failed!"); - return {}; - } + auto& opcode = bytecode.get_opcode(state); #if REGEX_DEBUG - s_regex_dbg.print_opcode("VM", *opcode, state, recursion_level, false); + s_regex_dbg.print_opcode("VM", opcode, state, recursion_level, false); #endif ExecutionResult result; @@ -317,14 +312,14 @@ Optional<bool> Matcher<Parser>::execute(const MatchInput& input, MatchState& sta --input.fail_counter; result = ExecutionResult::Failed_ExecuteLowPrioForks; } else { - result = opcode->execute(input, state, output); + result = opcode.execute(input, state, output); } #if REGEX_DEBUG - s_regex_dbg.print_result(*opcode, bytecode, input, state, result); + s_regex_dbg.print_result(opcode, bytecode, input, state, result); #endif - state.instruction_position += opcode->size(); + state.instruction_position += opcode.size(); switch (result) { case ExecutionResult::Fork_PrioLow: |