From 8ddd8c8ba956d5cabc23121fbe633e3135b7b5b2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 25 Apr 2012 17:55:36 +0200 Subject: Change QStringBuilder to use UTF-8 too This commit completes the previous commit so that both QString and QStringBuilder now operate on UTF-8 input. A small fix was required in QStringBuilder: an if clause isn't enough to separate the two append versions. Since there are no QString functions that append to char*, if we're converting to a QByteArray, we need to go through a QString first in a separate function. Change-Id: Ic503340c5d0c32d420c90c91cc2e0fc1ae9230f3 Reviewed-by: Lars Knoll --- src/corelib/tools/qstringbuilder.h | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'src/corelib/tools/qstringbuilder.h') diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 9b1cd1ee7e..6b258eb09a 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -381,15 +381,11 @@ operator+(const A &a, const B &b) } #endif +namespace QtStringBuilder { template -QByteArray &operator+=(QByteArray &a, const QStringBuilder &b) +QByteArray &appendToByteArray(QByteArray &a, const QStringBuilder &b, char) { -#ifndef QT_NO_CAST_TO_ASCII - if (sizeof(typename QConcatenable< QStringBuilder >::ConvertTo::value_type) == sizeof(QChar)) { - //it is not save to optimize as in utf8 it is not possible to compute the size - return a += QString(b); - } -#endif + // append 8-bit data to a byte array int len = a.size() + QConcatenable< QStringBuilder >::size(b); a.reserve(len); char *it = a.data() + a.size(); @@ -398,6 +394,23 @@ QByteArray &operator+=(QByteArray &a, const QStringBuilder &b) return a; } +#ifndef QT_NO_CAST_TO_ASCII +template +QByteArray &appendToByteArray(QByteArray &a, const QStringBuilder &b, QChar) +{ + // append UTF-16 data to the byte array + return a += QString(b); +} +#endif +} + +template +QByteArray &operator+=(QByteArray &a, const QStringBuilder &b) +{ + return QtStringBuilder::appendToByteArray(a, b, + typename QConcatenable< QStringBuilder >::ConvertTo::value_type()); +} + template QString &operator+=(QString &a, const QStringBuilder &b) { -- cgit v1.2.3