diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 3a1c0495fe..4d658e064c 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -83,21 +83,27 @@ bool _q_fromHex(const char *&src, Integral &value) return true; } -static char *_q_uuidToHex(const QUuid &uuid, char *dst) +static char *_q_uuidToHex(const QUuid &uuid, char *dst, QUuid::StringFormat mode = QUuid::WithBraces) { - *dst++ = '{'; + if ((mode & QUuid::WithoutBraces) == 0) + *dst++ = '{'; _q_toHex(dst, uuid.data1); - *dst++ = '-'; + if ((mode & QUuid::Id128) != QUuid::Id128) + *dst++ = '-'; _q_toHex(dst, uuid.data2); - *dst++ = '-'; + if ((mode & QUuid::Id128) != QUuid::Id128) + *dst++ = '-'; _q_toHex(dst, uuid.data3); - *dst++ = '-'; + if ((mode & QUuid::Id128) != QUuid::Id128) + *dst++ = '-'; for (int i = 0; i < 2; i++) _q_toHex(dst, uuid.data4[i]); - *dst++ = '-'; + if ((mode & QUuid::Id128) != QUuid::Id128) + *dst++ = '-'; for (int i = 2; i < 8; i++) _q_toHex(dst, uuid.data4[i]); - *dst++ = '}'; + if ((mode & QUuid::WithoutBraces) == 0) + *dst++ = '}'; return dst; } @@ -306,6 +312,22 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto and null UUIDs return true from isNull(). */ +/*! + \enum QUuid::StringFormat + \since 5.11 + + This enum is used by toString(StringFormat) to control the formatting of the + string representation. The possible values are: + + \value WithBraces The default, toString() will return five hex fields, separated by + dashes and surrounded by braces. Example: + {00000000-0000-0000-0000-000000000000}. + \value WithoutBraces Only the five dash-separated fields, without the braces. Example: + 00000000-0000-0000-0000-000000000000. + \value Id128 Only the hex digits, without braces or dashes. Note that QUuid + cannot parse this back again as input. +*/ + /*! \fn QUuid::QUuid(const GUID &guid) @@ -591,6 +613,47 @@ QString QUuid::toString() const return QString::fromLatin1(latin1, MaxStringUuidLength); } +/*! + \since 5.11 + + Returns the string representation of this QUuid, with the formattiong + controlled by the \a mode parameter. From left to right, the five hex + fields are obtained from the four public data members in QUuid as follows: + + \table + \header + \li Field # + \li Source + + \row + \li 1 + \li data1 + + \row + \li 2 + \li data2 + + \row + \li 3 + \li data3 + + \row + \li 4 + \li data4[0] .. data4[1] + + \row + \li 5 + \li data4[2] .. data4[7] + + \endtable +*/ +QString QUuid::toString(QUuid::StringFormat mode) const +{ + char latin1[MaxStringUuidLength]; + const auto end = _q_uuidToHex(*this, latin1, mode); + return QString::fromLatin1(latin1, end - latin1); +} + /*! Returns the binary representation of this QUuid. The byte array is formatted as five hex fields separated by '-' and enclosed in @@ -636,6 +699,48 @@ QByteArray QUuid::toByteArray() const return result; } +/*! + \since 5.11 + + Returns the string representation of this QUuid, with the formattiong + controlled by the \a mode parameter. From left to right, the five hex + fields are obtained from the four public data members in QUuid as follows: + + \table + \header + \li Field # + \li Source + + \row + \li 1 + \li data1 + + \row + \li 2 + \li data2 + + \row + \li 3 + \li data3 + + \row + \li 4 + \li data4[0] .. data4[1] + + \row + \li 5 + \li data4[2] .. data4[7] + + \endtable +*/ +QByteArray QUuid::toByteArray(QUuid::StringFormat mode) const +{ + QByteArray result(MaxStringUuidLength, Qt::Uninitialized); + const auto end = _q_uuidToHex(*this, const_cast(result.constData()), mode); + result.resize(end - result.constData()); + return result; +} + /*! Returns the binary representation of this QUuid. The byte array is in big endian format, and formatted according to RFC 4122, section 4.1.2 - diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index ee0a9f9dac..27a84b4e01 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -85,7 +85,14 @@ public: Sha1 = 5 // 0 1 0 1 }; + enum StringFormat { + WithBraces = 0, + WithoutBraces = 1, + Id128 = 3 + }; + #if defined(Q_COMPILER_UNIFORM_INIT) && !defined(Q_CLANG_QDOC) + Q_DECL_CONSTEXPR QUuid() Q_DECL_NOTHROW : data1(0), data2(0), data3(0), data4{0,0,0,0,0,0,0,0} {} Q_DECL_CONSTEXPR QUuid(uint l, ushort w1, ushort w2, uchar b1, uchar b2, uchar b3, @@ -121,8 +128,10 @@ public: static QUuid fromString(QLatin1String string) Q_DECL_NOTHROW; QUuid(const char *); QString toString() const; + QString toString(StringFormat mode) const; // ### Qt6: merge with previous QUuid(const QByteArray &); QByteArray toByteArray() const; + QByteArray toByteArray(StringFormat mode) const; // ### Qt6: merge with previous QByteArray toRfc4122() const; static QUuid fromRfc4122(const QByteArray &); bool isNull() const Q_DECL_NOTHROW; diff --git a/tests/auto/corelib/plugin/quuid/tst_quuid.cpp b/tests/auto/corelib/plugin/quuid/tst_quuid.cpp index 9e3edc96ec..bad72c081b 100644 --- a/tests/auto/corelib/plugin/quuid/tst_quuid.cpp +++ b/tests/auto/corelib/plugin/quuid/tst_quuid.cpp @@ -124,8 +124,16 @@ void tst_QUuid::fromChar() void tst_QUuid::toString() { QCOMPARE(uuidA.toString(), QString("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}")); + QCOMPARE(uuidA.toString(QUuid::WithoutBraces), + QString("fc69b59e-cc34-4436-a43c-ee95d128b8c5")); + QCOMPARE(uuidA.toString(QUuid::Id128), + QString("fc69b59ecc344436a43cee95d128b8c5")); QCOMPARE(uuidB.toString(), QString("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}")); + QCOMPARE(uuidB.toString(QUuid::WithoutBraces), + QString("1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b")); + QCOMPARE(uuidB.toString(QUuid::Id128), + QString("1ab6e93ab1cb4a87ba47ec7e99039a7b")); } void tst_QUuid::fromString_data() @@ -185,8 +193,16 @@ void tst_QUuid::fromString() void tst_QUuid::toByteArray() { QCOMPARE(uuidA.toByteArray(), QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}")); + QCOMPARE(uuidA.toByteArray(QUuid::WithoutBraces), + QByteArray("fc69b59e-cc34-4436-a43c-ee95d128b8c5")); + QCOMPARE(uuidA.toByteArray(QUuid::Id128), + QByteArray("fc69b59ecc344436a43cee95d128b8c5")); QCOMPARE(uuidB.toByteArray(), QByteArray("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}")); + QCOMPARE(uuidB.toByteArray(QUuid::WithoutBraces), + QByteArray("1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b")); + QCOMPARE(uuidB.toByteArray(QUuid::Id128), + QByteArray("1ab6e93ab1cb4a87ba47ec7e99039a7b")); } void tst_QUuid::fromByteArray()