diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2021-07-07 07:40:08 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2021-07-28 17:50:09 +0200 |
commit | 767e00a2d719e57dd60c7ae8776e6291c220b34d (patch) | |
tree | ad7ca0067d723244ea4ec00539ee7b34d34149bb /src/corelib | |
parent | 3a72496b5c43484a94882440993b0ca0cb842d8a (diff) |
QUuid: port fromRfc4122() to QByteArrayView
Remove the QByteArray overload from the API, but not the ABI.
Adapt callers.
Change-Id: I88aa09cbca3b89d0b249ce336ebe49c4b352c9e1
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/compat/removed_api.cpp | 7 | ||||
-rw-r--r-- | src/corelib/kernel/qcore_foundation.mm | 4 | ||||
-rw-r--r-- | src/corelib/plugin/quuid.cpp | 29 | ||||
-rw-r--r-- | src/corelib/plugin/quuid.h | 3 |
4 files changed, 26 insertions, 17 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 9bd7944c41..d5d71ce75a 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -57,6 +57,13 @@ QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method) return hash(QByteArrayView{data}, method); } +#include "quuid.h" + +QUuid QUuid::fromRfc4122(const QByteArray &bytes) +{ + return fromRfc4122(qToByteArrayViewIgnoringNull(bytes)); +} + // #include <qotherheader.h> // // implement removed functions from qotherheader.h diff --git a/src/corelib/kernel/qcore_foundation.mm b/src/corelib/kernel/qcore_foundation.mm index 5520b1fb24..958f0e2bc7 100644 --- a/src/corelib/kernel/qcore_foundation.mm +++ b/src/corelib/kernel/qcore_foundation.mm @@ -321,7 +321,7 @@ QUuid QUuid::fromCFUUID(CFUUIDRef uuid) if (!uuid) return QUuid(); const CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid); - return QUuid::fromRfc4122(QByteArray::fromRawData(reinterpret_cast<const char *>(&bytes), sizeof(bytes))); + return QUuid::fromRfc4122(QByteArrayView(reinterpret_cast<const char *>(&bytes), sizeof(bytes))); } /*! @@ -354,7 +354,7 @@ QUuid QUuid::fromNSUUID(const NSUUID *uuid) return QUuid(); uuid_t bytes; [uuid getUUIDBytes:bytes]; - return QUuid::fromRfc4122(QByteArray::fromRawData(reinterpret_cast<const char *>(bytes), sizeof(bytes))); + return QUuid::fromRfc4122(QByteArrayView(reinterpret_cast<const char *>(bytes), sizeof(bytes))); } /*! diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index be67898f8a..ea78fff5b3 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -151,16 +151,12 @@ static QUuid _q_uuidFromHex(const char *src) static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCryptographicHash::Algorithm algorithm, int version) { - QByteArray hashResult; - - // create a scope so later resize won't reallocate - { - QCryptographicHash hash(algorithm); - hash.addData(ns.toRfc4122()); - hash.addData(baseData); - hashResult = hash.result(); - } - hashResult.resize(16); // Sha1 will be too long + QCryptographicHash hash(algorithm); + hash.addData(ns.toRfc4122()); + hash.addData(baseData); + QByteArrayView hashResult = hash.resultView(); + Q_ASSERT(hashResult.size() >= 16); + hashResult.truncate(16); // Sha1 will be too long QUuid result = QUuid::fromRfc4122(hashResult); @@ -524,20 +520,23 @@ QUuid QUuid::createUuidV5(const QUuid &ns, const QByteArray &baseData) If the conversion fails, a null UUID is created. + \note In Qt versions prior to 6.3, this function took QByteArray, not + QByteArrayView. + \since 4.8 \sa toRfc4122(), QUuid() */ -QUuid QUuid::fromRfc4122(const QByteArray &bytes) +QUuid QUuid::fromRfc4122(QByteArrayView bytes) noexcept { - if (bytes.isEmpty() || bytes.length() != 16) + if (bytes.isEmpty() || bytes.size() != 16) return QUuid(); uint d1; ushort d2, d3; uchar d4[8]; - const uchar *data = reinterpret_cast<const uchar *>(bytes.constData()); + const uchar *data = reinterpret_cast<const uchar *>(bytes.data()); d1 = qFromBigEndian<quint32>(data); data += sizeof(quint32); @@ -744,7 +743,7 @@ QDataStream &operator<<(QDataStream &s, const QUuid &id) */ QDataStream &operator>>(QDataStream &s, QUuid &id) { - QByteArray bytes(16, Qt::Uninitialized); + std::array<char, 16> bytes; if (s.readRawData(bytes.data(), 16) != 16) { s.setStatus(QDataStream::ReadPastEnd); return s; @@ -753,7 +752,7 @@ QDataStream &operator>>(QDataStream &s, QUuid &id) if (s.byteOrder() == QDataStream::BigEndian) { id = QUuid::fromRfc4122(bytes); } else { - const uchar *data = reinterpret_cast<const uchar *>(bytes.constData()); + const uchar *data = reinterpret_cast<const uchar *>(bytes.data()); id.data1 = qFromLittleEndian<quint32>(data); data += sizeof(quint32); diff --git a/src/corelib/plugin/quuid.h b/src/corelib/plugin/quuid.h index 98ecced530..9192542684 100644 --- a/src/corelib/plugin/quuid.h +++ b/src/corelib/plugin/quuid.h @@ -105,7 +105,10 @@ public: explicit QUuid(const QByteArray &); QByteArray toByteArray(StringFormat mode = WithBraces) const; QByteArray toRfc4122() const; +#if QT_REMOVED_SINCE(6, 3) static QUuid fromRfc4122(const QByteArray &); +#endif + static QUuid fromRfc4122(QByteArrayView) noexcept; bool isNull() const noexcept; constexpr bool operator==(const QUuid &orig) const noexcept |