From 6a9cd5604e9bfd3debc4cece4d7c260e2333cb55 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 30 Jan 2017 13:59:19 +0100 Subject: Don't narrow lengths in qHash() implementations The crc32() functions take a size_t length, but the hash() functions wrapping them took int lengths. That makes no sense and actively hurts adding hash functions for STL types or QStringView, so port the hash() interface to size_t. Change-Id: Id303d6df4b698560fce656cec8ed693b01daac1c Reviewed-by: Milian Wolff Reviewed-by: Thiago Macieira --- src/corelib/tools/qhash.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index abec9ebb79..9270539f4f 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -199,14 +199,14 @@ static uint crc32(...) } #endif -static inline uint hash(const uchar *p, int len, uint seed) Q_DECL_NOTHROW +static inline uint hash(const uchar *p, size_t len, uint seed) Q_DECL_NOTHROW { uint h = seed; if (hasFastCrc32()) - return crc32(p, size_t(len), h); + return crc32(p, len, h); - for (int i = 0; i < len; ++i) + for (size_t i = 0; i < len; ++i) h = 31 * h + p[i]; return h; @@ -217,14 +217,14 @@ uint qHashBits(const void *p, size_t len, uint seed) Q_DECL_NOTHROW return hash(static_cast(p), int(len), seed); } -static inline uint hash(const QChar *p, int len, uint seed) Q_DECL_NOTHROW +static inline uint hash(const QChar *p, size_t len, uint seed) Q_DECL_NOTHROW { uint h = seed; if (hasFastCrc32()) - return crc32(p, size_t(len), h); + return crc32(p, len, h); - for (int i = 0; i < len; ++i) + for (size_t i = 0; i < len; ++i) h = 31 * h + p[i].unicode(); return h; @@ -232,23 +232,24 @@ static inline uint hash(const QChar *p, int len, uint seed) Q_DECL_NOTHROW uint qHash(const QByteArray &key, uint seed) Q_DECL_NOTHROW { - return hash(reinterpret_cast(key.constData()), key.size(), seed); + return hash(reinterpret_cast(key.constData()), size_t(key.size()), seed); } uint qHash(const QString &key, uint seed) Q_DECL_NOTHROW { - return hash(key.unicode(), key.size(), seed); + return hash(key.unicode(), size_t(key.size()), seed); } uint qHash(const QStringRef &key, uint seed) Q_DECL_NOTHROW { - return hash(key.unicode(), key.size(), seed); + return hash(key.unicode(), size_t(key.size()), seed); } uint qHash(const QBitArray &bitArray, uint seed) Q_DECL_NOTHROW { int m = bitArray.d.size() - 1; - uint result = hash(reinterpret_cast(bitArray.d.constData()), qMax(0, m), seed); + uint result = hash(reinterpret_cast(bitArray.d.constData()), + size_t(qMax(0, m)), seed); // deal with the last 0 to 7 bits manually, because we can't trust that // the padding is initialized to 0 in bitArray.d @@ -260,7 +261,7 @@ uint qHash(const QBitArray &bitArray, uint seed) Q_DECL_NOTHROW uint qHash(QLatin1String key, uint seed) Q_DECL_NOTHROW { - return hash(reinterpret_cast(key.data()), key.size(), seed); + return hash(reinterpret_cast(key.data()), size_t(key.size()), seed); } /*! -- cgit v1.2.3