QTextFormat: use qHash(double/float) instead of own implementation

The implementation of hash(float) does not treat +0 and -0 the same,
thus violating the property required of hash functions that they
produce equal hash values for equal input.

Simply use the qHash() FP functions which recently became available
and which do not have that problem.

This was found by unrelated code review. I'm not aware of any user-
visible issue this fixes.

Change-Id: I458c384aaf112e29cea677022969b62a34263cfb
Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
bb10
Marc Mutz 2014-09-02 11:02:20 +02:00
parent 23c1b132b1
commit 1629deec83
1 changed files with 3 additions and 17 deletions

View File

@ -266,20 +266,6 @@ private:
friend QDataStream &operator>>(QDataStream &, QTextFormat &);
};
// this is only safe because sizeof(int) == sizeof(float)
static inline uint hash(float d)
{
#ifdef Q_CC_GNU
// this is a GCC extension and isn't guaranteed to work in other compilers
// the reinterpret_cast below generates a strict-aliasing warning with GCC
union { float f; uint u; } cvt;
cvt.f = d;
return cvt.u;
#else
return reinterpret_cast<uint&>(d);
#endif
}
static inline uint hash(const QColor &color)
{
return (color.isValid()) ? color.rgba() : 0x234109;
@ -287,7 +273,7 @@ static inline uint hash(const QColor &color)
static inline uint hash(const QPen &pen)
{
return hash(pen.color()) + hash(pen.widthF());
return hash(pen.color()) + qHash(pen.widthF());
}
static inline uint hash(const QBrush &brush)
@ -300,7 +286,7 @@ static inline uint variantHash(const QVariant &variant)
// simple and fast hash functions to differentiate between type and value
switch (variant.userType()) { // sorted by occurrence frequency
case QVariant::String: return qHash(variant.toString());
case QVariant::Double: return hash(variant.toDouble());
case QVariant::Double: return qHash(variant.toDouble());
case QVariant::Int: return 0x811890 + variant.toInt();
case QVariant::Brush:
return 0x01010101 + hash(qvariant_cast<QBrush>(variant));
@ -311,7 +297,7 @@ static inline uint variantHash(const QVariant &variant)
case QVariant::Color: return hash(qvariant_cast<QColor>(variant));
case QVariant::TextLength:
return 0x377 + hash(qvariant_cast<QTextLength>(variant).rawValue());
case QMetaType::Float: return hash(variant.toFloat());
case QMetaType::Float: return qHash(variant.toFloat());
case QVariant::Invalid: return 0;
default: break;
}