diff options
Diffstat (limited to 'src/corelib/tools/qlocale.cpp')
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 275 |
1 files changed, 218 insertions, 57 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index bde4bf553e..9be20e6676 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -89,8 +89,9 @@ QT_BEGIN_INCLUDE_NAMESPACE #include "qlocale_data_p.h" QT_END_INCLUDE_NAMESPACE -QLocale::Language QLocalePrivate::codeToLanguage(const QChar *code, int len) Q_DECL_NOTHROW +QLocale::Language QLocalePrivate::codeToLanguage(QStringView code) Q_DECL_NOTHROW { + const auto len = code.size(); if (len != 2 && len != 3) return QLocale::C; ushort uc1 = code[0].toLower().unicode(); @@ -131,8 +132,9 @@ QLocale::Language QLocalePrivate::codeToLanguage(const QChar *code, int len) Q_D return QLocale::C; } -QLocale::Script QLocalePrivate::codeToScript(const QChar *code, int len) Q_DECL_NOTHROW +QLocale::Script QLocalePrivate::codeToScript(QStringView code) Q_DECL_NOTHROW { + const auto len = code.size(); if (len != 4) return QLocale::AnyScript; @@ -150,10 +152,12 @@ QLocale::Script QLocalePrivate::codeToScript(const QChar *code, int len) Q_DECL_ return QLocale::AnyScript; } -QLocale::Country QLocalePrivate::codeToCountry(const QChar *code, int len) Q_DECL_NOTHROW +QLocale::Country QLocalePrivate::codeToCountry(QStringView code) Q_DECL_NOTHROW { + const auto len = code.size(); if (len != 2 && len != 3) return QLocale::AnyCountry; + ushort uc1 = code[0].toUpper().unicode(); ushort uc2 = code[1].toUpper().unicode(); ushort uc3 = len > 2 ? code[2].toUpper().unicode() : 0; @@ -167,48 +171,35 @@ QLocale::Country QLocalePrivate::codeToCountry(const QChar *code, int len) Q_DEC return QLocale::AnyCountry; } -QString QLocalePrivate::languageToCode(QLocale::Language language) +QLatin1String QLocalePrivate::languageToCode(QLocale::Language language) { if (language == QLocale::AnyLanguage) - return QString(); + return QLatin1String(); if (language == QLocale::C) return QLatin1String("C"); const unsigned char *c = language_code_list + 3*(uint(language)); - QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized); - - code[0] = ushort(c[0]); - code[1] = ushort(c[1]); - if (c[2] != 0) - code[2] = ushort(c[2]); + return QLatin1String(reinterpret_cast<const char*>(c), c[2] == 0 ? 2 : 3); - return code; } -QString QLocalePrivate::scriptToCode(QLocale::Script script) +QLatin1String QLocalePrivate::scriptToCode(QLocale::Script script) { if (script == QLocale::AnyScript || script > QLocale::LastScript) - return QString(); + return QLatin1String(); const unsigned char *c = script_code_list + 4*(uint(script)); - return QString::fromLatin1((const char *)c, 4); + return QLatin1String(reinterpret_cast<const char *>(c), 4); } -QString QLocalePrivate::countryToCode(QLocale::Country country) +QLatin1String QLocalePrivate::countryToCode(QLocale::Country country) { if (country == QLocale::AnyCountry) - return QString(); + return QLatin1String(); const unsigned char *c = country_code_list + 3*(uint(country)); - QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized); - - code[0] = ushort(c[0]); - code[1] = ushort(c[1]); - if (c[2] != 0) - code[2] = ushort(c[2]); - - return code; + return QLatin1String(reinterpret_cast<const char*>(c), c[2] == 0 ? 2 : 3); } // http://www.unicode.org/reports/tr35/#Likely_Subtags @@ -1122,27 +1113,27 @@ QString QLocale::name() const return d->languageCode() + QLatin1Char('_') + d->countryCode(); } -static qlonglong toIntegral_helper(const QLocaleData *d, const QChar *data, int len, bool *ok, +static qlonglong toIntegral_helper(const QLocaleData *d, QStringView str, bool *ok, QLocale::NumberOptions mode, qlonglong) { - return d->stringToLongLong(data, len, 10, ok, mode); + return d->stringToLongLong(str, 10, ok, mode); } -static qulonglong toIntegral_helper(const QLocaleData *d, const QChar *data, int len, bool *ok, +static qulonglong toIntegral_helper(const QLocaleData *d, QStringView str, bool *ok, QLocale::NumberOptions mode, qulonglong) { - return d->stringToUnsLongLong(data, len, 10, ok, mode); + return d->stringToUnsLongLong(str, 10, ok, mode); } template <typename T> static inline -T toIntegral_helper(const QLocalePrivate *d, const QChar *data, int len, bool *ok) +T toIntegral_helper(const QLocalePrivate *d, QStringView str, bool *ok) { // ### Qt6: use std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type const bool isUnsigned = T(0) < T(-1); typedef typename QtPrivate::QConditional<isUnsigned, qulonglong, qlonglong>::Type Int64; // we select the right overload by the last, unused parameter - Int64 val = toIntegral_helper(d->m_data, data, len, ok, d->m_numberOptions, Int64()); + Int64 val = toIntegral_helper(d->m_data, str, ok, d->m_numberOptions, Int64()); if (T(val) != val) { if (ok) *ok = false; @@ -1211,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. @@ -1226,7 +1218,7 @@ QString QLocale::scriptToString(QLocale::Script script) short QLocale::toShort(const QString &s, bool *ok) const { - return toIntegral_helper<short>(d, s.constData(), s.size(), ok); + return toIntegral_helper<short>(d, s, ok); } /*! @@ -1244,7 +1236,7 @@ short QLocale::toShort(const QString &s, bool *ok) const ushort QLocale::toUShort(const QString &s, bool *ok) const { - return toIntegral_helper<ushort>(d, s.constData(), s.size(), ok); + return toIntegral_helper<ushort>(d, s, ok); } /*! @@ -1262,7 +1254,7 @@ ushort QLocale::toUShort(const QString &s, bool *ok) const int QLocale::toInt(const QString &s, bool *ok) const { - return toIntegral_helper<int>(d, s.constData(), s.size(), ok); + return toIntegral_helper<int>(d, s, ok); } /*! @@ -1280,7 +1272,7 @@ int QLocale::toInt(const QString &s, bool *ok) const uint QLocale::toUInt(const QString &s, bool *ok) const { - return toIntegral_helper<uint>(d, s.constData(), s.size(), ok); + return toIntegral_helper<uint>(d, s, ok); } /*! @@ -1299,7 +1291,7 @@ uint QLocale::toUInt(const QString &s, bool *ok) const qlonglong QLocale::toLongLong(const QString &s, bool *ok) const { - return toIntegral_helper<qlonglong>(d, s.constData(), s.size(), ok); + return toIntegral_helper<qlonglong>(d, s, ok); } /*! @@ -1318,7 +1310,7 @@ qlonglong QLocale::toLongLong(const QString &s, bool *ok) const qulonglong QLocale::toULongLong(const QString &s, bool *ok) const { - return toIntegral_helper<qulonglong>(d, s.constData(), s.size(), ok); + return toIntegral_helper<qulonglong>(d, s, ok); } /*! @@ -1361,7 +1353,7 @@ float QLocale::toFloat(const QString &s, bool *ok) const double QLocale::toDouble(const QString &s, bool *ok) const { - return d->m_data->stringToDouble(s.constData(), s.size(), ok, d->m_numberOptions); + return d->m_data->stringToDouble(s, ok, d->m_numberOptions); } /*! @@ -1381,7 +1373,7 @@ double QLocale::toDouble(const QString &s, bool *ok) const short QLocale::toShort(const QStringRef &s, bool *ok) const { - return toIntegral_helper<short>(d, s.constData(), s.size(), ok); + return toIntegral_helper<short>(d, s, ok); } /*! @@ -1401,7 +1393,7 @@ short QLocale::toShort(const QStringRef &s, bool *ok) const ushort QLocale::toUShort(const QStringRef &s, bool *ok) const { - return toIntegral_helper<ushort>(d, s.constData(), s.size(), ok); + return toIntegral_helper<ushort>(d, s, ok); } /*! @@ -1421,7 +1413,7 @@ ushort QLocale::toUShort(const QStringRef &s, bool *ok) const int QLocale::toInt(const QStringRef &s, bool *ok) const { - return toIntegral_helper<int>(d, s.constData(), s.size(), ok); + return toIntegral_helper<int>(d, s, ok); } /*! @@ -1441,7 +1433,7 @@ int QLocale::toInt(const QStringRef &s, bool *ok) const uint QLocale::toUInt(const QStringRef &s, bool *ok) const { - return toIntegral_helper<uint>(d, s.constData(), s.size(), ok); + return toIntegral_helper<uint>(d, s, ok); } /*! @@ -1462,7 +1454,7 @@ uint QLocale::toUInt(const QStringRef &s, bool *ok) const qlonglong QLocale::toLongLong(const QStringRef &s, bool *ok) const { - return toIntegral_helper<qlonglong>(d, s.constData(), s.size(), ok); + return toIntegral_helper<qlonglong>(d, s, ok); } /*! @@ -1483,7 +1475,7 @@ qlonglong QLocale::toLongLong(const QStringRef &s, bool *ok) const qulonglong QLocale::toULongLong(const QStringRef &s, bool *ok) const { - return toIntegral_helper<qulonglong>(d, s.constData(), s.size(), ok); + return toIntegral_helper<qulonglong>(d, s, ok); } /*! @@ -1530,9 +1522,178 @@ float QLocale::toFloat(const QStringRef &s, bool *ok) const double QLocale::toDouble(const QStringRef &s, bool *ok) const { - return d->m_data->stringToDouble(s.constData(), s.size(), ok, d->m_numberOptions); + 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. @@ -3076,12 +3237,12 @@ QString QLocaleData::unsLongLongToString(const QChar zero, const QChar group, number. We can't detect junk here, since we don't even know the base of the number. */ -bool QLocaleData::numberToCLocale(const QChar *str, int len, QLocale::NumberOptions number_options, +bool QLocaleData::numberToCLocale(QStringView s, QLocale::NumberOptions number_options, CharBuff *result) const { - const QChar *uc = str; - int l = len; - int idx = 0; + const QChar *uc = s.data(); + auto l = s.size(); + decltype(l) idx = 0; // Skip whitespace while (idx < l && uc[idx].isSpace()) @@ -3199,7 +3360,7 @@ bool QLocaleData::numberToCLocale(const QChar *str, int len, QLocale::NumberOpti return idx == l; } -bool QLocaleData::validateChars(const QString &str, NumberMode numMode, QByteArray *buff, +bool QLocaleData::validateChars(QStringView str, NumberMode numMode, QByteArray *buff, int decDigits, QLocale::NumberOptions number_options) const { buff->clear(); @@ -3213,7 +3374,7 @@ bool QLocaleData::validateChars(const QString &str, NumberMode numMode, QByteArr bool dec = false; int decDigitCnt = 0; - for (int i = 0; i < str.length(); ++i) { + for (QStringView::size_type i = 0; i < str.size(); ++i) { char c = digitToCLocale(str.at(i)); if (c >= '0' && c <= '9') { @@ -3300,11 +3461,11 @@ bool QLocaleData::validateChars(const QString &str, NumberMode numMode, QByteArr return true; } -double QLocaleData::stringToDouble(const QChar *begin, int len, bool *ok, +double QLocaleData::stringToDouble(QStringView str, bool *ok, QLocale::NumberOptions number_options) const { CharBuff buff; - if (!numberToCLocale(begin, len, number_options, &buff)) { + if (!numberToCLocale(str, number_options, &buff)) { if (ok != 0) *ok = false; return 0.0; @@ -3317,11 +3478,11 @@ double QLocaleData::stringToDouble(const QChar *begin, int len, bool *ok, return d; } -qlonglong QLocaleData::stringToLongLong(const QChar *begin, int len, int base, bool *ok, +qlonglong QLocaleData::stringToLongLong(QStringView str, int base, bool *ok, QLocale::NumberOptions number_options) const { CharBuff buff; - if (!numberToCLocale(begin, len, number_options, &buff)) { + if (!numberToCLocale(str, number_options, &buff)) { if (ok != 0) *ok = false; return 0; @@ -3330,11 +3491,11 @@ qlonglong QLocaleData::stringToLongLong(const QChar *begin, int len, int base, b return bytearrayToLongLong(buff.constData(), base, ok); } -qulonglong QLocaleData::stringToUnsLongLong(const QChar *begin, int len, int base, bool *ok, +qulonglong QLocaleData::stringToUnsLongLong(QStringView str, int base, bool *ok, QLocale::NumberOptions number_options) const { CharBuff buff; - if (!numberToCLocale(begin, len, number_options, &buff)) { + if (!numberToCLocale(str, number_options, &buff)) { if (ok != 0) *ok = false; return 0; |