summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2014-02-04 16:39:44 -0800
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-14 00:34:25 +0100
commitd9440df5e9bbc76dbc7d5281c9176a6f26aa173d (patch)
tree9bcdeaa10f950ab5d12a1d420c711c287cde97c0 /src/corelib/tools/qstring.h
parent3c15118fa38aaec13125ff82f9cfeabfdd57fd21 (diff)
Centralize the handling of all the toXxx (integral) functions
By way of templates. This makes the code a lot cleaner. Change-Id: Ie369561c7631b0d34d76a6852883716cc0aa89d4 Reviewed-by: Lars Knoll <lars.knoll@digia.com>
Diffstat (limited to 'src/corelib/tools/qstring.h')
-rw-r--r--src/corelib/tools/qstring.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index 8fb817d519..9063f59171 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -552,6 +552,7 @@ public:
int localeAwareCompare(const QStringRef &s) const;
static int localeAwareCompare(const QString& s1, const QStringRef& s2);
+ // ### Qt6: make inline except for the long long versions
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;
@@ -746,6 +747,8 @@ private:
static QByteArray toUtf8_helper(const QString &);
static QByteArray toLocal8Bit_helper(const QChar *data, int size);
static int toUcs4_helper(const ushort *uc, int length, uint *out);
+ static qlonglong toIntegral_helper(const QChar *data, int len, bool *ok, int base);
+ static qulonglong toIntegral_helper(const QChar *data, uint len, bool *ok, int base);
void replace_helper(uint *indices, int nIndices, int blen, const QChar *after, int alen);
friend class QCharRef;
friend class QTextCodec;
@@ -754,6 +757,24 @@ private:
friend class QCollator;
friend struct QAbstractConcatenable;
+ template <typename T> static
+ T toIntegral_helper(const QChar *data, int len, bool *ok, int base)
+ {
+ // ### 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;
+ typedef typename QtPrivate::QConditional<isUnsigned, uint, int>::Type Int32;
+
+ // we select the right overload by casting size() to int or uint
+ Int64 val = toIntegral_helper(data, Int32(len), ok, base);
+ if (T(val) != val) {
+ if (ok)
+ *ok = false;
+ val = 0;
+ }
+ return T(val);
+ }
+
public:
typedef Data * DataPtr;
inline DataPtr &data_ptr() { return d; }