summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qhash.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-01-30 13:59:19 +0100
committerMarc Mutz <marc.mutz@kdab.com>2017-01-31 00:38:37 +0000
commit6a9cd5604e9bfd3debc4cece4d7c260e2333cb55 (patch)
tree034e6f4636eed6f6fc2d0a61f38d43180c19b187 /src/corelib/tools/qhash.cpp
parent85468f7bccb276c2be5801481a6ce10f07581cdb (diff)
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 <milian.wolff@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qhash.cpp')
-rw-r--r--src/corelib/tools/qhash.cpp23
1 files changed, 12 insertions, 11 deletions
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<const uchar*>(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<const uchar *>(key.constData()), key.size(), seed);
+ return hash(reinterpret_cast<const uchar *>(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<const uchar *>(bitArray.d.constData()), qMax(0, m), seed);
+ uint result = hash(reinterpret_cast<const uchar *>(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<const uchar *>(key.data()), key.size(), seed);
+ return hash(reinterpret_cast<const uchar *>(key.data()), size_t(key.size()), seed);
}
/*!