QByteArray: make toDouble() reject space-only strings
This does not affect leading and trailing spaces, which remain allowed. This is only about a string containing only spaces, which used to be rejected prior to Qt 5.9 and are rejected with QString (unit tests added to confirm). Drive-by indent one QString test row, which I've also reordered so null comes before empty. [ChangeLog][QtCore][QByteArray & QByteArrayView] Fixed an old regression that caused toDouble() and toFloat() to return ok = true for a string containing only whitespaces. Pick-to: 6.8 Fixes: QTBUG-137038 Change-Id: Ia6f7714c5e289317de60fffd0f8aa6d2198a91ef Reviewed-by: Ahmad Samir <a.samirh78@gmail.com> Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> (cherry picked from commit 61b17127ae50516323d64523e6cfdf524ae8e974) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
1efb21ba00
commit
4e785f1da5
@ -4044,7 +4044,8 @@ double QByteArray::toDouble(bool *ok) const
|
||||
|
||||
auto QtPrivate::toDouble(QByteArrayView a) noexcept -> ParsedNumber<double>
|
||||
{
|
||||
auto r = qt_asciiToDouble(a.data(), a.size(), WhitespacesAllowed);
|
||||
a = a.trimmed();
|
||||
auto r = qt_asciiToDouble(a.data(), a.size());
|
||||
if (r.ok())
|
||||
return ParsedNumber{r.result};
|
||||
else
|
||||
|
@ -1497,9 +1497,18 @@ template <typename ByteArray> void tst_QByteArrayApiSymmetry::toFloat() const
|
||||
QCOMPARE(ByteArray().toFloat(&ok), 0.0f);
|
||||
QVERIFY(!ok);
|
||||
|
||||
ok = true;
|
||||
QCOMPARE(ByteArray("").toFloat(&ok), 0.0f);
|
||||
QVERIFY(!ok);
|
||||
|
||||
ok = true;
|
||||
QCOMPARE(ByteArray(" ").toFloat(&ok), 0.0f);
|
||||
QVERIFY(!ok);
|
||||
|
||||
ok = true;
|
||||
QCOMPARE(ByteArray(" ").toFloat(&ok), 0.0f);
|
||||
QVERIFY(!ok);
|
||||
|
||||
// NB: floats < 1e-6 are zero as far as QCOMPARE() is concerned !
|
||||
const char data[] = "0.0000931322574615478515625";
|
||||
const float expectedValue = 9.31322574615478515625e-5f;
|
||||
@ -1523,6 +1532,8 @@ void tst_QByteArrayApiSymmetry::toDouble_data() const
|
||||
|
||||
QTest::newRow("null") << QByteArray() << 0.0 << false;
|
||||
QTest::newRow("empty") << QByteArray("") << 0.0 << false;
|
||||
QTest::newRow("space-only") << QByteArray(" ") << 0.0 << false;
|
||||
QTest::newRow("spaces-only") << QByteArray(" ") << 0.0 << false;
|
||||
|
||||
QTest::newRow("decimal") << QByteArray("1.2345") << 1.2345 << true;
|
||||
QTest::newRow("exponent lowercase") << QByteArray("1.2345e+01") << 12.345 << true;
|
||||
|
@ -7573,8 +7573,10 @@ void tst_QString::double_conversion_data()
|
||||
|
||||
// The bad...
|
||||
|
||||
QTest::newRow("C null") << QString() << false << 0.0;
|
||||
QTest::newRow("C empty") << u""_s << false << 0.0;
|
||||
QTest::newRow("C null") << QString() << false << 0.0;
|
||||
QTest::newRow("C space") << u" "_s << false << 0.0;
|
||||
QTest::newRow("C spaces") << u" "_s << false << 0.0;
|
||||
QTest::newRow("C .") << u"."_s << false << 0.0;
|
||||
QTest::newRow("C 1e") << u"1e"_s << false << 0.0;
|
||||
QTest::newRow("C 1,") << u"1,"_s << false << 0.0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user