From 1e3833bed8a661ee8ae284188a23bd0ffc3bed85 Mon Sep 17 00:00:00 2001 From: John Layt Date: Sun, 22 Jan 2012 21:23:50 +0000 Subject: QString: Make convert to number methods only use C locale Ensure consistent conversions by not using the system default locale. Change-Id: I60db9fc4f465c0254f3213419e57d7879aaddd65 Reviewed-by: Thiago Macieira Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.cpp | 103 ++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 50 deletions(-) (limited to 'src/corelib/tools/qstring.cpp') diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index c285f7a007..524096b889 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -5433,11 +5433,14 @@ QString &QString::vsprintf(const char* cformat, va_list ap) 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 74 - \sa number(), toULongLong(), toInt() + \sa number(), toULongLong(), toInt(), QLocale::toLongLong() */ qint64 QString::toLongLong(bool *ok, int base) const @@ -5449,15 +5452,6 @@ qint64 QString::toLongLong(bool *ok, int base) const } #endif - bool my_ok; - QLocale def_locale; - qint64 result = def_locale.d()->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators); - if (my_ok) { - if (ok != 0) - *ok = true; - return result; - } - QLocale c_locale(QLocale::C); return c_locale.d()->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators); } @@ -5474,11 +5468,14 @@ qint64 QString::toLongLong(bool *ok, int base) const 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 79 - \sa number(), toLongLong() + \sa number(), toLongLong(), QLocale::toULongLong() */ quint64 QString::toULongLong(bool *ok, int base) const @@ -5490,15 +5487,6 @@ quint64 QString::toULongLong(bool *ok, int base) const } #endif - bool my_ok; - QLocale def_locale; - quint64 result = def_locale.d()->stringToUnsLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators); - if (my_ok) { - if (ok != 0) - *ok = true; - return result; - } - QLocale c_locale(QLocale::C); return c_locale.d()->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators); } @@ -5517,11 +5505,14 @@ quint64 QString::toULongLong(bool *ok, int base) const 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 73 - \sa number(), toULong(), toInt() + \sa number(), toULong(), toInt(), QLocale::toLong() */ long QString::toLong(bool *ok, int base) const @@ -5549,11 +5540,14 @@ long QString::toLong(bool *ok, int base) const 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::toULong() + Example: \snippet doc/src/snippets/qstring/main.cpp 78 - \sa number() + \sa number(), QLocale::toULong() */ ulong QString::toULong(bool *ok, int base) const @@ -5580,11 +5574,14 @@ ulong QString::toULong(bool *ok, int base) const 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 72 - \sa number(), toUInt(), toDouble() + \sa number(), toUInt(), toDouble(), QLocale::toInt() */ int QString::toInt(bool *ok, int base) const @@ -5610,11 +5607,14 @@ int QString::toInt(bool *ok, int base) const 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 77 - \sa number(), toInt() + \sa number(), toInt(), QLocale::toUInt() */ uint QString::toUInt(bool *ok, int base) const @@ -5640,11 +5640,14 @@ uint QString::toUInt(bool *ok, int base) const 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 76 - \sa number(), toUShort(), toInt() + \sa number(), toUShort(), toInt(), QLocale::toShort() */ short QString::toShort(bool *ok, int base) const @@ -5670,11 +5673,14 @@ short QString::toShort(bool *ok, int base) const 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() + Example: \snippet doc/src/snippets/qstring/main.cpp 80 - \sa number(), toShort() + \sa number(), toShort(), QLocale::toUShort() */ ushort QString::toUShort(bool *ok, int base) const @@ -5704,37 +5710,22 @@ ushort QString::toUShort(bool *ok, int base) const \snippet doc/src/snippets/qstring/main.cpp 67 - This function tries to interpret the string according to the - current locale. The current locale is determined from the - system at application startup and can be changed by calling - QLocale::setDefault(). If the string cannot be interpreted - according to the current locale, this function falls back - on the "C" locale. + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toDouble() - \snippet doc/src/snippets/qstring/main.cpp 69 - \snippet doc/src/snippets/qstring/main.cpp 70 + \snippet doc/src/snippets/qstring/main.cpp 68 - Due to the ambiguity between the decimal point and thousands group - separator in various locales, this function does not handle + For historic reasons, this function does not handle thousands group separators. If you need to convert such numbers, - see QLocale::toDouble(). + use QLocale::toDouble(). - \snippet doc/src/snippets/qstring/main.cpp 68 + \snippet doc/src/snippets/qstring/main.cpp 69 \sa number() QLocale::setDefault() QLocale::toDouble() trimmed() */ double QString::toDouble(bool *ok) const { - bool my_ok; - QLocale def_locale; - double result = def_locale.d()->stringToDouble(*this, &my_ok, QLocalePrivate::FailOnGroupSeparators); - if (my_ok) { - if (ok != 0) - *ok = true; - return result; - } - QLocale c_locale(QLocale::C); return c_locale.d()->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators); } @@ -5745,11 +5736,14 @@ double QString::toDouble(bool *ok) const If a conversion error occurs, *\a{ok} is set to false; otherwise *\a{ok} is set to true. Returns 0.0 if the conversion fails. + The string conversion will always happen in the 'C' locale. For locale + dependent conversion use QLocale::toFloat() + Example: \snippet doc/src/snippets/qstring/main.cpp 71 - \sa number(), toDouble(), toInt() + \sa number(), toDouble(), toInt(), QLocale::toFloat() */ #define QT_MAX_FLOAT 3.4028234663852886e+38 @@ -5851,8 +5845,9 @@ QString &QString::setNum(qulonglong n, int base) The \a format can be 'f', 'F', 'e', 'E', 'g' or 'G' (see the arg() function documentation for an explanation of the formats). - Unlike QLocale::toString(), this function doesn't honor the - user's locale settings. + The formatting always uses QLocale::C, i.e., English/UnitedStates. + To get a localized string representation of a number, use + QLocale::toString() with the appropriate locale. */ QString &QString::setNum(double n, char f, int prec) @@ -5893,6 +5888,10 @@ QString &QString::setNum(double n, char f, int prec) Sets the string to the printed value of \a n, formatted according to the given \a format and \a precision, and returns a reference to the string. + + The formatting always uses QLocale::C, i.e., English/UnitedStates. + To get a localized string representation of a number, use + QLocale::toString() with the appropriate locale. */ @@ -5906,6 +5905,10 @@ QString &QString::setNum(double n, char f, int prec) and 36. For bases other than 10, \a n is treated as an unsigned integer. + The formatting always uses QLocale::C, i.e., English/UnitedStates. + To get a localized string representation of a number, use + QLocale::toString() with the appropriate locale. + \snippet doc/src/snippets/qstring/main.cpp 35 \sa setNum() -- cgit v1.2.3