From bbe65832f57709ac14bccfe0d5d06723f1397d1d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 4 Nov 2015 13:11:23 +0100 Subject: [PATCH] QString::vasprintf: avoid allocating memory for format text copied verbatim ... by using the new QUtf8::convertToUnicode() function in conjunction with QString::resize(). For this to be a clear optimization, resize() must be banned from shrinking capacity, which another patch will implement. Change-Id: I6394af95ef1aaae131e23a4227734eb76fa685cd Reviewed-by: Oswald Buddenhagen --- src/corelib/tools/qstring.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index b965e35175e..7e8eb5e317f 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -5867,6 +5867,14 @@ QString &QString::vsprintf(const char *cformat, va_list ap) return *this = vasprintf(cformat, ap); } +static void append_utf8(QString &qs, const char *cs, int len) +{ + const int oldSize = qs.size(); + qs.resize(oldSize + len); + const QChar *newEnd = QUtf8::convertToUnicode(qs.data() + oldSize, cs, len); + qs.resize(newEnd - qs.constData()); +} + static uint parse_flag_characters(const char * &c) Q_DECL_NOTHROW { uint flags = 0; @@ -5929,7 +5937,7 @@ QString QString::vasprintf(const char *cformat, va_list ap) const char *cb = c; while (*c != '\0' && *c != '%') c++; - result.append(QString::fromUtf8(cb, (int)(c - cb))); + append_utf8(result, cb, int(c - cb)); if (*c == '\0') break;