From e0d87f70d87a63aa55a11ecb91f8d355767d61bd Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 21 May 2024 10:53:43 +0200 Subject: [PATCH] Replace QUuid::toRfc4122() with toBytes() where possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They're content-equivalent, except that the latter doesn't have to allocate a return value on the heap. Pick-to: 6.7 Change-Id: Ifcae47b487c80c2bac02900f08393b386cfe806c Reviewed-by: MÃ¥rten Nordheim --- src/corelib/kernel/qcore_foundation.mm | 11 +++++++---- src/corelib/plugin/quuid.cpp | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/corelib/kernel/qcore_foundation.mm b/src/corelib/kernel/qcore_foundation.mm index a31040944f1..6d2451e078e 100644 --- a/src/corelib/kernel/qcore_foundation.mm +++ b/src/corelib/kernel/qcore_foundation.mm @@ -300,8 +300,8 @@ QUuid QUuid::fromCFUUID(CFUUIDRef uuid) */ CFUUIDRef QUuid::toCFUUID() const { - const QByteArray bytes = toRfc4122(); - return CFUUIDCreateFromUUIDBytes(0, *reinterpret_cast(bytes.constData())); + const auto bytes = toBytes(); + return CFUUIDCreateFromUUIDBytes(0, *reinterpret_cast(&bytes)); } /*! @@ -333,8 +333,11 @@ QUuid QUuid::fromNSUUID(const NSUUID *uuid) */ NSUUID *QUuid::toNSUUID() const { - const QByteArray bytes = toRfc4122(); - return [[[NSUUID alloc] initWithUUIDBytes:*reinterpret_cast(bytes.constData())] autorelease]; + const auto bytes = toBytes(); + static_assert(sizeof bytes == sizeof(uuid_t)); + uuid_t u; + memcpy(&u, &bytes, sizeof(uuid_t)); + return [[[NSUUID alloc] initWithUUIDBytes:u] autorelease]; } // ---------------------------------------------------------------------------- diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 42f21eea88c..8c3b758aaca 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -119,7 +119,7 @@ static QUuid _q_uuidFromHex(const char *src) static QUuid createFromName(const QUuid &ns, const QByteArray &baseData, QCryptographicHash::Algorithm algorithm, int version) { QCryptographicHash hash(algorithm); - hash.addData(ns.toRfc4122()); + hash.addData(QByteArrayView{ns.toBytes()}); hash.addData(baseData); QByteArrayView hashResult = hash.resultView(); Q_ASSERT(hashResult.size() >= 16);