Add QUuid::toByteArray() and relevant
authorLiang Qi <liang.qi@nokia.com>
Fri, 20 May 2011 09:36:01 +0000 (11:36 +0200)
committerQt Continuous Integration System <qt-info@nokia.com>
Fri, 27 May 2011 11:21:42 +0000 (13:21 +0200)
Add QUuid::toByteArray() and QUuid(const QByteArray &). Same
behavior with QUuid::toString() and QUuid(const QString &).

Task-number: QTBUG-19419
Reviewed-by: joao
(cherry picked from commit 71f923f29e2c60444a85fc765fc582e06cb7eca4)

Change-Id: I41dad65e269f739ba9ec1c27e9da96af6401356c
Reviewed-on: http://codereview.qt.nokia.com/167
Reviewed-by: Liang Qi <liang.qi@nokia.com>
src/corelib/plugin/quuid.cpp
src/corelib/plugin/quuid.h
tests/auto/quuid/tst_quuid.cpp
tests/benchmarks/corelib/plugin/quuid/tst_quuid.cpp

index 796ee01..facd732 100644 (file)
@@ -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
index e9afacc..69c4044 100644 (file)
@@ -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;
 
index 6e067b7..3abc8af 100644 (file)
@@ -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;
index 61876e5..24754ab 100644 (file)
@@ -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;