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>
This commit is contained in:
Marc Mutz 2017-04-10 15:29:59 +02:00
parent d5cecfe8fd
commit b3e66c76aa

View File

@ -1,6 +1,7 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** 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/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -49,16 +50,20 @@
#endif #endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
template <class Char, class Integral> // 16 bytes (a uint, two shorts and a uchar[8]), each represented by two hex
void _q_toHex(Char *&dst, Integral value) // 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); value = qToBigEndian(value);
const char* p = reinterpret_cast<const char*>(&value); const char* p = reinterpret_cast<const char*>(&value);
for (uint i = 0; i < sizeof(Integral); ++i, dst += 2) { for (uint i = 0; i < sizeof(Integral); ++i, dst += 2) {
dst[0] = Char(QtMiscUtils::toHexLower((p[i] >> 4) & 0xf)); dst[0] = QtMiscUtils::toHexLower((p[i] >> 4) & 0xf);
dst[1] = Char(QtMiscUtils::toHexLower(p[i] & 0xf)); dst[1] = QtMiscUtils::toHexLower(p[i] & 0xf);
} }
} }
@ -79,22 +84,22 @@ bool _q_fromHex(const Char *&src, Integral &value)
return true; return true;
} }
template <class Char> static char *_q_uuidToHex(const QUuid &uuid, char *dst)
void _q_uuidToHex(Char *&dst, const uint &d1, const ushort &d2, const ushort &d3, const uchar (&d4)[8])
{ {
*dst++ = Char('{'); *dst++ = '{';
_q_toHex(dst, d1); _q_toHex(dst, uuid.data1);
*dst++ = Char('-'); *dst++ = '-';
_q_toHex(dst, d2); _q_toHex(dst, uuid.data2);
*dst++ = Char('-'); *dst++ = '-';
_q_toHex(dst, d3); _q_toHex(dst, uuid.data3);
*dst++ = Char('-'); *dst++ = '-';
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
_q_toHex(dst, d4[i]); _q_toHex(dst, uuid.data4[i]);
*dst++ = Char('-'); *dst++ = '-';
for (int i = 2; i < 8; i++) for (int i = 2; i < 8; i++)
_q_toHex(dst, d4[i]); _q_toHex(dst, uuid.data4[i]);
*dst = Char('}'); *dst++ = '}';
return dst;
} }
template <class Char> template <class Char>
@ -548,12 +553,11 @@ QUuid QUuid::fromRfc4122(const QByteArray &bytes)
*/ */
QString QUuid::toString() const QString QUuid::toString() const
{ {
QString result(38, Qt::Uninitialized); char latin1[MaxStringUuidLength];
ushort *data = (ushort *)result.data(); const auto end = _q_uuidToHex(*this, latin1);
Q_ASSERT(end - latin1 == MaxStringUuidLength);
_q_uuidToHex(data, data1, data2, data3, data4); Q_UNUSED(end);
return QString::fromLatin1(latin1, MaxStringUuidLength);
return result;
} }
/*! /*!
@ -594,11 +598,10 @@ QString QUuid::toString() const
*/ */
QByteArray QUuid::toByteArray() const QByteArray QUuid::toByteArray() const
{ {
QByteArray result(38, Qt::Uninitialized); QByteArray result(MaxStringUuidLength, Qt::Uninitialized);
char *data = result.data(); const auto end = _q_uuidToHex(*this, const_cast<char*>(result.constData()));
Q_ASSERT(end - result.constData() == MaxStringUuidLength);
_q_uuidToHex(data, data1, data2, data3, data4); Q_UNUSED(end);
return result; return result;
} }