From 4ff09cdd47a1f21321a56017743401a7c6b91ad3 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 5 May 2020 15:45:47 +0200 Subject: [PATCH] Optimize appending to an existing string Optimize the common pattern of "str += decode(data);" and "bytearray += encode(string);" Change-Id: I1da621fa1ad400f23c9718ecf8ae64c00d9d459c Reviewed-by: Thiago Macieira --- src/corelib/text/qstringconverter.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/corelib/text/qstringconverter.h b/src/corelib/text/qstringconverter.h index 7443173df6..989773e113 100644 --- a/src/corelib/text/qstringconverter.h +++ b/src/corelib/text/qstringconverter.h @@ -327,6 +327,28 @@ struct QConcatenable> out = s.decoder->decodeIntoBuffer(out, s.data.data(), s.data.length()); } }; + +template +QString &operator+=(QString &a, const QStringDecoder::EncodedData &b) +{ + qsizetype len = a.size() + QConcatenable>::size(b); + a.reserve(len); + QChar *it = a.data() + a.size(); + QConcatenable>::appendTo(b, it); + a.resize(qsizetype(it - a.constData())); //may be smaller than len + return a; +} + +template +QByteArray &operator+=(QByteArray &a, const QStringEncoder::DecodedData &b) +{ + qsizetype len = a.size() + QConcatenable>::size(b); + a.reserve(len); + char *it = a.data() + a.size(); + QConcatenable>::appendTo(b, it); + a.resize(qsizetype(it - a.constData())); //may be smaller than len + return a; +} #endif QT_END_NAMESPACE