LibRegex: Consider the inverse=true case when finding pattern overlap

Previously we were only checking for overlap when the range wasn't in
inverse mode, which made us miss things like /[^x]x/; this patch makes
it so we don't miss that.
This commit is contained in:
Ali Mohammad Pur 2023-02-15 10:12:03 +03:30 committed by Andreas Kling
parent 936a9fd759
commit af441bb939
2 changed files with 5 additions and 3 deletions

View file

@ -984,6 +984,8 @@ TEST_CASE(optimizer_atomic_groups)
Tuple { "(1+)0"sv, "10"sv, true },
// Rewrite should not skip over first required iteration of <x>+.
Tuple { "a+"sv, ""sv, false },
// 'y' and [^x] have an overlap ('y'), the loop should not be rewritten here.
Tuple { "[^x]+y"sv, "ay"sv, true },
};
for (auto& test : tests) {

View file

@ -251,7 +251,7 @@ static bool has_overlap(Vector<CompareTypeAndValuePair> const& lhs, Vector<Compa
return true;
break;
case CharacterCompareType::Char:
if (!current_lhs_inversion_state() && range_contains(pair.value))
if (current_lhs_inversion_state() ^ range_contains(pair.value))
return true;
break;
case CharacterCompareType::String:
@ -259,12 +259,12 @@ static bool has_overlap(Vector<CompareTypeAndValuePair> const& lhs, Vector<Compa
// Just bail out to avoid false positives.
return true;
case CharacterCompareType::CharClass:
if (!current_lhs_inversion_state() && char_class_contains(static_cast<CharClass>(pair.value)))
if (current_lhs_inversion_state() ^ char_class_contains(static_cast<CharClass>(pair.value)))
return true;
break;
case CharacterCompareType::CharRange: {
auto range = CharRange(pair.value);
if (!current_lhs_inversion_state() && range_contains(range))
if (current_lhs_inversion_state() ^ range_contains(range))
return true;
break;
}