diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-02-22 16:06:08 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2022-03-07 00:06:19 +0000 |
commit | 91e5b279e9b67eb3c70c2fdabf11e6ffa20bfbe8 (patch) | |
tree | b2603601250f460cfc99efeb01433355fd81941c /src/corelib/text/qbytearray.cpp | |
parent | a6bd11531309ad8c2773800fec5f5c67d8513ba8 (diff) |
Prepare for QByteArrayView number parsing modernization
Remove the unholy bool out parameter and make QtPrivate::to{Double,
Float,{Signed,Unsigned}Integer}() return a struct instead.
The struct contains what we'll most likely need for a full
QParsedNumber in the future: the value, an error code (always zero
atm), and a pointer to the first character that wasn't parsed (always
nullptr atm), so we don't need to change the ABI when QParsedNumber
eventually lands.
As an immediate positive contribution, even without the backend ported
away from bool out parameters, the functions can now be marked as PURE
and, in case of the FP versions, also noexcept (the int versions have
a narrow contract d/t the base argument, which, unlike the return
value, can be fixed later, by overloading).
Change-Id: I67945af80a9b53d6f170502a6df3384895e82d3e
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 1775029c9b34dde6c5d5ca2d15b25556ad7bc7c8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'src/corelib/text/qbytearray.cpp')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 4b2896433c..ce86f8df83 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -3528,7 +3528,7 @@ bool QByteArray::isNull() const noexcept return d->isNull(); } -qlonglong QtPrivate::toSignedInteger(QByteArrayView data, bool *ok, int base) +auto QtPrivate::toSignedInteger(QByteArrayView data, int base) -> ParsedNumber<qlonglong> { #if defined(QT_CHECK_RANGE) if (base != 0 && (base < 2 || base > 36)) { @@ -3536,16 +3536,17 @@ qlonglong QtPrivate::toSignedInteger(QByteArrayView data, bool *ok, int base) base = 10; } #endif - if (data.isEmpty()) { - if (ok) - *ok = false; - return 0; - } + if (data.isEmpty()) + return {}; - return QLocaleData::bytearrayToLongLong(data, base, ok); + bool ok = false; + const auto i = QLocaleData::bytearrayToLongLong(data, base, &ok); + if (ok) + return ParsedNumber(i); + return {}; } -qulonglong QtPrivate::toUnsignedInteger(QByteArrayView data, bool *ok, int base) +auto QtPrivate::toUnsignedInteger(QByteArrayView data, int base) -> ParsedNumber<qulonglong> { #if defined(QT_CHECK_RANGE) if (base != 0 && (base < 2 || base > 36)) { @@ -3553,13 +3554,14 @@ qulonglong QtPrivate::toUnsignedInteger(QByteArrayView data, bool *ok, int base) base = 10; } #endif - if (data.isEmpty()) { - if (ok) - *ok = false; - return 0; - } + if (data.isEmpty()) + return {}; - return QLocaleData::bytearrayToUnsLongLong(data, base, ok); + bool ok = false; + const auto u = QLocaleData::bytearrayToUnsLongLong(data, base, &ok); + if (ok) + return ParsedNumber(u); + return {}; } /*! @@ -3814,14 +3816,15 @@ double QByteArray::toDouble(bool *ok) const return QByteArrayView(*this).toDouble(ok); } -double QtPrivate::toDouble(QByteArrayView a, bool *ok) +auto QtPrivate::toDouble(QByteArrayView a) noexcept -> ParsedNumber<double> { bool nonNullOk = false; int processed = 0; double d = qt_asciiToDouble(a.data(), a.size(), nonNullOk, processed, WhitespacesAllowed); - if (ok) - *ok = nonNullOk; - return d; + if (nonNullOk) + return ParsedNumber{d}; + else + return {}; } /*! @@ -3854,9 +3857,15 @@ float QByteArray::toFloat(bool *ok) const return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); } -float QtPrivate::toFloat(QByteArrayView a, bool *ok) +auto QtPrivate::toFloat(QByteArrayView a) noexcept -> ParsedNumber<float> { - return QLocaleData::convertDoubleToFloat(a.toDouble(ok), ok); + if (const auto r = toDouble(a)) { + bool ok = true; + const auto f = QLocaleData::convertDoubleToFloat(*r, &ok); + if (ok) + return ParsedNumber(f); + } + return {}; } /*! |