refactor consume_hilites to assign last_hilite to an argument, rather than return it, to avoid code duplication in are_equal_with_hilites

This commit is contained in:
natasha 2023-04-04 14:58:39 +01:00
parent f3560a295d
commit 0a021d3378
3 changed files with 11 additions and 20 deletions

View File

@ -3,25 +3,22 @@
namespace HiliteComparator
{
[[maybe_unused]] Hilite consume_hilites(const char * & ptr)
void consume_hilites(const char * & ptr, Hilite * last_hilite)
{
const char * last_hilite = nullptr;
while (true)
bool changed_hilite = false;
do
{
bool changed_hilite = false;
for (Hilite hilite : HILITES)
{
if (std::string_view(ptr).starts_with(hilite))
{
ptr += strlen(hilite);
changed_hilite = true;
last_hilite = hilite;
if (last_hilite != nullptr)
*last_hilite = hilite;
}
}
if (!changed_hilite)
break;
}
return last_hilite;
} while (changed_hilite);
}
bool are_equal_with_hilites_removed(std::string_view left, std::string_view right)
@ -62,15 +59,8 @@ bool are_equal_with_hilites(std::string_view left, std::string_view right)
while (true)
{
// Consume all prefix hilites, update the current hilite to be the last one.
Hilite last_hilite;
last_hilite = consume_hilites(left_it);
if (last_hilite != nullptr)
left_hilite = last_hilite;
last_hilite = consume_hilites(right_it);
if (last_hilite != nullptr)
right_hilite = last_hilite;
consume_hilites(left_it, &left_hilite);
consume_hilites(right_it, &right_hilite);
if (left_it == left.end() && right_it == right.end())
return true;

View File

@ -18,7 +18,7 @@ const std::vector<Hilite> HILITES = {
DB::IAST::hilite_none
};
[[maybe_unused]] Hilite consume_hilites(const char * & ptr);
void consume_hilites(const char * & ptr, Hilite * last_hilite = nullptr);
String remove_hilites(std::string_view string);

View File

@ -20,7 +20,8 @@ TEST(HiliteComparator, ConsumeHilites)
s += IAST::hilite_keyword;
const char * ptr = s.c_str();
const char * expected_ptr = strchr(ptr, 't');
const char * last_hilite = consume_hilites(ptr);
const char * last_hilite = nullptr;
consume_hilites(ptr, &last_hilite);
ASSERT_EQ(expected_ptr, ptr);
ASSERT_TRUE(last_hilite != nullptr);
ASSERT_EQ(IAST::hilite_function, last_hilite);