summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKeith Gardner <kreios4004@gmail.com>2012-12-28 16:35:39 -0600
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-02-22 21:23:33 +0100
commit3b5600f6ee9f2982c5a93250e577f87a34fe32d1 (patch)
tree9d3bcfa252d9a6b522a93c0944807c566bb50cb5 /src
parent736e4258a10c1df8dd5e4a5170fbc1a7d6e5588f (diff)
QStringRef: Added toInt(), toUInt(), etc... functions to QStringRef.
Added the following functions to QStringRef: toShort, toUShort, toInt, toUInt, toLong, toULong, toLongLong, toULongLong, toFloat, and toDouble. These functions use the corresponding functions found in QLocale. Updated tst_qstringref.cpp to exercise the new functionality. Change-Id: I38668a0cc7da0c101a62613fd16cb5a98286617f Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qstring.cpp319
-rw-r--r--src/corelib/tools/qstring.h10
2 files changed, 329 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 0be0a84459..bbe7628d38 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -9226,6 +9226,325 @@ QStringRef QStringRef::trimmed() const
}
/*!
+ Returns the string converted to a \c{long long} using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toLongLong()
+
+ \since 5.1
+*/
+
+qint64 QStringRef::toLongLong(bool *ok, int base) const
+{
+#if defined(QT_CHECK_RANGE)
+ if (base != 0 && (base < 2 || base > 36)) {
+ qWarning("QString::toLongLong: Invalid base (%d)", base);
+ base = 10;
+ }
+#endif
+
+ QLocale c_locale(QLocale::C);
+ return c_locale.d->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
+}
+
+/*!
+ Returns the string converted to an \c{unsigned long long} using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toULongLong()
+
+ \since 5.1
+*/
+
+quint64 QStringRef::toULongLong(bool *ok, int base) const
+{
+#if defined(QT_CHECK_RANGE)
+ if (base != 0 && (base < 2 || base > 36)) {
+ qWarning("QString::toULongLong: Invalid base (%d)", base);
+ base = 10;
+ }
+#endif
+
+ QLocale c_locale(QLocale::C);
+ return c_locale.d->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
+}
+
+/*!
+ \fn long QStringRef::toLong(bool *ok, int base) const
+
+ Returns the string converted to a \c long using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toLong()
+
+ \since 5.1
+*/
+
+long QStringRef::toLong(bool *ok, int base) const
+{
+ qint64 v = toLongLong(ok, base);
+ if (v < LONG_MIN || v > LONG_MAX) {
+ if (ok)
+ *ok = false;
+ v = 0;
+ }
+ return long(v);
+}
+
+/*!
+ \fn ulong QStringRef::toULong(bool *ok, int base) const
+
+ Returns the string converted to an \c{unsigned long} using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toULong()
+
+ \since 5.1
+*/
+
+ulong QStringRef::toULong(bool *ok, int base) const
+{
+ quint64 v = toULongLong(ok, base);
+ if (v > ULONG_MAX) {
+ if (ok)
+ *ok = false;
+ v = 0;
+ }
+ return ulong(v);
+}
+
+
+/*!
+ Returns the string converted to an \c int using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toInt()
+
+ \since 5.1
+*/
+
+int QStringRef::toInt(bool *ok, int base) const
+{
+ qint64 v = toLongLong(ok, base);
+ if (v < INT_MIN || v > INT_MAX) {
+ if (ok)
+ *ok = false;
+ v = 0;
+ }
+ return int(v);
+}
+
+/*!
+ Returns the string converted to an \c{unsigned int} using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toUInt()
+
+ \since 5.1
+*/
+
+uint QStringRef::toUInt(bool *ok, int base) const
+{
+ quint64 v = toULongLong(ok, base);
+ if (v > UINT_MAX) {
+ if (ok)
+ *ok = false;
+ v = 0;
+ }
+ return uint(v);
+}
+
+/*!
+ Returns the string converted to a \c short using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toShort()
+
+ \since 5.1
+*/
+
+short QStringRef::toShort(bool *ok, int base) const
+{
+ long v = toLongLong(ok, base);
+ if (v < SHRT_MIN || v > SHRT_MAX) {
+ if (ok)
+ *ok = false;
+ v = 0;
+ }
+ return short(v);
+}
+
+/*!
+ Returns the string converted to an \c{unsigned short} using base \a
+ base, which is 10 by default and must be between 2 and 36, or 0.
+ Returns 0 if the conversion fails.
+
+ If a conversion error occurs, *\a{ok} is set to false; otherwise
+ *\a{ok} is set to true.
+
+ If \a base is 0, the C language convention is used: If the string
+ 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()
+
+ \sa QString::toUShort()
+
+ \since 5.1
+*/
+
+ushort QStringRef::toUShort(bool *ok, int base) const
+{
+ ulong v = toULongLong(ok, base);
+ if (v > USHRT_MAX) {
+ if (ok)
+ *ok = false;
+ v = 0;
+ }
+ return ushort(v);
+}
+
+
+/*!
+ Returns the string converted to a \c double value.
+
+ Returns 0.0 if the conversion fails.
+
+ If a conversion error occurs, \c{*}\a{ok} is set to false;
+ otherwise \c{*}\a{ok} is set to true.
+
+ The string conversion will always happen in the 'C' locale. For locale
+ dependent conversion use QLocale::toDouble()
+
+ For historic reasons, this function does not handle
+ thousands group separators. If you need to convert such numbers,
+ use QLocale::toDouble().
+
+ \sa QString::toDouble()
+
+ \since 5.1
+*/
+
+double QStringRef::toDouble(bool *ok) const
+{
+ QLocale c_locale(QLocale::C);
+ return c_locale.d->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
+}
+
+/*!
+ Returns the string converted to a \c float value.
+
+ 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()
+
+ \sa QString::toFloat()
+
+ \since 5.1
+*/
+
+float QStringRef::toFloat(bool *ok) const
+{
+ bool myOk;
+ double d = toDouble(&myOk);
+ if (!myOk) {
+ if (ok != 0)
+ *ok = false;
+ return 0.0;
+ }
+ if (qIsInf(d))
+ return float(d);
+ if (d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
+ if (ok != 0)
+ *ok = false;
+ return 0.0;
+ }
+ if (ok)
+ *ok = true;
+ return float(d);
+}
+
+/*!
\obsolete
\fn QString Qt::escape(const QString &plain)
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index 9cde603c0b..d152e6795e 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -1279,6 +1279,16 @@ public:
static int localeAwareCompare(const QStringRef &s1, const QStringRef &s2);
QStringRef trimmed() const Q_REQUIRED_RESULT;
+ short toShort(bool *ok = 0, int base = 10) const;
+ ushort toUShort(bool *ok = 0, int base = 10) const;
+ int toInt(bool *ok = 0, int base = 10) const;
+ uint toUInt(bool *ok = 0, int base = 10) const;
+ long toLong(bool *ok = 0, int base = 10) const;
+ ulong toULong(bool *ok = 0, int base = 10) const;
+ qlonglong toLongLong(bool *ok = 0, int base = 10) const;
+ qulonglong toULongLong(bool *ok = 0, int base = 10) const;
+ float toFloat(bool *ok = 0) const;
+ double toDouble(bool *ok = 0) const;
};
Q_DECLARE_TYPEINFO(QStringRef, Q_PRIMITIVE_TYPE);