mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
LibRegex: Make OpCode_Compare use a switch-case instead of if-else
This has no performance impact, the if-elses have been annoying as it's impossible to fold the entire thing in basically any IDE.
This commit is contained in:
parent
4e69eb89e8
commit
d9ed58eb39
1 changed files with 42 additions and 34 deletions
|
@ -459,11 +459,11 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
|
|
||||||
auto compare_type = (CharacterCompareType)m_bytecode->at(offset++);
|
auto compare_type = (CharacterCompareType)m_bytecode->at(offset++);
|
||||||
|
|
||||||
if (compare_type == CharacterCompareType::Inverse) {
|
switch (compare_type) {
|
||||||
|
case CharacterCompareType::Inverse:
|
||||||
inverse = !inverse;
|
inverse = !inverse;
|
||||||
continue;
|
continue;
|
||||||
|
case CharacterCompareType::TemporaryInverse:
|
||||||
} else if (compare_type == CharacterCompareType::TemporaryInverse) {
|
|
||||||
// If "TemporaryInverse" is given, negate the current inversion state only for the next opcode.
|
// If "TemporaryInverse" is given, negate the current inversion state only for the next opcode.
|
||||||
// it follows that this cannot be the last compare element.
|
// it follows that this cannot be the last compare element.
|
||||||
VERIFY(i != arguments_count() - 1);
|
VERIFY(i != arguments_count() - 1);
|
||||||
|
@ -471,8 +471,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
temporary_inverse = true;
|
temporary_inverse = true;
|
||||||
reset_temp_inverse = false;
|
reset_temp_inverse = false;
|
||||||
continue;
|
continue;
|
||||||
|
case CharacterCompareType::Char: {
|
||||||
} else if (compare_type == CharacterCompareType::Char) {
|
|
||||||
u32 ch = m_bytecode->at(offset++);
|
u32 ch = m_bytecode->at(offset++);
|
||||||
|
|
||||||
// We want to compare a string that is longer or equal in length to the available string
|
// We want to compare a string that is longer or equal in length to the available string
|
||||||
|
@ -480,8 +479,9 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
||||||
|
|
||||||
compare_char(input, state, ch, current_inversion_state(), inverse_matched);
|
compare_char(input, state, ch, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::AnyChar) {
|
}
|
||||||
|
case CharacterCompareType::AnyChar: {
|
||||||
// We want to compare a string that is definitely longer than the available string
|
// We want to compare a string that is definitely longer than the available string
|
||||||
if (input.view.length() <= state.string_position)
|
if (input.view.length() <= state.string_position)
|
||||||
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
||||||
|
@ -498,8 +498,9 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
else
|
else
|
||||||
advance_string_position(state, input.view, input_view);
|
advance_string_position(state, input.view, input_view);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::String) {
|
}
|
||||||
|
case CharacterCompareType::String: {
|
||||||
VERIFY(!current_inversion_state());
|
VERIFY(!current_inversion_state());
|
||||||
|
|
||||||
auto const& length = m_bytecode->at(offset++);
|
auto const& length = m_bytecode->at(offset++);
|
||||||
|
@ -521,9 +522,9 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
if (current_inversion_state())
|
if (current_inversion_state())
|
||||||
inverse_matched = true;
|
inverse_matched = true;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::CharClass) {
|
}
|
||||||
|
case CharacterCompareType::CharClass: {
|
||||||
if (input.view.length() <= state.string_position_in_code_units)
|
if (input.view.length() <= state.string_position_in_code_units)
|
||||||
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
||||||
|
|
||||||
|
@ -531,8 +532,9 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
auto ch = input.view[state.string_position_in_code_units];
|
auto ch = input.view[state.string_position_in_code_units];
|
||||||
|
|
||||||
compare_character_class(input, state, character_class, ch, current_inversion_state(), inverse_matched);
|
compare_character_class(input, state, character_class, ch, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::LookupTable) {
|
}
|
||||||
|
case CharacterCompareType::LookupTable: {
|
||||||
if (input.view.length() <= state.string_position)
|
if (input.view.length() <= state.string_position)
|
||||||
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
||||||
|
|
||||||
|
@ -565,8 +567,9 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
else
|
else
|
||||||
advance_string_position(state, input.view, ch);
|
advance_string_position(state, input.view, ch);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::CharRange) {
|
}
|
||||||
|
case CharacterCompareType::CharRange: {
|
||||||
if (input.view.length() <= state.string_position)
|
if (input.view.length() <= state.string_position)
|
||||||
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
return ExecutionResult::Failed_ExecuteLowPrioForks;
|
||||||
|
|
||||||
|
@ -577,8 +580,9 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
auto ch = input.view[state.string_position_in_code_units];
|
auto ch = input.view[state.string_position_in_code_units];
|
||||||
|
|
||||||
compare_character_range(input, state, from, to, ch, current_inversion_state(), inverse_matched);
|
compare_character_range(input, state, from, to, ch, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::Reference) {
|
}
|
||||||
|
case CharacterCompareType::Reference: {
|
||||||
auto reference_number = (size_t)m_bytecode->at(offset++);
|
auto reference_number = (size_t)m_bytecode->at(offset++);
|
||||||
auto& groups = state.capture_group_matches.at(input.match_index);
|
auto& groups = state.capture_group_matches.at(input.match_index);
|
||||||
if (groups.size() <= reference_number)
|
if (groups.size() <= reference_number)
|
||||||
|
@ -594,24 +598,29 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
if (current_inversion_state())
|
if (current_inversion_state())
|
||||||
inverse_matched = true;
|
inverse_matched = true;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::Property) {
|
}
|
||||||
|
case CharacterCompareType::Property: {
|
||||||
auto property = static_cast<Unicode::Property>(m_bytecode->at(offset++));
|
auto property = static_cast<Unicode::Property>(m_bytecode->at(offset++));
|
||||||
compare_property(input, state, property, current_inversion_state(), inverse_matched);
|
compare_property(input, state, property, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::GeneralCategory) {
|
}
|
||||||
|
case CharacterCompareType::GeneralCategory: {
|
||||||
auto general_category = static_cast<Unicode::GeneralCategory>(m_bytecode->at(offset++));
|
auto general_category = static_cast<Unicode::GeneralCategory>(m_bytecode->at(offset++));
|
||||||
compare_general_category(input, state, general_category, current_inversion_state(), inverse_matched);
|
compare_general_category(input, state, general_category, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::Script) {
|
}
|
||||||
|
case CharacterCompareType::Script: {
|
||||||
auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
|
auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
|
||||||
compare_script(input, state, script, current_inversion_state(), inverse_matched);
|
compare_script(input, state, script, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::ScriptExtension) {
|
}
|
||||||
|
case CharacterCompareType::ScriptExtension: {
|
||||||
auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
|
auto script = static_cast<Unicode::Script>(m_bytecode->at(offset++));
|
||||||
compare_script_extension(input, state, script, current_inversion_state(), inverse_matched);
|
compare_script_extension(input, state, script, current_inversion_state(), inverse_matched);
|
||||||
|
break;
|
||||||
} else if (compare_type == CharacterCompareType::And) {
|
}
|
||||||
|
case CharacterCompareType::And:
|
||||||
disjunction_states.append({
|
disjunction_states.append({
|
||||||
.active = true,
|
.active = true,
|
||||||
.is_conjunction = false,
|
.is_conjunction = false,
|
||||||
|
@ -620,8 +629,7 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
.initial_code_unit_position = state.string_position_in_code_units,
|
.initial_code_unit_position = state.string_position_in_code_units,
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
|
case CharacterCompareType::Or:
|
||||||
} else if (compare_type == CharacterCompareType::Or) {
|
|
||||||
disjunction_states.append({
|
disjunction_states.append({
|
||||||
.active = true,
|
.active = true,
|
||||||
.is_conjunction = true,
|
.is_conjunction = true,
|
||||||
|
@ -630,15 +638,15 @@ ALWAYS_INLINE ExecutionResult OpCode_Compare::execute(MatchInput const& input, M
|
||||||
.initial_code_unit_position = state.string_position_in_code_units,
|
.initial_code_unit_position = state.string_position_in_code_units,
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
|
case CharacterCompareType::EndAndOr: {
|
||||||
} else if (compare_type == CharacterCompareType::EndAndOr) {
|
|
||||||
auto disjunction_state = disjunction_states.take_last();
|
auto disjunction_state = disjunction_states.take_last();
|
||||||
if (!disjunction_state.fail) {
|
if (!disjunction_state.fail) {
|
||||||
state.string_position = disjunction_state.last_accepted_position.value_or(disjunction_state.initial_position);
|
state.string_position = disjunction_state.last_accepted_position.value_or(disjunction_state.initial_position);
|
||||||
state.string_position_in_code_units = disjunction_state.last_accepted_code_unit_position.value_or(disjunction_state.initial_code_unit_position);
|
state.string_position_in_code_units = disjunction_state.last_accepted_code_unit_position.value_or(disjunction_state.initial_code_unit_position);
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
} else {
|
}
|
||||||
|
default:
|
||||||
warnln("Undefined comparison: {}", (int)compare_type);
|
warnln("Undefined comparison: {}", (int)compare_type);
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue