diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-04-10 15:29:59 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-04-19 11:07:11 +0000 |
commit | b3e66c76aaadb979bec487869ce1fb7ee056b517 (patch) | |
tree | b60a9eed418f75a9662da0a47ac60599350ca724 /src | |
parent | d5cecfe8fda66ee64807765d669b0248cfda3dcc (diff) |
QUuid: de-duplicate uuid → string formatting
The code was duplicated for QChar and char characters. Keep only the
char version, and use QString::fromLatin1() to convert to QString.
This does not perform more allocations. It just copies 38 bytes more
than before.
Saves 788B in text size on optimized GCC 6.1 Linux AMD64 builds.
Change-Id: I1a65c8128eb2097e11527961618d54ea362e1b80
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/plugin/quuid.cpp | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 0afce7fcf0..3b010b3ae1 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -49,16 +50,20 @@ #endif QT_BEGIN_NAMESPACE -template <class Char, class Integral> -void _q_toHex(Char *&dst, Integral value) +// 16 bytes (a uint, two shorts and a uchar[8]), each represented by two hex +// digits; plus four dashes and a pair of enclosing brace: 16*2 + 4 + 2 = 38. +enum { MaxStringUuidLength = 38 }; + +template <class Integral> +void _q_toHex(char *&dst, Integral value) { value = qToBigEndian(value); const char* p = reinterpret_cast<const char*>(&value); for (uint i = 0; i < sizeof(Integral); ++i, dst += 2) { - dst[0] = Char(QtMiscUtils::toHexLower((p[i] >> 4) & 0xf)); - dst[1] = Char(QtMiscUtils::toHexLower(p[i] & 0xf)); + dst[0] = QtMiscUtils::toHexLower((p[i] >> 4) & 0xf); + dst[1] = QtMiscUtils::toHexLower(p[i] & 0xf); } } @@ -79,22 +84,22 @@ bool _q_fromHex(const Char *&src, Integral &value) return true; } -template <class Char> -void _q_uuidToHex(Char *&dst, const uint &d1, const ushort &d2, const ushort &d3, const uchar (&d4)[8]) +static char *_q_uuidToHex(const QUuid &uuid, char *dst) { - *dst++ = Char('{'); - _q_toHex(dst, d1); - *dst++ = Char('-'); - _q_toHex(dst, d2); - *dst++ = Char('-'); - _q_toHex(dst, d3); - *dst++ = Char('-'); + *dst++ = '{'; + _q_toHex(dst, uuid.data1); + *dst++ = '-'; + _q_toHex(dst, uuid.data2); + *dst++ = '-'; + _q_toHex(dst, uuid.data3); + *dst++ = '-'; for (int i = 0; i < 2; i++) - _q_toHex(dst, d4[i]); - *dst++ = Char('-'); + _q_toHex(dst, uuid.data4[i]); + *dst++ = '-'; for (int i = 2; i < 8; i++) - _q_toHex(dst, d4[i]); - *dst = Char('}'); + _q_toHex(dst, uuid.data4[i]); + *dst++ = '}'; + return dst; } template <class Char> @@ -548,12 +553,11 @@ QUuid QUuid::fromRfc4122(const QByteArray &bytes) */ QString QUuid::toString() const { - QString result(38, Qt::Uninitialized); - ushort *data = (ushort *)result.data(); - - _q_uuidToHex(data, data1, data2, data3, data4); - - return result; + char latin1[MaxStringUuidLength]; + const auto end = _q_uuidToHex(*this, latin1); + Q_ASSERT(end - latin1 == MaxStringUuidLength); + Q_UNUSED(end); + return QString::fromLatin1(latin1, MaxStringUuidLength); } /*! @@ -594,11 +598,10 @@ QString QUuid::toString() const */ QByteArray QUuid::toByteArray() const { - QByteArray result(38, Qt::Uninitialized); - char *data = result.data(); - - _q_uuidToHex(data, data1, data2, data3, data4); - + QByteArray result(MaxStringUuidLength, Qt::Uninitialized); + const auto end = _q_uuidToHex(*this, const_cast<char*>(result.constData())); + Q_ASSERT(end - result.constData() == MaxStringUuidLength); + Q_UNUSED(end); return result; } |