QString: add from{Ascii,Latin1,Utf8,Local8Bit() overloads for QByteArray
authorMarc Mutz <marc.mutz@kdab.com>
Thu, 23 Feb 2012 06:43:30 +0000 (07:43 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 23 Feb 2012 13:57:06 +0000 (14:57 +0100)
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>
src/corelib/tools/qstring.h

index f1bad5c..12a5acb 100644 (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);