From f39f539858417048f11a39c7b6198688e9657bea Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 22 Feb 2022 09:03:19 +0100 Subject: [PATCH] Add numeric conversion methods to QLatin1String MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [ChangeLog][QtCore][QLatin1String] Added numeric conversion methods. Task-number: QTBUG-98433 Change-Id: I414577ae715debe3d5ba9c6a160859aca790e017 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira Reviewed-by: Marc Mutz --- src/corelib/text/qstring.cpp | 56 +++++++++++++++++++ src/corelib/text/qstring.h | 31 ++++++++++ .../tst_qstringapisymmetry.cpp | 2 + 3 files changed, 89 insertions(+) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 9b6fe88cf1b..0375d682031 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -10130,6 +10130,62 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) equal to string \a s2; otherwise returns \c false. */ +/*! + \fn qlonglong QLatin1String::toLongLong(bool *ok, int base) const + \fn qulonglong QLatin1String::toULongLong(bool *ok, int base) const + \fn int QLatin1String::toInt(bool *ok, int base) const + \fn uint QLatin1String::toUInt(bool *ok, int base) const + \fn long QLatin1String::toLong(bool *ok, int base) const + \fn ulong QLatin1String::toULong(bool *ok, int base) const + \fn short QLatin1String::toShort(bool *ok, int base) const + \fn ushort QLatin1String::toUShort(bool *ok, int base) const + + \since 6.4 + + Returns this QLatin1String converted to a corresponding numeric value 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 Latin-1 string 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. + +//! [latin1-numeric-conversion-note] + \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. +//! [latin1-numeric-conversion-note] +*/ + +/*! + \fn double QLatin1String::toDouble(bool *ok) const + \fn float QLatin1String::toFloat(bool *ok) const + \since 6.4 + + Returns this QLatin1String converted to a corresponding floating-point 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 QLatin1String 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. + + \include qstring.cpp latin1-numeric-conversion-note +*/ + #if !defined(QT_NO_DATASTREAM) || defined(QT_BOOTSTRAPPED) /*! \fn QDataStream &operator<<(QDataStream &stream, const QString &string) diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index c8c25da3cc8..55cf471d10c 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -177,6 +177,37 @@ public: [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } + [[nodiscard]] short toShort(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] ushort toUShort(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] int toInt(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] uint toUInt(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] long toLong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] ulong toULong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] qlonglong toLongLong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] qulonglong toULongLong(bool *ok = nullptr, int base = 10) const + { return QtPrivate::toIntegral(QByteArrayView(*this), ok, base); } + [[nodiscard]] float toFloat(bool *ok = nullptr) const + { + const auto r = QtPrivate::toFloat(*this); + if (ok) + *ok = bool(r); + return r.value_or(0.0f); + } + [[nodiscard]] double toDouble(bool *ok = nullptr) const + { + const auto r = QtPrivate::toDouble(*this); + if (ok) + *ok = bool(r); + return r.value_or(0.0); + } + using value_type = const char; using reference = value_type&; using const_reference = reference; diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 3eeaf9b6af3..ad154f81030 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -714,6 +714,8 @@ private Q_SLOTS: void toNumber_QStringView() { toNumber_impl(); } void toNumber_QByteArray_data() { toNumber_data(); } void toNumber_QByteArray() { toNumber_impl(); } + void toNumber_QLatin1String_data() { toNumber_data(); } + void toNumber_QLatin1String() { toNumber_impl(); } private: void count_data();