QString: add from{Ascii,Latin1,Utf8,Local8Bit() overloads for QByteArray

One of the more frequent uses for QByteArray::operator const char*()
is in passing a QByteArray to QString::fromLatin1().

But this is highly inefficient, since the bytearray already knows
its size, but since its demoted to a const char* in passing to
fromLatin1(), it forces the latter to call strlen() _again_.

The solution, then, is to add overloads for QByteArray that
pass the array's .size() as a second argument to the two-arg
fromLatin1() version.

Change-Id: I5ea1ad3c96d9e64167be53c0c418c7b7dba51f68
Reviewed-by: David Faure <faure@kde.org>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Marc Mutz 2012-02-23 07:43:30 +01:00 committed by Qt by Nokia
parent 949b7452e0
commit c951908bc2

View File

@ -417,6 +417,10 @@ public:
{
return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
}
static inline QString fromAscii(const QByteArray &str) { return fromAscii(str.data(), str.size()); }
static inline QString fromLatin1(const QByteArray &str) { return fromLatin1(str.data(), str.size()); }
static inline QString fromUtf8(const QByteArray &str) { return fromUtf8(str.data(), str.size()); }
static inline QString fromLocal8Bit(const QByteArray &str) { return fromLocal8Bit(str.data(), str.size()); }
static QString fromUtf16(const ushort *, int size = -1);
static QString fromUcs4(const uint *, int size = -1);
static QString fromRawData(const QChar *, int size);