Add Base::Tools::escapeQuotesFromString with corresponding test
To be used to avoid sending unescaped quotes to python console
This commit is contained in:
committed by
Chris Hennes
parent
f34861c99b
commit
808e5d9d74
@@ -236,6 +236,26 @@ std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s)
|
||||
return string;
|
||||
}
|
||||
|
||||
std::string Base::Tools::escapeQuotesFromString(const std::string& s)
|
||||
{
|
||||
std::string result;
|
||||
size_t len = s.size();
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
switch (s.at(i)) {
|
||||
case '\"':
|
||||
result += "\\\"";
|
||||
break;
|
||||
case '\'':
|
||||
result += "\\\'";
|
||||
break;
|
||||
default:
|
||||
result += s.at(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Base::Tools::escapeEncodeString(const QString& s)
|
||||
{
|
||||
QString result;
|
||||
|
||||
@@ -297,6 +297,7 @@ struct BaseExport Tools
|
||||
static std::string narrow(const std::wstring& str);
|
||||
static std::string escapedUnicodeFromUtf8(const char* s);
|
||||
static std::string escapedUnicodeToUtf8(const std::string& s);
|
||||
static std::string escapeQuotesFromString(const std::string& s);
|
||||
|
||||
static QString escapeEncodeString(const QString& s);
|
||||
static std::string escapeEncodeString(const std::string& s);
|
||||
|
||||
@@ -145,4 +145,10 @@ TEST(BaseToolsSuite, TestJoinList)
|
||||
{
|
||||
EXPECT_EQ(Base::Tools::joinList({"AB", "CD"}), "AB, CD, ");
|
||||
}
|
||||
TEST(BaseToolsSuite, TestEscapeQuotesFromString)
|
||||
{
|
||||
EXPECT_EQ(Base::Tools::escapeQuotesFromString("\'"), "\\\'");
|
||||
EXPECT_EQ(Base::Tools::escapeQuotesFromString("\""), "\\\"");
|
||||
EXPECT_EQ(Base::Tools::escapeQuotesFromString("\\"), "\\");
|
||||
}
|
||||
// NOLINTEND(cppcoreguidelines-*,readability-*)
|
||||
|
||||
Reference in New Issue
Block a user