summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qlocale.cpp
diff options
context:
space:
mode:
authorKeith Gardner <kreios4004@gmail.com>2013-02-02 13:37:31 -0600
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-18 22:24:54 +0100
commitcfc09b656420a42a9f4b8e7b8b3ebe4fc4a426a2 (patch)
treee5d660f5fbc86ebca28c7a5b18d7bd0b881126e0 /src/corelib/tools/qlocale.cpp
parent3654a4a3c1cb2ac9bb8b486877d9f1e6a6b220be (diff)
QLocale: Added QStringRef overloads to toInt(), toUInt(), etc...
Added the following function overloads to QLocale: toShort, toUShort, toInt, toUInt, toLong, toULong, toLongLong, toULongLong, toFloat, and toDouble. Change-Id: I8cd90ca08b88338b08a73a72492f4c91c4f46ea4 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qlocale.cpp')
-rw-r--r--src/corelib/tools/qlocale.cpp276
1 files changed, 276 insertions, 0 deletions
diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp
index 6c35f45cdb..454025f54d 100644
--- a/src/corelib/tools/qlocale.cpp
+++ b/src/corelib/tools/qlocale.cpp
@@ -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()
@@ -3098,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)
{