Fix escaping of special chars for copyAsCString
This commit is contained in:
parent
5f2857f12a
commit
05bca069e3
2 changed files with 36 additions and 1 deletions
|
|
@ -107,6 +107,24 @@ void copySelectionToClipboard(const QTableView *view)
|
|||
}
|
||||
}
|
||||
|
||||
QString EscapeForCString(QString qi)
|
||||
{
|
||||
int escape_count = 0;
|
||||
for (QChar c : qi)
|
||||
if (c == '"' || c == '\\') ++escape_count;
|
||||
|
||||
QString out;
|
||||
out.reserve(qi.size() + escape_count);
|
||||
for (QChar c : qi)
|
||||
if (c == '"' || c == '\\') {
|
||||
out += "\\";
|
||||
out += c;
|
||||
}
|
||||
else
|
||||
out += c;
|
||||
return out;
|
||||
}
|
||||
|
||||
QString ConvertToMultiLineCString(const QString &in_)
|
||||
{
|
||||
// We need to atleast escape " and \ and also any multi byte utf8 char
|
||||
|
|
@ -170,7 +188,7 @@ QString ConvertToMultiLineCString(const QString &in_)
|
|||
comment.clear();
|
||||
}
|
||||
else {
|
||||
line += token.out;
|
||||
line += EscapeForCString(token.out);
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -106,3 +106,20 @@ FROM tab
|
|||
ASSERT_EQ(output, expected);
|
||||
}
|
||||
|
||||
TEST(ConvertToMultiLineCString, escapeDoubleQuote)
|
||||
{
|
||||
QString in(R"__(SELECT * FROM "table")__");
|
||||
QString expected("\"SELECT * FROM \\\"table\\\"\"");
|
||||
|
||||
auto output = ConvertToMultiLineCString(in);
|
||||
ASSERT_EQ(output, expected);
|
||||
}
|
||||
|
||||
TEST(ConvertToMultiLineCString, escapeBackslash)
|
||||
{
|
||||
QString in(R"__(SELECT '\test')__");
|
||||
QString expected(R"__("SELECT '\\test'")__");
|
||||
|
||||
auto output = ConvertToMultiLineCString(in);
|
||||
ASSERT_EQ(output, expected);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue