From 6d05c6d75ae7206e63c22c173de398d5f22193e1 Mon Sep 17 00:00:00 2001 From: eelke Date: Fri, 8 Jul 2022 19:53:45 +0200 Subject: [PATCH] Improve escaping routine, no need to resort to using the E prefix just doubling the quotes should be enough. --- pglablib/SqlFormattingUtils.cpp | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/pglablib/SqlFormattingUtils.cpp b/pglablib/SqlFormattingUtils.cpp index 0b1c0a5..a75620e 100644 --- a/pglablib/SqlFormattingUtils.cpp +++ b/pglablib/SqlFormattingUtils.cpp @@ -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 + int num_quotes = 0; /* single or double, depending on as_ident */ 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"; + QString output; + output.reserve(output_size); 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 *