diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2021-08-09 18:18:19 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2021-08-30 17:46:00 +0200 |
commit | 4e9efb0b6096c35edc0b98650cf64acb367d5ba8 (patch) | |
tree | 05d874684b243439a7906548108ec18f7ba195b2 /src/corelib | |
parent | 6db5fd5918e9c2fb73d61de13356307248c4f2e9 (diff) |
Teach QByteArrayView how to parse numbers
Now that we don't need '\0'-termination on the data, this is possible.
Moved QByteArray's tests to tst_QByteArrayApiSymmetry and added some
more test-cases.
[ChangeLog][QtCore][QByteArrayView] Added numeric parsing methods.
Change-Id: Ic0df91ecfe5dbf6f008d344dd0464d7927f32273
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp | 23 | ||||
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 52 | ||||
-rw-r--r-- | src/corelib/text/qbytearrayview.h | 11 | ||||
-rw-r--r-- | src/corelib/text/qbytearrayview.qdoc | 240 |
4 files changed, 323 insertions, 3 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp index d70caa2650..519f609790 100644 --- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. @@ -58,3 +58,24 @@ void fun(QByteArrayView bv); void fun(char ch) { fun(QByteArrayView(&ch, 1)); } //! [1] + +//! [2] +QByteArrayView str("FF"); +bool ok; +int hex = str.toInt(&ok, 16); // hex == 255, ok == true +int dec = str.toInt(&ok, 10); // dec == 0, ok == false +//! [2] + +//! [3] +QByteArrayView str("FF"); +bool ok; +long hex = str.toLong(&ok, 16); // hex == 255, ok == true +long dec = str.toLong(&ok, 10); // dec == 0, ok == false +//! [3] + +//! [4] +QByteArrayView string("1234.56 Volt"); +bool ok; +float a = str.toFloat(&ok); // a == 0, ok == false +a = string.first(7).toFloat(&ok); // a == 1234.56, ok == true +//! [4] diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 320f744c4f..a1b0f30d01 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -3571,6 +3571,11 @@ qlonglong QByteArray::toLongLong(bool *ok, int base) const return toIntegral_helper<qlonglong>(*this, ok, base); } +qlonglong QByteArrayView::toLongLong(bool *ok, int base) const +{ + return toIntegral_helper<qlonglong>(*this, ok, base); +} + /*! Returns the byte array converted to an \c {unsigned long long} using base \a base, which is ten by default. Bases 0 and 2 through 36 are supported, using @@ -3598,6 +3603,11 @@ qulonglong QByteArray::toULongLong(bool *ok, int base) const return toIntegral_helper<qulonglong>(*this, ok, base); } +qulonglong QByteArrayView::toULongLong(bool *ok, int base) const +{ + return toIntegral_helper<qulonglong>(*this, ok, base); +} + /*! Returns the byte array converted to an \c int using base \a base, which is ten by default. Bases 0 and 2 through 36 are supported, using letters for @@ -3627,6 +3637,11 @@ int QByteArray::toInt(bool *ok, int base) const return toIntegral_helper<int>(*this, ok, base); } +int QByteArrayView::toInt(bool *ok, int base) const +{ + return toIntegral_helper<int>(*this, ok, base); +} + /*! Returns the byte array converted to an \c {unsigned int} using base \a base, which is ten by default. Bases 0 and 2 through 36 are supported, using @@ -3654,6 +3669,11 @@ uint QByteArray::toUInt(bool *ok, int base) const return toIntegral_helper<uint>(*this, ok, base); } +uint QByteArrayView::toUInt(bool *ok, int base) const +{ + return toIntegral_helper<uint>(*this, ok, base); +} + /*! \since 4.1 @@ -3684,6 +3704,11 @@ long QByteArray::toLong(bool *ok, int base) const return toIntegral_helper<long>(*this, ok, base); } +long QByteArrayView::toLong(bool *ok, int base) const +{ + return toIntegral_helper<long>(*this, ok, base); +} + /*! \since 4.1 @@ -3712,6 +3737,11 @@ ulong QByteArray::toULong(bool *ok, int base) const return toIntegral_helper<ulong>(*this, ok, base); } +ulong QByteArrayView::toULong(bool *ok, int base) const +{ + return toIntegral_helper<ulong>(*this, ok, base); +} + /*! Returns the byte array converted to a \c short using base \a base, which is ten by default. Bases 0 and 2 through 36 are supported, using letters for @@ -3739,6 +3769,11 @@ short QByteArray::toShort(bool *ok, int base) const return toIntegral_helper<short>(*this, ok, base); } +short QByteArrayView::toShort(bool *ok, int base) const +{ + return toIntegral_helper<short>(*this, ok, base); +} + /*! Returns the byte array converted to an \c {unsigned short} using base \a base, which is ten by default. Bases 0 and 2 through 36 are supported, using @@ -3766,6 +3801,10 @@ ushort QByteArray::toUShort(bool *ok, int base) const return toIntegral_helper<ushort>(*this, ok, base); } +ushort QByteArrayView::toUShort(bool *ok, int base) const +{ + return toIntegral_helper<ushort>(*this, ok, base); +} /*! Returns the byte array converted to a \c double value. @@ -3794,10 +3833,14 @@ ushort QByteArray::toUShort(bool *ok, int base) const double QByteArray::toDouble(bool *ok) const { + return QByteArrayView(*this).toDouble(ok); +} + +double QByteArrayView::toDouble(bool *ok) const +{ bool nonNullOk = false; int processed = 0; - double d = qt_asciiToDouble(constData(), size(), - nonNullOk, processed, WhitespacesAllowed); + double d = qt_asciiToDouble(data(), size(), nonNullOk, processed, WhitespacesAllowed); if (ok) *ok = nonNullOk; return d; @@ -3833,6 +3876,11 @@ float QByteArray::toFloat(bool *ok) const return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); } +float QByteArrayView::toFloat(bool *ok) const +{ + return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); +} + /*! \since 5.2 diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 1824826437..1cd5b0333e 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -240,8 +240,19 @@ public: constexpr void chop(qsizetype n) { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; } + // Defined in qbytearray.cpp: [[nodiscard]] QByteArrayView trimmed() const noexcept { return QtPrivate::trimmed(*this); } + [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const; + [[nodiscard]] float toFloat(bool *ok = nullptr) const; + [[nodiscard]] double toDouble(bool *ok = nullptr) const; [[nodiscard]] bool startsWith(QByteArrayView other) const noexcept { return QtPrivate::startsWith(*this, other); } diff --git a/src/corelib/text/qbytearrayview.qdoc b/src/corelib/text/qbytearrayview.qdoc index 5bfd8bdef2..624b056744 100644 --- a/src/corelib/text/qbytearrayview.qdoc +++ b/src/corelib/text/qbytearrayview.qdoc @@ -646,6 +646,246 @@ */ /*! + \fn qlonglong QByteArrayView::toLongLong(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to a \c {long long} using base \a + base, which is ten by default. Bases 0 and 2 through 36 are supported, using + letters for digits beyond 9; A is ten, B is eleven and so on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn qulonglong QByteArrayView::toULongLong(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to an \c {unsigned long long} using + base \a base, which is ten by default. Bases 0 and 2 through 36 are + supported, using letters for digits beyond 9; A is ten, B is eleven and so + on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn int QByteArrayView::toInt(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to an \c int using base \a base, + which is ten by default. Bases 0 and 2 through 36 are supported, using + letters for digits beyond 9; A is ten, B is eleven and so on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \snippet code/src_corelib_text_qbytearrayview.cpp 2 + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn uint QByteArrayView::toUInt(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to an \c {unsigned int} using base \a + base, which is ten by default. Bases 0 and 2 through 36 are supported, using + letters for digits beyond 9; A is ten, B is eleven and so on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn long QByteArrayView::toLong(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to a \c long int using base \a base, + which is ten by default. Bases 0 and 2 through 36 are supported, using + letters for digits beyond 9; A is ten, B is eleven and so on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \snippet code/src_corelib_text_qbytearrayview.cpp 3 + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn ulong QByteArrayView::toULong(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to an \c {unsigned long int} using + base \a base, which is ten by default. Bases 0 and 2 through 36 are + supported, using letters for digits beyond 9; A is ten, B is eleven and so + on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn short QByteArrayView::toShort(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to a \c short using base \a base, + which is ten by default. Bases 0 and 2 through 36 are supported, using + letters for digits beyond 9; A is ten, B is eleven and so on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn ushort QByteArrayView::toUShort(bool *ok, int base) const + \since 6.3 + + Returns this byte array view converted to an \c {unsigned short} using base + \a base, which is ten by default. Bases 0 and 2 through 36 are supported, + using letters for digits beyond 9; A is ten, B is eleven and so on. + + If \a base is 0, the base is determined automatically using the following + rules: if the byte array view begins with "0x", the rest of it is read as + hexadecimal (base 16); otherwise, if it begins with "0", the rest of it is + read as octal (base 8); otherwise it is read as decimal. + + Returns 0 if the conversion fails. + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. +*/ + +/*! + \fn double QByteArrayView::toDouble(bool *ok) const + \since 6.3 + + Returns this byte array view converted to a \c double value. + + Returns an infinity if the conversion overflows or 0.0 if the + conversion fails for other reasons (e.g. underflow). + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \warning The QByteArrayView content may only contain valid numerical + characters which includes the plus/minus sign, the character e used in + scientific notation, and the decimal point. Including the unit or additional + characters leads to a conversion error. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. + + This function ignores leading and trailing spacing characters. +*/ + +/*! + \fn float QByteArrayView::toFloat(bool *ok) const + \since 6.3 + + Returns this byte array view converted to a \c float value. + + Returns an infinity if the conversion overflows or 0.0 if the + conversion fails for other reasons (e.g. underflow). + + If \a ok is not \nullptr, failure is reported by setting *\a{ok} + to \c false, and success by setting *\a{ok} to \c true. + + \snippet code/src_corelib_text_qbytearrayview.cpp 4 + + \warning The QByteArrayView content may only contain valid numerical + characters which includes the plus/minus sign, the character e used in + scientific notation, and the decimal point. Including the unit or additional + characters leads to a conversion error. + + \note The conversion of the number is performed in the default C locale, + regardless of the user's locale. Use QLocale to perform locale-aware + conversions between numbers and strings. + + This function ignores leading and trailing whitespace. +*/ + +/*! \fn bool QByteArrayView::startsWith(QByteArrayView bv) const \fn bool QByteArrayView::startsWith(char ch) const |