diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 796ee01602..facd732609 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -351,6 +351,39 @@ QUuid::QUuid(const char *text) return; } } + +/*! + Creates a QUuid object from the QByteArray \a text, which must be + formatted as five hex fields separated by '-', e.g., + "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where 'x' is a hex + digit. The curly braces shown here are optional, but it is normal to + include them. If the conversion fails, a null UUID is created. See + toByteArray() for an explanation of how the five hex fields map to the + public data members in QUuid. + + \since 4.8 + + \sa toByteArray(), QUuid() +*/ +QUuid::QUuid(const QByteArray &text) +{ + if (text.length() < 36) { + *this = QUuid(); + return; + } + + const char *data = text.constData(); + + if (*data == '{' && text.length() < 37) { + *this = QUuid(); + return; + } + + if (!_q_uuidFromHex(data, data1, data2, data3, data4)) { + *this = QUuid(); + return; + } +} #endif /*! @@ -418,6 +451,52 @@ QString QUuid::toString() const return result; } + +/*! + Returns the binary representation of this QUuid. The byte array is + formatted as five hex fields separated by '-' and enclosed in + curly braces, i.e., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where + 'x' is a hex digit. From left to right, the five hex fields are + obtained from the four public data members in QUuid as follows: + + \table + \header + \o Field # + \o Source + + \row + \o 1 + \o data1 + + \row + \o 2 + \o data2 + + \row + \o 3 + \o data3 + + \row + \o 4 + \o data4[0] .. data4[1] + + \row + \o 5 + \o data4[2] .. data4[7] + + \endtable + + \since 4.8 +*/ +QByteArray QUuid::toByteArray() const +{ + QByteArray result(38, Qt::Uninitialized); + char *data = result.data(); + + _q_uuidToHex(data, data1, data2, data3, data4); + + return result; +} #endif #ifndef QT_NO_DATASTREAM diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index e9afacc560..69c4044e3c 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -109,6 +109,8 @@ struct Q_CORE_EXPORT QUuid QUuid(const char *); QString toString() const; operator QString() const { return toString(); } // ### Qt5 remove + QUuid(const QByteArray &); + QByteArray toByteArray() const; #endif bool isNull() const; diff --git a/tests/auto/quuid/tst_quuid.cpp b/tests/auto/quuid/tst_quuid.cpp index 6e067b7d24..3abc8af4c0 100644 --- a/tests/auto/quuid/tst_quuid.cpp +++ b/tests/auto/quuid/tst_quuid.cpp @@ -63,6 +63,8 @@ private slots: void fromChar(); void toString(); void fromString(); + void toByteArray(); + void fromByteArray(); void check_QDataStream(); void isNull(); void equal(); @@ -127,6 +129,24 @@ void tst_QUuid::fromString() QCOMPARE(uuidB, QUuid(QString("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}"))); } +void tst_QUuid::toByteArray() +{ + QCOMPARE(uuidA.toByteArray(), QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}")); + + QCOMPARE(uuidB.toByteArray(), QByteArray("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}")); +} + +void tst_QUuid::fromByteArray() +{ + QCOMPARE(uuidA, QUuid(QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5}"))); + QCOMPARE(uuidA, QUuid(QByteArray("fc69b59e-cc34-4436-a43c-ee95d128b8c5}"))); + QCOMPARE(uuidA, QUuid(QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c5"))); + QCOMPARE(uuidA, QUuid(QByteArray("fc69b59e-cc34-4436-a43c-ee95d128b8c5"))); + QCOMPARE(QUuid(), QUuid(QByteArray("{fc69b59e-cc34-4436-a43c-ee95d128b8c"))); + + QCOMPARE(uuidB, QUuid(QByteArray("{1ab6e93a-b1cb-4a87-ba47-ec7e99039a7b}"))); +} + void tst_QUuid::check_QDataStream() { QUuid tmp; diff --git a/tests/benchmarks/corelib/plugin/quuid/tst_quuid.cpp b/tests/benchmarks/corelib/plugin/quuid/tst_quuid.cpp index 61876e5816..24754ab370 100644 --- a/tests/benchmarks/corelib/plugin/quuid/tst_quuid.cpp +++ b/tests/benchmarks/corelib/plugin/quuid/tst_quuid.cpp @@ -56,6 +56,8 @@ private slots: void fromChar(); void toString(); void fromString(); + void toByteArray(); + void fromByteArray(); void toDataStream(); void fromDataStream(); void isNull(); @@ -93,6 +95,22 @@ void tst_bench_QUuid::fromString() } } +void tst_bench_QUuid::toByteArray() +{ + QUuid uuid = QUuid::createUuid(); + QBENCHMARK { + uuid.toByteArray(); + } +} + +void tst_bench_QUuid::fromByteArray() +{ + QByteArray string = "{67C8770B-44F1-410A-AB9A-F9B5446F13EE}"; + QBENCHMARK { + QUuid uuid(string); + } +} + void tst_bench_QUuid::toDataStream() { QUuid uuid1, uuid2;