From 1e43b64a7a5c3823a6bdcb8d0cd28a17955939a2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 7 Feb 2014 14:52:41 -0800 Subject: Centralize the merging toFloat conversions The QByteArray version was missing the overflow check that the other versions had. Change-Id: I03cd92e5e5a84c038bee1f1ee217e93e9d9a675a Reviewed-by: Lars Knoll --- src/corelib/tools/qbytearray.cpp | 2 +- src/corelib/tools/qlocale.cpp | 24 ++---------------------- src/corelib/tools/qlocale_p.h | 17 +++++++++++++++++ src/corelib/tools/qstring.cpp | 38 ++------------------------------------ 4 files changed, 22 insertions(+), 59 deletions(-) diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 5755bc6ffe..304ce69449 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -3545,7 +3545,7 @@ double QByteArray::toDouble(bool *ok) const float QByteArray::toFloat(bool *ok) const { - return float(toDouble(ok)); + return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); } /*! diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index c0e5706af2..98e38ca587 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -1277,20 +1277,9 @@ qulonglong QLocale::toULongLong(const QString &s, bool *ok) const \sa toDouble(), toInt(), toString() */ -#define QT_MAX_FLOAT 3.4028234663852886e+38 - float QLocale::toFloat(const QString &s, bool *ok) const { - bool myOk; - double d = toDouble(s, &myOk); - if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { - if (ok != 0) - *ok = false; - return 0.0; - } - if (ok != 0) - *ok = true; - return float(d); + return QLocaleData::convertDoubleToFloat(toDouble(s, ok), ok); } /*! @@ -1462,16 +1451,7 @@ qulonglong QLocale::toULongLong(const QStringRef &s, bool *ok) const float QLocale::toFloat(const QStringRef &s, bool *ok) const { - bool myOk; - double d = toDouble(s, &myOk); - if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { - if (ok) - *ok = false; - return 0.0; - } - if (ok) - *ok = true; - return float(d); + return QLocaleData::convertDoubleToFloat(toDouble(s, ok), ok); } /*! diff --git a/src/corelib/tools/qlocale_p.h b/src/corelib/tools/qlocale_p.h index ac12f03356..917e83834a 100644 --- a/src/corelib/tools/qlocale_p.h +++ b/src/corelib/tools/qlocale_p.h @@ -56,9 +56,13 @@ #include "QtCore/qstring.h" #include "QtCore/qvarlengtharray.h" #include "QtCore/qvariant.h" +#include "QtCore/qnumeric.h" #include "qlocale.h" +#include +#include + QT_BEGIN_NAMESPACE #ifndef QT_NO_SYSTEMLOCALE @@ -227,6 +231,19 @@ public: int width = -1, unsigned flags = NoFlags) const; + // this function is meant to be called with the result of stringToDouble or bytearrayToDouble + static float convertDoubleToFloat(double d, bool *ok) + { + if (qIsInf(d)) + return float(d); + if (std::fabs(d) > std::numeric_limits::max()) { + if (ok != 0) + *ok = false; + return 0.0f; + } + return float(d); + } + double stringToDouble(const QChar *begin, int len, bool *ok, GroupSeparatorMode group_sep_mode) const; qint64 stringToLongLong(const QChar *begin, int len, int base, bool *ok, GroupSeparatorMode group_sep_mode) const; quint64 stringToUnsLongLong(const QChar *begin, int len, int base, bool *ok, GroupSeparatorMode group_sep_mode) const; diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 5bc485be7a..673363c66f 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -6341,27 +6341,9 @@ double QString::toDouble(bool *ok) const \sa number(), toDouble(), toInt(), QLocale::toFloat() */ -#define QT_MAX_FLOAT 3.4028234663852886e+38 - float QString::toFloat(bool *ok) const { - bool myOk; - double d = toDouble(&myOk); - if (!myOk) { - if (ok != 0) - *ok = false; - return 0.0; - } - if (qIsInf(d)) - return float(d); - if (d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { - if (ok != 0) - *ok = false; - return 0.0; - } - if (ok != 0) - *ok = true; - return float(d); + return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); } /*! \fn QString &QString::setNum(int n, int base) @@ -9806,23 +9788,7 @@ double QStringRef::toDouble(bool *ok) const float QStringRef::toFloat(bool *ok) const { - bool myOk; - double d = toDouble(&myOk); - if (!myOk) { - if (ok != 0) - *ok = false; - return 0.0; - } - if (qIsInf(d)) - return float(d); - if (d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { - if (ok != 0) - *ok = false; - return 0.0; - } - if (ok) - *ok = true; - return float(d); + return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); } /*! -- cgit v1.2.3