diff options
Diffstat (limited to 'src/corelib/tools/qbytearray.cpp')
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 125 |
1 files changed, 53 insertions, 72 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 3e3bae9a94..304ce69449 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -132,10 +132,10 @@ char *qstrcpy(char *dst, const char *src) return 0; #if defined(_MSC_VER) && _MSC_VER >= 1400 const int len = int(strlen(src)); - // This is actually not secure!!! It will be fixed - // properly in a later release! + // This is actually not secure!!! It will be fixed + // properly in a later release! if (len >= 0 && strcpy_s(dst, len+1, src) == 0) - return dst; + return dst; return 0; #else return strcpy(dst, src); @@ -166,7 +166,7 @@ char *qstrncpy(char *dst, const char *src, uint len) if (!src || !dst) return 0; #if defined(_MSC_VER) && _MSC_VER >= 1400 - strncpy_s(dst, len, src, len-1); + strncpy_s(dst, len, src, len-1); #else strncpy(dst, src, len); #endif @@ -3266,6 +3266,39 @@ QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); } +static qlonglong toIntegral_helper(const char *data, bool *ok, int base, qlonglong) +{ + return QLocaleData::bytearrayToLongLong(data, base, ok); +} + +static qulonglong toIntegral_helper(const char *data, bool *ok, int base, qulonglong) +{ + return QLocaleData::bytearrayToUnsLongLong(data, base, ok); +} + +template <typename T> static inline +T toIntegral_helper(const char *data, 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; + +#if defined(QT_CHECK_RANGE) + if (base != 0 && (base < 2 || base > 36)) { + qWarning("QByteArray::toIntegral: Invalid base %d", base); + base = 10; + } +#endif + + // we select the right overload by the last, unused parameter + Int64 val = toIntegral_helper(data, ok, base, Int64()); + if (T(val) != val) { + if (ok) + *ok = false; + val = 0; + } + return T(val); +} /*! Returns the byte array converted to a \c {long long} using base \a @@ -3289,14 +3322,7 @@ bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); } qlonglong QByteArray::toLongLong(bool *ok, int base) const { -#if defined(QT_CHECK_RANGE) - if (base != 0 && (base < 2 || base > 36)) { - qWarning("QByteArray::toLongLong: Invalid base %d", base); - base = 10; - } -#endif - - return QLocalePrivate::bytearrayToLongLong(nulTerminated().constData(), base, ok); + return toIntegral_helper<qlonglong>(nulTerminated().constData(), ok, base); } /*! @@ -3322,17 +3348,9 @@ qlonglong QByteArray::toLongLong(bool *ok, int base) const qulonglong QByteArray::toULongLong(bool *ok, int base) const { -#if defined(QT_CHECK_RANGE) - if (base != 0 && (base < 2 || base > 36)) { - qWarning("QByteArray::toULongLong: Invalid base %d", base); - base = 10; - } -#endif - - return QLocalePrivate::bytearrayToUnsLongLong(nulTerminated().constData(), base, ok); + return toIntegral_helper<qulonglong>(nulTerminated().constData(), ok, base); } - /*! Returns the byte array converted to an \c int using base \a base, which is 10 by default and must be between 2 and 36, or 0. @@ -3357,13 +3375,7 @@ qulonglong QByteArray::toULongLong(bool *ok, int base) const int QByteArray::toInt(bool *ok, int base) const { - qlonglong v = toLongLong(ok, base); - if (v < INT_MIN || v > INT_MAX) { - if (ok) - *ok = false; - v = 0; - } - return int(v); + return toIntegral_helper<int>(nulTerminated().constData(), ok, base); } /*! @@ -3388,13 +3400,7 @@ int QByteArray::toInt(bool *ok, int base) const uint QByteArray::toUInt(bool *ok, int base) const { - qulonglong v = toULongLong(ok, base); - if (v > UINT_MAX) { - if (ok) - *ok = false; - v = 0; - } - return uint(v); + return toIntegral_helper<uint>(nulTerminated().constData(), ok, base); } /*! @@ -3422,13 +3428,7 @@ uint QByteArray::toUInt(bool *ok, int base) const */ long QByteArray::toLong(bool *ok, int base) const { - qlonglong v = toLongLong(ok, base); - if (v < LONG_MIN || v > LONG_MAX) { - if (ok) - *ok = false; - v = 0; - } - return long(v); + return toIntegral_helper<long>(nulTerminated().constData(), ok, base); } /*! @@ -3454,13 +3454,7 @@ long QByteArray::toLong(bool *ok, int base) const */ ulong QByteArray::toULong(bool *ok, int base) const { - qulonglong v = toULongLong(ok, base); - if (v > ULONG_MAX) { - if (ok) - *ok = false; - v = 0; - } - return ulong(v); + return toIntegral_helper<ulong>(nulTerminated().constData(), ok, base); } /*! @@ -3485,13 +3479,7 @@ ulong QByteArray::toULong(bool *ok, int base) const short QByteArray::toShort(bool *ok, int base) const { - qlonglong v = toLongLong(ok, base); - if (v < SHRT_MIN || v > SHRT_MAX) { - if (ok) - *ok = false; - v = 0; - } - return short(v); + return toIntegral_helper<short>(nulTerminated().constData(), ok, base); } /*! @@ -3516,13 +3504,7 @@ short QByteArray::toShort(bool *ok, int base) const ushort QByteArray::toUShort(bool *ok, int base) const { - qulonglong v = toULongLong(ok, base); - if (v > USHRT_MAX) { - if (ok) - *ok = false; - v = 0; - } - return ushort(v); + return toIntegral_helper<ushort>(nulTerminated().constData(), ok, base); } @@ -3544,7 +3526,7 @@ ushort QByteArray::toUShort(bool *ok, int base) const double QByteArray::toDouble(bool *ok) const { - return QLocalePrivate::bytearrayToDouble(nulTerminated().constData(), ok); + return QLocaleData::bytearrayToDouble(nulTerminated().constData(), ok); } /*! @@ -3563,7 +3545,7 @@ double QByteArray::toDouble(bool *ok) const float QByteArray::toFloat(bool *ok) const { - return float(toDouble(ok)); + return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); } /*! @@ -3772,22 +3754,22 @@ QByteArray &QByteArray::setNum(qulonglong n, int base) QByteArray &QByteArray::setNum(double n, char f, int prec) { - QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal; + QLocaleData::DoubleForm form = QLocaleData::DFDecimal; uint flags = 0; if (qIsUpper(f)) - flags = QLocalePrivate::CapitalEorX; + flags = QLocaleData::CapitalEorX; f = qToLower(f); switch (f) { case 'f': - form = QLocalePrivate::DFDecimal; + form = QLocaleData::DFDecimal; break; case 'e': - form = QLocalePrivate::DFExponent; + form = QLocaleData::DFExponent; break; case 'g': - form = QLocalePrivate::DFSignificantDigits; + form = QLocaleData::DFSignificantDigits; break; default: #if defined(QT_CHECK_RANGE) @@ -3796,8 +3778,7 @@ QByteArray &QByteArray::setNum(double n, char f, int prec) break; } - QLocale locale(QLocale::C); - *this = locale.d->doubleToString(n, prec, form, -1, flags).toLatin1(); + *this = QLocaleData::c()->doubleToString(n, prec, form, -1, flags).toLatin1(); return *this; } |