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 <lars.knoll@digia.com>
bb10
Thiago Macieira 2014-02-07 14:52:41 -08:00 committed by The Qt Project
parent c0791ac76e
commit 1e43b64a7a
4 changed files with 22 additions and 59 deletions

View File

@ -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);
}
/*!

View File

@ -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);
}
/*!

View File

@ -56,9 +56,13 @@
#include "QtCore/qstring.h"
#include "QtCore/qvarlengtharray.h"
#include "QtCore/qvariant.h"
#include "QtCore/qnumeric.h"
#include "qlocale.h"
#include <limits>
#include <cmath>
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<float>::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;

View File

@ -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);
}
/*!