get rid of std::stringstream

This commit is contained in:
natasha 2023-03-28 15:30:54 +01:00
parent d7d008c44d
commit e984e221ce

View File

@ -8,11 +8,7 @@
String hilite(const String & s, const char * hilite_type) String hilite(const String & s, const char * hilite_type)
{ {
std::stringstream ss; return hilite_type + s + DB::IAST::hilite_none;
ss << hilite_type;
ss << s;
ss << DB::IAST::hilite_none;
return ss.str();
} }
String keyword(const String & s) String keyword(const String & s)
@ -80,13 +76,13 @@ std::vector<const char *> HILITES =
String remove_hilites(std::string_view string) String remove_hilites(std::string_view string)
{ {
const char * ptr = string.begin(); const char * ptr = string.begin();
std::stringstream ss; String string_without_hilites;
while (true) while (true)
{ {
consume_hilites(&ptr); consume_hilites(&ptr);
if (ptr == string.end()) if (ptr == string.end())
return ss.str(); return string_without_hilites;
ss << *(ptr++); string_without_hilites += *(ptr++);
} }
} }
@ -148,18 +144,18 @@ bool are_equal_with_hilites(std::string_view left, std::string_view right)
TEST(FormatHiliting, MetaTestConsumeHilites) TEST(FormatHiliting, MetaTestConsumeHilites)
{ {
using namespace DB; using namespace DB;
std::stringstream ss;
// The order is different from the order in HILITES on purpose. // The order is different from the order in HILITES on purpose.
ss << IAST::hilite_keyword String s;
<< IAST::hilite_alias s += IAST::hilite_keyword;
<< IAST::hilite_identifier s += IAST::hilite_alias;
<< IAST::hilite_none s += IAST::hilite_identifier;
<< IAST::hilite_operator s += IAST::hilite_none;
<< IAST::hilite_substitution s += IAST::hilite_operator;
<< IAST::hilite_function s += IAST::hilite_substitution;
<< "test" << IAST::hilite_keyword; s += IAST::hilite_function;
String string = ss.str(); s += "test";
const char * ptr = string.c_str(); s += IAST::hilite_keyword;
const char * ptr = s.c_str();
const char * expected_ptr = strchr(ptr, 't'); const char * expected_ptr = strchr(ptr, 't');
const char * last_hilite = consume_hilites(&ptr); const char * last_hilite = consume_hilites(&ptr);
ASSERT_EQ(expected_ptr, ptr); ASSERT_EQ(expected_ptr, ptr);
@ -170,16 +166,23 @@ TEST(FormatHiliting, MetaTestConsumeHilites)
TEST(FormatHiliting, MetaTestRemoveHilites) TEST(FormatHiliting, MetaTestRemoveHilites)
{ {
using namespace DB; using namespace DB;
std::stringstream ss; String s;
ss << IAST::hilite_keyword s += IAST::hilite_keyword;
<< "te" << IAST::hilite_alias << IAST::hilite_identifier s += "te";
<< "s" << IAST::hilite_none s += IAST::hilite_alias;
<< "t" << IAST::hilite_operator << IAST::hilite_substitution << IAST::hilite_function; s += IAST::hilite_identifier;
ASSERT_EQ("test", remove_hilites(ss.str())); s += "s";
s += IAST::hilite_none;
s += "t";
s += IAST::hilite_operator;
s += IAST::hilite_substitution;
s += IAST::hilite_function;
ASSERT_EQ("test", remove_hilites(s));
} }
TEST(FormatHiliting, MetaTestAreEqualWithHilites) TEST(FormatHiliting, MetaTestAreEqualWithHilites)
{ {
using namespace DB;
ASSERT_PRED2(are_equal_with_hilites, "", ""); ASSERT_PRED2(are_equal_with_hilites, "", "");
for (const char * hilite : HILITES) for (const char * hilite : HILITES)
@ -189,23 +192,37 @@ TEST(FormatHiliting, MetaTestAreEqualWithHilites)
} }
{ {
std::stringstream ss; String s;
ss << DB::IAST::hilite_none << "select" << DB::IAST::hilite_none; s += IAST::hilite_none;
ASSERT_PRED2(are_equal_with_hilites, ss.str(), "select"); s += "select";
s += IAST::hilite_none;
ASSERT_PRED2(are_equal_with_hilites, s, "select");
} }
{ {
std::stringstream ss; String s;
ss << DB::IAST::hilite_none << "\n " << "sel" << DB::IAST::hilite_none << "ect" << DB::IAST::hilite_none; s += DB::IAST::hilite_none;
ASSERT_PRED2(are_equal_with_hilites, ss.str(), "\n select"); s += "\n sel";
s += DB::IAST::hilite_none;
s += "ect";
s += DB::IAST::hilite_none;
ASSERT_PRED2(are_equal_with_hilites, s, "\n select");
} }
{ {
std::stringstream left; String left;
left << DB::IAST::hilite_keyword << "keyword" << " long" << DB::IAST::hilite_none; left += DB::IAST::hilite_keyword;
std::stringstream right; left += "keyword long";
right << DB::IAST::hilite_keyword << "keyword" << DB::IAST::hilite_none << " " << DB::IAST::hilite_keyword << "long"; left += DB::IAST::hilite_none;
ASSERT_PRED2(are_equal_with_hilites, left.str(), right.str());
String right;
right += DB::IAST::hilite_keyword;
right += "keyword";
right += DB::IAST::hilite_none;
right += " ";
right += DB::IAST::hilite_keyword;
right += "long";
ASSERT_PRED2(are_equal_with_hilites, left, right);
} }
} }