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.cpp | 16 ++++++++++++++-- src/corelib/tools/qstringbuilder.h | 27 ++++++++++++++++++++------- 2 files changed, 34 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qstringbuilder.cpp b/src/corelib/tools/qstringbuilder.cpp index a044cca3c9..2fe556096b 100644 --- a/src/corelib/tools/qstringbuilder.cpp +++ b/src/corelib/tools/qstringbuilder.cpp @@ -108,12 +108,24 @@ void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out if (len == -1) { if (!a) return; - while (*a) + while (*a && uchar(*a) < 0x80U) *out++ = QLatin1Char(*a++); + if (!*a) + return; } else { - for (int i = 0; i < len; ++i) + int i; + for (i = 0; i < len && uchar(a[i]) < 0x80U; ++i) *out++ = QLatin1Char(a[i]); + if (i == len) + return; + a += i; + len -= i; } + + // we need to complement with UTF-8 appending + QString tmp = QString::fromUtf8(a, len); + memcpy(out, reinterpret_cast(tmp.constData()), sizeof(QChar) * tmp.size()); + out += tmp.size(); } QT_END_NAMESPACE 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