diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-09-23 12:30:44 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-10-12 11:25:15 +0200 |
commit | e0785253dfb3ace32e3626bcb03211bfaa4cd4e0 (patch) | |
tree | 22cd567a935f402eac261d439adc5e89500b9bb6 /src | |
parent | 84b53a4514c62ab06034de555ed03e41ab718dfc (diff) |
Add toInt() and friends to QStringView
The implementations are not trimmed for efficiency, but as a porting
help for Qt 6. Because of that, they simply use the methods available
in QString.
Task-number: QTBUG-86516
Change-Id: I39ea04a6c96ca43f3a88a9be4e63a5dea4a4e479
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qstring.h | 21 | ||||
-rw-r--r-- | src/corelib/text/qstringview.cpp | 248 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 11 |
3 files changed, 280 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 8c09c80f5e..f48777d7f1 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -2121,6 +2121,27 @@ QString QLatin1String::arg(Args &&...args) const return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...); } +inline short QStringView::toShort(bool *ok, int base) const +{ return toString().toShort(ok, base); } +inline ushort QStringView::toUShort(bool *ok, int base) const +{ return toString().toUShort(ok, base); } +inline int QStringView::toInt(bool *ok, int base) const +{ return toString().toInt(ok, base); } +inline uint QStringView::toUInt(bool *ok, int base) const +{ return toString().toUInt(ok, base); } +inline long QStringView::toLong(bool *ok, int base) const +{ return toString().toLong(ok, base); } +inline ulong QStringView::toULong(bool *ok, int base) const +{ return toString().toULong(ok, base); } +inline qlonglong QStringView::toLongLong(bool *ok, int base) const +{ return toString().toLongLong(ok, base); } +inline qulonglong QStringView::toULongLong(bool *ok, int base) const +{ return toString().toULongLong(ok, base); } +inline float QStringView::toFloat(bool *ok) const +{ return toString().toFloat(ok); } +inline double QStringView::toDouble(bool *ok) const +{ return toString().toDouble(ok); } + QT_END_NAMESPACE #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 93febceb19..1451dfed44 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -918,4 +918,252 @@ QT_BEGIN_NAMESPACE \sa QString::toWCharArray() */ +/*! + \fn qint64 QStringView::toLongLong(bool *ok, int base) const + + Returns the string converted to a \c{long long} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toLongLong() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toLongLong() + + \since 5.15.2 +*/ + +/*! + \fn quint64 QStringView::toULongLong(bool *ok, int base) const + + Returns the string converted to an \c{unsigned long long} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toULongLong() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toULongLong() + + \since 5.15.2 +*/ + +/*! + \fn long QStringView::toLong(bool *ok, int base) const + + Returns the string converted to a \c long using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toLong() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toLong() + + \since 5.15.2 +*/ + +/*! + \fn ulong QStringView::toULong(bool *ok, int base) const + + Returns the string converted to an \c{unsigned long} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toULongLong() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toULong() + + \since 5.15.2 +*/ + +/*! + \fn int QStringView::toInt(bool *ok, int base) const + + Returns the string converted to an \c int using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toInt() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toInt() + + \since 5.15.2 +*/ + +/*! + \fn uint QStringView::toUInt(bool *ok, int base) const + + Returns the string converted to an \c{unsigned int} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toUInt() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toUInt() + + \since 5.15.2 +*/ + +/*! + \fn short QStringView::toShort(bool *ok, int base) const + + Returns the string converted to a \c short using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toShort() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toShort() + + \since 5.15.2 +*/ + +/*! + \fn ushort QStringView::toUShort(bool *ok, int base) const + + Returns the string converted to an \c{unsigned short} using base \a + base, which is 10 by default and must be between 2 and 36, or 0. + 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. + + If \a base is 0, the C language convention is used: If the string + begins with "0x", base 16 is used; if the string begins with "0", + base 8 is used; otherwise, base 10 is used. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toUShort() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toUShort() + + \since 5.15.2 +*/ + +/*! + \fn double QStringView::toDouble(bool *ok) const + + Returns the string 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. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toDouble() + + For historic reasons, this function does not handle + thousands group separators. If you need to convert such numbers, + use QLocale::toDouble(). + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toDouble() + + \since 5.15.2 +*/ + +/*! + \fn float QStringView::toFloat(bool *ok) const + + Returns the string 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. + + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toFloat() + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::toFloat() + + \since 5.15.2 +*/ + QT_END_NAMESPACE diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 72ddd91968..e599e5a8c2 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -310,6 +310,17 @@ public: Q_REQUIRED_RESULT bool isValidUtf16() const noexcept { return QtPrivate::isValidUtf16(*this); } + Q_REQUIRED_RESULT inline short toShort(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline ushort toUShort(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline int toInt(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline uint toUInt(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline long toLong(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline ulong toULong(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline qlonglong toLongLong(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline qulonglong toULongLong(bool *ok = nullptr, int base = 10) const; + Q_REQUIRED_RESULT inline float toFloat(bool *ok = nullptr) const; + Q_REQUIRED_RESULT inline double toDouble(bool *ok = nullptr) const; + Q_REQUIRED_RESULT inline int toWCharArray(wchar_t *array) const; // defined in qstring.h // |