From c951908bc201afa59402967d50fa926212845fae Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 23 Feb 2012 07:43:30 +0100 Subject: [PATCH] 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 Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index f1bad5c..12a5acb 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -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); -- 2.7.4