diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2014-02-04 16:39:44 -0800 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-14 00:34:25 +0100 |
commit | d9440df5e9bbc76dbc7d5281c9176a6f26aa173d (patch) | |
tree | 9bcdeaa10f950ab5d12a1d420c711c287cde97c0 /src/corelib/tools/qstring.cpp | |
parent | 3c15118fa38aaec13125ff82f9cfeabfdd57fd21 (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.cpp')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 131 |
1 files changed, 28 insertions, 103 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 0c0bcac1b6..5bc485be7a 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -6068,16 +6068,22 @@ QString &QString::vsprintf(const char* cformat, va_list ap) qint64 QString::toLongLong(bool *ok, int base) const { + return toIntegral_helper<qlonglong>(constData(), size(), ok, base); +} + +qlonglong QString::toIntegral_helper(const QChar *data, int len, bool *ok, int base) +{ #if defined(QT_CHECK_RANGE) if (base != 0 && (base < 2 || base > 36)) { - qWarning("QString::toLongLong: Invalid base (%d)", base); + qWarning("QString::toULongLong: Invalid base (%d)", base); base = 10; } #endif - return QLocaleData::c()->stringToLongLong(constData(), size(), base, ok, QLocaleData::FailOnGroupSeparators); + return QLocaleData::c()->stringToLongLong(data, len, base, ok, QLocaleData::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. @@ -6102,6 +6108,11 @@ qint64 QString::toLongLong(bool *ok, int base) const quint64 QString::toULongLong(bool *ok, int base) const { + return toIntegral_helper<qulonglong>(constData(), size(), ok, base); +} + +qulonglong QString::toIntegral_helper(const QChar *data, uint len, bool *ok, int base) +{ #if defined(QT_CHECK_RANGE) if (base != 0 && (base < 2 || base > 36)) { qWarning("QString::toULongLong: Invalid base (%d)", base); @@ -6109,7 +6120,7 @@ quint64 QString::toULongLong(bool *ok, int base) const } #endif - return QLocaleData::c()->stringToUnsLongLong(constData(), size(), base, ok, QLocaleData::FailOnGroupSeparators); + return QLocaleData::c()->stringToUnsLongLong(data, len, base, ok, QLocaleData::FailOnGroupSeparators); } /*! @@ -6138,13 +6149,7 @@ quint64 QString::toULongLong(bool *ok, int base) const long QString::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; + return toIntegral_helper<long>(constData(), size(), ok, base); } /*! @@ -6173,13 +6178,7 @@ long QString::toLong(bool *ok, int base) const ulong QString::toULong(bool *ok, int base) const { - quint64 v = toULongLong(ok, base); - if (v > ULONG_MAX) { - if (ok) - *ok = false; - v = 0; - } - return (ulong)v; + return toIntegral_helper<ulong>(constData(), size(), ok, base); } @@ -6207,13 +6206,7 @@ ulong QString::toULong(bool *ok, int base) const int QString::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 v; + return toIntegral_helper<int>(constData(), size(), ok, base); } /*! @@ -6240,13 +6233,7 @@ int QString::toInt(bool *ok, int base) const uint QString::toUInt(bool *ok, int base) const { - quint64 v = toULongLong(ok, base); - if (v > UINT_MAX) { - if (ok) - *ok = false; - v = 0; - } - return (uint)v; + return toIntegral_helper<uint>(constData(), size(), ok, base); } /*! @@ -6273,13 +6260,7 @@ uint QString::toUInt(bool *ok, int base) const short QString::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; + return toIntegral_helper<short>(constData(), size(), ok, base); } /*! @@ -6306,13 +6287,7 @@ short QString::toShort(bool *ok, int base) const ushort QString::toUShort(bool *ok, int base) const { - ulong v = toULongLong(ok, base); - if (v > USHRT_MAX) { - if (ok) - *ok = false; - v = 0; - } - return (ushort)v; + return toIntegral_helper<ushort>(constData(), size(), ok, base); } @@ -9606,14 +9581,7 @@ QStringRef QStringRef::trimmed() const 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 - - return QLocaleData::c()->stringToLongLong(constData(), size(), base, ok, QLocaleData::FailOnGroupSeparators); + return QString::toIntegral_helper<qint64>(constData(), size(), ok, base); } /*! @@ -9638,14 +9606,7 @@ qint64 QStringRef::toLongLong(bool *ok, int base) const 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 - - return QLocaleData::c()->stringToUnsLongLong(constData(), size(), base, ok, QLocaleData::FailOnGroupSeparators); + return QString::toIntegral_helper<quint64>(constData(), size(), ok, base); } /*! @@ -9672,13 +9633,7 @@ quint64 QStringRef::toULongLong(bool *ok, int base) const 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); + return QString::toIntegral_helper<long>(constData(), size(), ok, base); } /*! @@ -9705,13 +9660,7 @@ long QStringRef::toLong(bool *ok, int base) const 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); + return QString::toIntegral_helper<ulong>(constData(), size(), ok, base); } @@ -9737,13 +9686,7 @@ ulong QStringRef::toULong(bool *ok, int base) const 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); + return QString::toIntegral_helper<int>(constData(), size(), ok, base); } /*! @@ -9768,13 +9711,7 @@ int QStringRef::toInt(bool *ok, int base) const 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); + return QString::toIntegral_helper<uint>(constData(), size(), ok, base); } /*! @@ -9799,13 +9736,7 @@ uint QStringRef::toUInt(bool *ok, int base) const 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); + return QString::toIntegral_helper<short>(constData(), size(), ok, base); } /*! @@ -9830,13 +9761,7 @@ short QStringRef::toShort(bool *ok, int base) const 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); + return QString::toIntegral_helper<ushort>(constData(), size(), ok, base); } |