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 <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
Marc Mutz 2015-11-04 13:11:23 +01:00
parent db9a0befca
commit bbe65832f5

View File

@ -5867,6 +5867,14 @@ QString &QString::vsprintf(const char *cformat, va_list ap)
return *this = vasprintf(cformat, 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 static uint parse_flag_characters(const char * &c) Q_DECL_NOTHROW
{ {
uint flags = 0; uint flags = 0;
@ -5929,7 +5937,7 @@ QString QString::vasprintf(const char *cformat, va_list ap)
const char *cb = c; const char *cb = c;
while (*c != '\0' && *c != '%') while (*c != '\0' && *c != '%')
c++; c++;
result.append(QString::fromUtf8(cb, (int)(c - cb))); append_utf8(result, cb, int(c - cb));
if (*c == '\0') if (*c == '\0')
break; break;