diff options
| author | Ulf Hermann <ulf.hermann@qt.io> | 2025-04-10 17:11:42 +0200 |
|---|---|---|
| committer | Ulf Hermann <ulf.hermann@qt.io> | 2025-04-11 15:50:24 +0200 |
| commit | 916f5b51c8ec3595e054e4bf8e62949258a552ee (patch) | |
| tree | 46ca2654d47ad1314318f414b42786cbcaf4d42c /src/qmlcompiler | |
| parent | f181086eeb370f7ba7fabc8434c77b2768a0afee (diff) | |
qmltc: Properly escape translation strings
We have QQmlJSUtils::toLiteral() for that. Extend it to handle byte
arrays in addition to strings and use it.
Fixes: QTBUG-134726
Change-Id: Ibde1f56b25794fc8c49b796303c4f39933aedb42
Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io>
Diffstat (limited to 'src/qmlcompiler')
| -rw-r--r-- | src/qmlcompiler/qqmljsutils_p.h | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/qmlcompiler/qqmljsutils_p.h b/src/qmlcompiler/qqmljsutils_p.h index 56aec3dd78..1b4a5b707d 100644 --- a/src/qmlcompiler/qqmljsutils_p.h +++ b/src/qmlcompiler/qqmljsutils_p.h @@ -73,13 +73,13 @@ struct Q_QMLCOMPILER_EXPORT QQmlJSUtils Returns escaped version of \a s. This function is mostly useful for code generators. */ - static QString escapeString(QString s) + template<typename String, typename CharacterLiteral, typename StringView> + static String escapeString(String s) { - using namespace Qt::StringLiterals; - return s.replace('\\'_L1, "\\\\"_L1) - .replace('"'_L1, "\\\""_L1) - .replace('\n'_L1, "\\n"_L1) - .replace('?'_L1, "\\?"_L1); + return s.replace(CharacterLiteral('\\'), StringView("\\\\")) + .replace(CharacterLiteral('"'), StringView("\\\"")) + .replace(CharacterLiteral('\n'), StringView("\\n")) + .replace(CharacterLiteral('?'), StringView("\\?")); } /*! \internal @@ -89,9 +89,14 @@ struct Q_QMLCOMPILER_EXPORT QQmlJSUtils \note This function escapes \a s before wrapping it. */ - static QString toLiteral(const QString &s, QStringView ctor = u"QStringLiteral") + template< + typename String = QString, + typename CharacterLiteral = QLatin1Char, + typename StringView = QLatin1StringView> + static String toLiteral(const String &s, StringView ctor = StringView("QStringLiteral")) { - return ctor % u"(\"" % escapeString(s) % u"\")"; + return ctor % StringView("(\"") + % escapeString<String, CharacterLiteral, StringView>(s) % StringView("\")"); } /*! \internal |
