diff options
author | Oswald Buddenhagen <oswald.buddenhagen@digia.com> | 2013-03-20 13:46:57 +0100 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@digia.com> | 2013-03-20 13:49:28 +0100 |
commit | 76c0be34cd4ff4564693162fa7528463e23ce9d8 (patch) | |
tree | f165b7bc319548fb0082365411a871028f92e89e /src/corelib/tools/qlocale.cpp | |
parent | 27b4fe96b59e9e63d1e570e802c072e9afdfb2d4 (diff) | |
parent | 36cb3f3f655a9090c82de609010cbfb88651a0f3 (diff) |
Merge branch 'dev' into stable
This starts Qt 5.1 release cycle
Conflicts:
src/gui/text/qfontdatabase.cpp
src/gui/text/qharfbuzz_copy_p.h
src/widgets/kernel/qapplication.cpp
src/widgets/kernel/qcoreapplication.cpp
Change-Id: I72fbf83ab3c2206aeea1b089428b0fc2a89bd62b
Diffstat (limited to 'src/corelib/tools/qlocale.cpp')
-rw-r--r-- | src/corelib/tools/qlocale.cpp | 326 |
1 files changed, 310 insertions, 16 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index 365033d84a..21a01c4867 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -164,14 +164,14 @@ QLocale::Country QLocalePrivate::codeToCountry(const QString &code) return QLocale::AnyCountry; } -QString QLocalePrivate::languageCode() const +QString QLocalePrivate::languageToCode(QLocale::Language language) { - if (m_data->m_language_id == QLocale::AnyLanguage) + if (language == QLocale::AnyLanguage) return QString(); - if (m_data->m_language_id == QLocale::C) + if (language == QLocale::C) return QLatin1String("C"); - const unsigned char *c = language_code_list + 3*(uint(m_data->m_language_id)); + const unsigned char *c = language_code_list + 3*(uint(language)); QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized); @@ -183,20 +183,20 @@ QString QLocalePrivate::languageCode() const return code; } -QString QLocalePrivate::scriptCode() const +QString QLocalePrivate::scriptToCode(QLocale::Script script) { - if (m_data->m_script_id == QLocale::AnyScript || m_data->m_script_id > QLocale::LastScript) + if (script == QLocale::AnyScript || script > QLocale::LastScript) return QString(); - const unsigned char *c = script_code_list + 4*(uint(m_data->m_script_id)); + const unsigned char *c = script_code_list + 4*(uint(script)); return QString::fromLatin1((const char *)c, 4); } -QString QLocalePrivate::countryCode() const +QString QLocalePrivate::countryToCode(QLocale::Country country) { - if (m_data->m_country_id == QLocale::AnyCountry) + if (country == QLocale::AnyCountry) return QString(); - const unsigned char *c = country_code_list + 3*(uint(m_data->m_country_id)); + const unsigned char *c = country_code_list + 3*(uint(country)); QString code(c[2] == 0 ? 2 : 3, Qt::Uninitialized); @@ -1327,6 +1327,224 @@ double QLocale::toDouble(const QString &s, bool *ok) const } /*! + 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.1 +*/ + +short QLocale::toShort(const QStringRef &s, bool *ok) const +{ + qlonglong i = toLongLong(s, ok); + if (i < SHRT_MIN || i > SHRT_MAX) { + if (ok) + *ok = false; + return 0; + } + return short(i); +} + +/*! + 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.1 +*/ + +ushort QLocale::toUShort(const QStringRef &s, bool *ok) const +{ + qulonglong i = toULongLong(s, ok); + if (i > USHRT_MAX) { + if (ok) + *ok = false; + return 0; + } + return ushort(i); +} + +/*! + 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.1 +*/ + +int QLocale::toInt(const QStringRef &s, bool *ok) const +{ + qlonglong i = toLongLong(s, ok); + if (i < INT_MIN || i > INT_MAX) { + if (ok) + *ok = false; + return 0; + } + return int(i); +} + +/*! + 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.1 +*/ + +uint QLocale::toUInt(const QStringRef &s, bool *ok) const +{ + qulonglong i = toULongLong(s, ok); + if (i > UINT_MAX) { + if (ok) + *ok = false; + return 0; + } + return uint(i); +} + +/*! + 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.1 +*/ + + +qlonglong QLocale::toLongLong(const QStringRef &s, bool *ok) const +{ + QLocalePrivate::GroupSeparatorMode mode + = d->m_numberOptions & RejectGroupSeparator + ? QLocalePrivate::FailOnGroupSeparators + : QLocalePrivate::ParseGroupSeparators; + + return d->stringToLongLong(s, 10, ok, mode); +} + +/*! + 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.1 +*/ + +qulonglong QLocale::toULongLong(const QStringRef &s, bool *ok) const +{ + QLocalePrivate::GroupSeparatorMode mode + = d->m_numberOptions & RejectGroupSeparator + ? QLocalePrivate::FailOnGroupSeparators + : QLocalePrivate::ParseGroupSeparators; + + return d->stringToUnsLongLong(s, 10, ok, mode); +} + +/*! + 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.1 +*/ + +float QLocale::toFloat(const QStringRef &s, bool *ok) const +{ + bool myOk; + double d = toDouble(s, &myOk); + if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) { + if (ok) + *ok = false; + return 0.0; + } + if (ok) + *ok = true; + return float(d); +} + +/*! + 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 + + 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.1 +*/ + +double QLocale::toDouble(const QStringRef &s, bool *ok) const +{ + QLocalePrivate::GroupSeparatorMode mode + = d->m_numberOptions & RejectGroupSeparator + ? QLocalePrivate::FailOnGroupSeparators + : QLocalePrivate::ParseGroupSeparators; + + return d->stringToDouble(s, ok, mode); +} + + +/*! Returns a localized string representation of \a i. \sa toLongLong() @@ -2892,12 +3110,12 @@ QString QLocalePrivate::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 QLocalePrivate::numberToCLocale(const QString &num, +bool QLocalePrivate::numberToCLocale(const QChar *str, int len, GroupSeparatorMode group_sep_mode, CharBuff *result) const { - const QChar *uc = num.unicode(); - int l = num.length(); + const QChar *uc = str; + int l = len; int idx = 0; // Skip whitespace @@ -3043,7 +3261,13 @@ double QLocalePrivate::stringToDouble(const QString &number, bool *ok, GroupSeparatorMode group_sep_mode) const { CharBuff buff; - if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number, + // Do not use the ternary operator - triggers msvc2012 bug in optimized builds + QString trimmedNumber; + if (group().unicode() == 0xa0) + trimmedNumber = number.trimmed(); + else + trimmedNumber = number; + if (!numberToCLocale(trimmedNumber.unicode(), trimmedNumber.size(), group_sep_mode, &buff)) { if (ok != 0) *ok = false; @@ -3056,7 +3280,13 @@ qlonglong QLocalePrivate::stringToLongLong(const QString &number, int base, bool *ok, GroupSeparatorMode group_sep_mode) const { CharBuff buff; - if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number, + // Do not use the ternary operator - triggers msvc2012 bug in optimized builds + QString trimmedNumber; + if (group().unicode() == 0xa0) + trimmedNumber = number.trimmed(); + else + trimmedNumber = number; + if (!numberToCLocale(trimmedNumber.unicode(), trimmedNumber.size(), group_sep_mode, &buff)) { if (ok != 0) *ok = false; @@ -3070,7 +3300,13 @@ qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base, bool *ok, GroupSeparatorMode group_sep_mode) const { CharBuff buff; - if (!numberToCLocale(group().unicode() == 0xa0 ? number.trimmed() : number, + // Do not use the ternary operator - triggers msvc2012 bug in optimized builds + QString trimmedNumber; + if (group().unicode() == 0xa0) + trimmedNumber = number.trimmed(); + else + trimmedNumber = number; + if (!numberToCLocale(trimmedNumber.unicode(), trimmedNumber.size(), group_sep_mode, &buff)) { if (ok != 0) *ok = false; @@ -3080,6 +3316,64 @@ qulonglong QLocalePrivate::stringToUnsLongLong(const QString &number, int base, return bytearrayToUnsLongLong(buff.constData(), base, ok); } +double QLocalePrivate::stringToDouble(const QStringRef &number, bool *ok, + GroupSeparatorMode group_sep_mode) const +{ + CharBuff buff; + QStringRef trimmedNumber; + // Do not use the ternary operator - triggers msvc2012 bug in optimized builds + if (group().unicode() == 0xa0) + trimmedNumber = number.trimmed(); + else + trimmedNumber = number; + if (!numberToCLocale(trimmedNumber.unicode(), trimmedNumber.size(), + group_sep_mode, &buff)) { + if (ok != 0) + *ok = false; + return 0.0; + } + return bytearrayToDouble(buff.constData(), ok); +} + +qlonglong QLocalePrivate::stringToLongLong(const QStringRef &number, int base, + bool *ok, GroupSeparatorMode group_sep_mode) const +{ + CharBuff buff; + QStringRef trimmedNumber; + // Do not use the ternary operator - triggers msvc2012 bug in optimized builds + if (group().unicode() == 0xa0) + trimmedNumber = number.trimmed(); + else + trimmedNumber = number; + if (!numberToCLocale(trimmedNumber.unicode(), trimmedNumber.size(), + group_sep_mode, &buff)) { + if (ok != 0) + *ok = false; + return 0; + } + + return bytearrayToLongLong(buff.constData(), base, ok); +} + +qulonglong QLocalePrivate::stringToUnsLongLong(const QStringRef &number, int base, + bool *ok, GroupSeparatorMode group_sep_mode) const +{ + CharBuff buff; + QStringRef trimmedNumber; + // Do not use the ternary operator - triggers msvc2012 bug in optimized builds + if (group().unicode() == 0xa0) + trimmedNumber = number.trimmed(); + else + trimmedNumber = number; + if (!numberToCLocale(trimmedNumber.unicode(), trimmedNumber.size(), + group_sep_mode, &buff)) { + if (ok != 0) + *ok = false; + return 0; + } + + return bytearrayToUnsLongLong(buff.constData(), base, ok); +} double QLocalePrivate::bytearrayToDouble(const char *num, bool *ok, bool *overflow) { |