From ea17c21fd8b93a94027fad7d3827904ae96e2a3b Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Wed, 4 Apr 2012 20:37:04 +0100 Subject: [PATCH] QHostAddress: improve qHash implementation Avoid the conversion to a temporary QString -- just hash the address as a byte array. Change-Id: Ic35cdbbc3ee66c32a28d911bd27de0092395979f Done-with: Shane Kearns Reviewed-by: Thiago Macieira Reviewed-by: Shane Kearns --- src/network/kernel/qhostaddress.cpp | 3 ++- src/network/kernel/qhostaddress.h | 4 +--- tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp | 2 ++ 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/network/kernel/qhostaddress.cpp b/src/network/kernel/qhostaddress.cpp index 2adf19e..3c08717 100644 --- a/src/network/kernel/qhostaddress.cpp +++ b/src/network/kernel/qhostaddress.cpp @@ -1029,7 +1029,8 @@ QDebug operator<<(QDebug d, const QHostAddress &address) uint qHash(const QHostAddress &key, uint seed) { - return qHash(key.toString(), seed); + QT_ENSURE_PARSED(&key); + return qHash(QByteArray::fromRawData(reinterpret_cast(key.d->a6.c), 16), seed); } #ifndef QT_NO_DATASTREAM diff --git a/src/network/kernel/qhostaddress.h b/src/network/kernel/qhostaddress.h index b298e3f..ce4470d 100644 --- a/src/network/kernel/qhostaddress.h +++ b/src/network/kernel/qhostaddress.h @@ -123,6 +123,7 @@ public: static QPair parseSubnet(const QString &subnet); + friend Q_NETWORK_EXPORT uint qHash(const QHostAddress &key, uint seed = 0); protected: QScopedPointer d; }; @@ -134,9 +135,6 @@ inline bool operator ==(QHostAddress::SpecialAddress address1, const QHostAddres Q_NETWORK_EXPORT QDebug operator<<(QDebug, const QHostAddress &); #endif - -Q_NETWORK_EXPORT uint qHash(const QHostAddress &key, uint seed = 0); - #ifndef QT_NO_DATASTREAM Q_NETWORK_EXPORT QDataStream &operator<<(QDataStream &, const QHostAddress &); Q_NETWORK_EXPORT QDataStream &operator>>(QDataStream &, QHostAddress &); diff --git a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp index a0403e5..d1027c8 100644 --- a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp +++ b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp @@ -323,6 +323,8 @@ void tst_QHostAddress::compare() QFETCH(bool, result); QCOMPARE(first == second, result); + if (result == true) + QVERIFY(qHash(first) == qHash(second)); } void tst_QHostAddress::assignment() -- 2.7.4