summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2018-01-24 14:09:08 -0800
committerThiago Macieira <thiago.macieira@intel.com>2018-02-03 21:30:37 +0000
commit687dc7fac7fec6c2f77aa1d130119a8bc1fd368b (patch)
tree83f67123c1430067ae497f84a4153d0509f2c3a5 /src
parent017569f702b6dd0bc3aa077d85e4a7fc27562133 (diff)
QUuid: add a way to get the string form without the braces
While we're at it, add a way to get it without the dashes too. I'm calling it "id128", as in "128-bit ID", as seen in journald's sd_id128_t type and the sd_id128_xxx() API. [ChangeLog][QtCore][QUuid] Added a parameter to both toString() and toByteArray() to allow controlling the use or not of the braces and dashes in the string form. Change-Id: I56b444f9d6274221a3b7fffd150cde706cfc5098 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/plugin/quuid.cpp119
-rw-r--r--src/corelib/plugin/quuid.h9
2 files changed, 121 insertions, 7 deletions
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;
}
@@ -307,6 +313,22 @@ static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCrypto
*/
/*!
+ \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)
Casts a Windows \a guid to a Qt QUuid.
@@ -592,6 +614,47 @@ QString QUuid::toString() const
}
/*!
+ \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
curly braces, i.e., "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}" where
@@ -637,6 +700,48 @@ QByteArray QUuid::toByteArray() const
}
/*!
+ \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<char*>(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 -
"Layout and byte order".
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;