qHash: make hashing of QLatin1StringView be the same as QString

Everywhere, except for ARM.

Change-Id: I50e2158aeade4256ad1dfffd17b11ca2d57ad1fb
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Thiago Macieira 2024-02-05 15:53:39 -08:00
parent fff9f5047a
commit 45fd36f148
3 changed files with 8 additions and 11 deletions

View File

@ -1140,21 +1140,12 @@ size_t qHash(QLatin1StringView key, size_t seed) noexcept
// the seed is always 0 in bootstrapped mode (no seed generation code),
// so help the compiler do dead code elimination
seed = 0;
constexpr bool Qt6DeterministicHash = true;
#else
constexpr bool Qt6DeterministicHash = QT_VERSION_MAJOR == 6;
#endif
auto data = reinterpret_cast<const uchar *>(key.data());
size_t size = key.size();
if (seed == 0 && Qt6DeterministicHash) {
// fall back to what we used to use prior to Qt 6.8
return qHashBits(data, size, seed);
}
// mix in the length as a secondary seed. For seed == 0, seed2 must be
// size, to match what we used to do prior to Qt 6.2.
// Mix in the length as a secondary seed.
// Multiplied by 2 to match the byte size of the equiavlent UTF-16 string.
size_t seed2 = size * 2;
if (seed)

View File

@ -58,6 +58,12 @@ template <> struct QHashHeterogeneousSearch<QString, QStringView> : std::true_ty
template <> struct QHashHeterogeneousSearch<QStringView, QString> : std::true_type {};
template <> struct QHashHeterogeneousSearch<QByteArray, QByteArrayView> : std::true_type {};
template <> struct QHashHeterogeneousSearch<QByteArrayView, QByteArray> : std::true_type {};
#ifndef Q_PROCESSOR_ARM
template <> struct QHashHeterogeneousSearch<QString, QLatin1StringView> : std::true_type {};
template <> struct QHashHeterogeneousSearch<QStringView, QLatin1StringView> : std::true_type {};
template <> struct QHashHeterogeneousSearch<QLatin1StringView, QString> : std::true_type {};
template <> struct QHashHeterogeneousSearch<QLatin1StringView, QStringView> : std::true_type {};
#endif
namespace QHashPrivate {

View File

@ -324,7 +324,7 @@ void tst_QHashFunctions::stringConsistency()
QCOMPARE(qHash(sv, seed), qHash(value, seed));
QCOMPARE(qHash(u8bav, seed), qHash(u8ba, seed));
if (seed || QT_VERSION_MAJOR > 6) {
if (seed == 0 || QHashHeterogeneousSearch<QString, QLatin1StringView>::value) {
QByteArray l1ba = value.toLatin1();
QLatin1StringView l1sv(l1ba.data(), l1ba.size());
#ifdef Q_PROCESSOR_ARM