From cb2e53636583e5969113d21affb2caaf11ce7441 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 21 Apr 2022 07:43:42 -0700 Subject: [PATCH] qHash: pass QByteArrayView to qHash() by value The QBAV one should pass the parameter by value, like QStringView. And now that we have it, the non-View one should call the View one in an inline function, like we already do for QString. The extra, defaulted parameter is there only so we get a different signature in the new inline function compared to the removed one. Change-Id: If05aeeb7176e4f13af9afffd16e7f08062b1dc86 Reviewed-by: Marc Mutz (cherry picked from commit bc144312c18e7436275267ad8900fce980597c45) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/compat/removed_api.cpp | 12 ++++++++++++ src/corelib/tools/qhash.cpp | 7 +------ src/corelib/tools/qhashfunctions.h | 8 ++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 7c3dbd3be26..2b001429549 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -170,6 +170,18 @@ QCalendar::QCalendar(QLatin1StringView name) : QCalendar(QAnyStringView{name}) {} +#include "qhashfunctions.h" + +size_t qHash(const QByteArray &key, size_t seed) noexcept +{ + return qHashBits(key.constData(), size_t(key.size()), seed); +} + +size_t qHash(const QByteArrayView &key, size_t seed) noexcept +{ + return qHashBits(key.constData(), size_t(key.size()), seed); +} + #include "qobject.h" void QObject::setObjectName(const QString &name) diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index c973eaeabe9..2d9b6750c20 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -957,12 +957,7 @@ size_t qHashBits(const void *p, size_t size, size_t seed) noexcept return siphash(reinterpret_cast(p), size, seed, seed2); } -size_t qHash(const QByteArray &key, size_t seed) noexcept -{ - return qHashBits(key.constData(), size_t(key.size()), seed); -} - -size_t qHash(const QByteArrayView &key, size_t seed) noexcept +size_t qHash(QByteArrayView key, size_t seed) noexcept { return qHashBits(key.constData(), size_t(key.size()), seed); } diff --git a/src/corelib/tools/qhashfunctions.h b/src/corelib/tools/qhashfunctions.h index 04d165983da..58f4afc94f3 100644 --- a/src/corelib/tools/qhashfunctions.h +++ b/src/corelib/tools/qhashfunctions.h @@ -144,8 +144,16 @@ Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(std::nullptr_t, size_t seed // (some) Qt types Q_DECL_CONST_FUNCTION constexpr inline size_t qHash(const QChar key, size_t seed = 0) noexcept { return qHash(key.unicode(), seed); } + +#if QT_CORE_REMOVED_SINCE(6, 4) Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray &key, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArrayView &key, size_t seed = 0) noexcept; +#else +Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(QByteArrayView key, size_t seed = 0) noexcept; +inline Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray &key, size_t seed = 0 QT6_ONLY(, int = 0)) noexcept +{ return qHash(qToByteArrayViewIgnoringNull(key), seed); } +#endif + Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(QStringView key, size_t seed = 0) noexcept; #if QT_STRINGVIEW_LEVEL < 2 inline Q_DECL_PURE_FUNCTION size_t qHash(const QString &key, size_t seed = 0) noexcept