diff options
-rw-r--r-- | src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp | 17 | ||||
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 170 | ||||
-rw-r--r-- | src/corelib/tools/qlocale.h | 11 |
3 files changed, 198 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp index 3794049f97..aed14c379f 100644 --- a/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_tools_qlocale.cpp @@ -103,3 +103,20 @@ d = german.toDouble( "1234.56", &ok ); // ok == false d = german.toDouble( "1.234", &ok ); // ok == true, d == 1234.0 //! [3] + +//! [3-qstringview] +bool ok; +double d; + +QLocale c(QLocale::C); +d = c.toDouble(u"1234.56", &ok); // ok == true, d == 1234.56 +d = c.toDouble(u"1,234.56", &ok); // ok == true, d == 1234.56 +d = c.toDouble(u"1234,56", &ok); // ok == false + +QLocale german(QLocale::German); +d = german.toDouble(u"1234,56", &ok); // ok == true, d == 1234.56 +d = german.toDouble(u"1.234,56", &ok); // ok == true, d == 1234.56 +d = german.toDouble(u"1234.56", &ok); // ok == false + +d = german.toDouble(u"1.234", &ok); // ok == true, d == 1234.0 +//! [3-qstringview] diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index 4a9c767724..0448a78160 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -1202,6 +1202,7 @@ QString QLocale::scriptToString(QLocale::Script script) return QLatin1String(script_name_list + script_name_index[script]); } +#if QT_STRINGVIEW_LEVEL < 2 /*! Returns the short int represented by the localized string \a s. @@ -1523,7 +1524,176 @@ double QLocale::toDouble(const QStringRef &s, bool *ok) const { return d->m_data->stringToDouble(s, ok, d->m_numberOptions); } +#endif // QT_STRINGVIEW_LEVEL < 2 +/*! + Returns the short int represented by the localized string \a s. + + If the conversion fails, the function returns 0. + + If \a ok is not null, failure is reported by setting *ok to false, and + success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toUShort(), toString() + + \since 5.10 +*/ + +short QLocale::toShort(QStringView s, bool *ok) const +{ + return toIntegral_helper<short>(d, s, ok); +} + +/*! + Returns the unsigned short int represented by the localized string \a s. + + If the conversion fails, the function returns 0. + + If \a ok is not null, failure is reported by setting *ok to false, and + success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toShort(), toString() + + \since 5.10 +*/ + +ushort QLocale::toUShort(QStringView s, bool *ok) const +{ + return toIntegral_helper<ushort>(d, s, ok); +} + +/*! + Returns the int represented by the localized string \a s. + + If the conversion fails, the function returns 0. + + If \a ok is not null, failure is reported by setting *ok to false, and + success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toUInt(), toString() + + \since 5.10 +*/ + +int QLocale::toInt(QStringView s, bool *ok) const +{ + return toIntegral_helper<int>(d, s, ok); +} + +/*! + Returns the unsigned int represented by the localized string \a s. + + If the conversion fails, the function returns 0. + + If \a ok is not null, failure is reported by setting *ok to false, and + success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toInt(), toString() + + \since 5.10 +*/ + +uint QLocale::toUInt(QStringView s, bool *ok) const +{ + return toIntegral_helper<uint>(d, s, ok); +} + +/*! + Returns the long long int represented by the localized string \a s. + + If the conversion fails, the function returns 0. + + If \a ok is not null, failure is reported by setting *ok to false, and + success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toInt(), toULongLong(), toDouble(), toString() + + \since 5.10 +*/ + + +qlonglong QLocale::toLongLong(QStringView s, bool *ok) const +{ + return toIntegral_helper<qlonglong>(d, s, ok); +} + +/*! + Returns the unsigned long long int represented by the localized + string \a s. + + If the conversion fails, the function returns 0. + + If \a ok is not null, failure is reported by setting *ok to false, and + success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toLongLong(), toInt(), toDouble(), toString() + + \since 5.10 +*/ + +qulonglong QLocale::toULongLong(QStringView s, bool *ok) const +{ + return toIntegral_helper<qulonglong>(d, s, ok); +} + +/*! + Returns the float represented by the localized string \a s, or 0.0 + if the conversion failed. + + If \a ok is not null, reports failure by setting + *ok to false and success by setting *ok to true. + + This function ignores leading and trailing whitespace. + + \sa toDouble(), toInt(), toString() + + \since 5.10 +*/ + +float QLocale::toFloat(QStringView s, bool *ok) const +{ + return QLocaleData::convertDoubleToFloat(toDouble(s, ok), ok); +} + +/*! + Returns the double represented by the localized string \a s, or + 0.0 if the conversion failed. + + If \a ok is not null, reports failure by setting + *ok to false and success by setting *ok to true. + + Unlike QString::toDouble(), this function does not fall back to + the "C" locale if the string cannot be interpreted in this + locale. + + \snippet code/src_corelib_tools_qlocale.cpp 3-qstringview + + Notice that the last conversion returns 1234.0, because '.' is the + thousands group separator in the German locale. + + This function ignores leading and trailing whitespace. + + \sa toFloat(), toInt(), toString() + + \since 5.10 +*/ + +double QLocale::toDouble(QStringView s, bool *ok) const +{ + return d->m_data->stringToDouble(s, ok, d->m_numberOptions); +} /*! Returns a localized string representation of \a i. diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h index bd89e48234..1f20bcd241 100644 --- a/src/corelib/tools/qlocale.h +++ b/src/corelib/tools/qlocale.h @@ -935,6 +935,7 @@ public: QString nativeLanguageName() const; QString nativeCountryName() const; +#if QT_STRINGVIEW_LEVEL < 2 short toShort(const QString &s, bool *ok = Q_NULLPTR) const; ushort toUShort(const QString &s, bool *ok = Q_NULLPTR) const; int toInt(const QString &s, bool *ok = Q_NULLPTR) const; @@ -952,6 +953,16 @@ public: qulonglong toULongLong(const QStringRef &s, bool *ok = Q_NULLPTR) const; float toFloat(const QStringRef &s, bool *ok = Q_NULLPTR) const; double toDouble(const QStringRef &s, bool *ok = Q_NULLPTR) const; +#endif + + short toShort(QStringView s, bool *ok = nullptr) const; + ushort toUShort(QStringView s, bool *ok = nullptr) const; + int toInt(QStringView s, bool *ok = nullptr) const; + uint toUInt(QStringView s, bool *ok = nullptr) const; + qlonglong toLongLong(QStringView s, bool *ok = nullptr) const; + qulonglong toULongLong(QStringView s, bool *ok = nullptr) const; + float toFloat(QStringView s, bool *ok = nullptr) const; + double toDouble(QStringView s, bool *ok = nullptr) const; QString toString(qlonglong i) const; QString toString(qulonglong i) const; |