Improve escaping routine, no need to resort to using the E prefix just doubling the quotes should be enough.

This commit is contained in:
eelke 2022-07-08 19:53:45 +02:00
parent 8b671090a0
commit 6d05c6d75a

View file

@ -15,40 +15,27 @@
namespace {
QString escapeInternal(const QString &input, bool as_ident)
QString escapeInternal(const QString &input, QChar quote_char)
{
int num_quotes = 0; /* single or double, depending on as_ident */
int num_backslashes = 0;
QChar quote_char = as_ident ? '"' : '\'';
// Doorloop input
// tel quotes
// tel backslashes
const int len = input.length();
for (int idx = 0; idx < len; ++idx) {
QChar c = input[idx];
if (c == quote_char)
for (int idx = 0; idx < len; ++idx)
if (input[idx] == quote_char)
++num_quotes;
else if (c == '\\')
++num_backslashes;
}
int output_size = len + num_quotes + 2; // + 2 for the quotes
if (!as_ident && num_backslashes > 0)
output_size += num_backslashes + 2; // +2 so whe can add the " E"
QString output;
output.reserve(output_size);
if (!as_ident && num_backslashes > 0)
output += " E";
output += quote_char;
if (num_quotes == 0 && (num_backslashes == 0 || as_ident)) {
if (num_quotes == 0) {
output += input;
}
else {
for (int idx = 0; idx < len; ++idx) {
QChar c = input[idx];
output += c;
if (c == quote_char || (!as_ident && c == '\\'))
if (c == quote_char)
output += c;
}
}
@ -60,12 +47,12 @@ namespace {
QString escapeIdent(const QString &input)
{
return escapeInternal(input, true);
return escapeInternal(input, '"');
}
QString escapeLiteral(const QString &input)
{
return escapeInternal(input, false);
return escapeInternal(input, '\'');
}
//char *