diff options
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 46 | ||||
-rw-r--r-- | src/corelib/tools/qbytearray.h | 4 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 19 |
3 files changed, 54 insertions, 15 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index feda8f441d..3685a8938a 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -3585,7 +3585,8 @@ QByteArray QByteArray::toBase64() const Sets the byte array to the printed value of \a n in base \a base (10 by default) and returns a reference to the byte array. The \a base can - be any value between 2 and 36. + be any value between 2 and 36. For bases other than 10, n is treated + as an unsigned integer. Example: \snippet code/src_corelib_tools_qbytearray.cpp 40 @@ -3623,7 +3624,7 @@ QByteArray QByteArray::toBase64() const \sa toLongLong() */ -QByteArray &QByteArray::setNum(qlonglong n, int base) +static char *qulltoa2(char *p, qulonglong n, int base) { #if defined(QT_CHECK_RANGE) if (base < 2 || base > 36) { @@ -3631,8 +3632,31 @@ QByteArray &QByteArray::setNum(qlonglong n, int base) base = 10; } #endif - QLocale locale(QLocale::C); - *this = locale.d->longLongToString(n, -1, base).toLatin1(); + const char b = 'a' - 10; + do { + const int c = n % base; + n /= base; + *--p = c + (c < 10 ? '0' : b); + } while (n); + + return p; +} + +QByteArray &QByteArray::setNum(qlonglong n, int base) +{ + const int buffsize = 66; // big enough for MAX_ULLONG in base 2 + char buff[buffsize]; + char *p; + + if (n < 0 && base == 10) { + p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base); + *--p = '-'; + } else { + p = qulltoa2(buff + buffsize, qulonglong(n), base); + } + + clear(); + append(p, buffsize - (p - buff)); return *this; } @@ -3644,14 +3668,12 @@ QByteArray &QByteArray::setNum(qlonglong n, int base) QByteArray &QByteArray::setNum(qulonglong n, int base) { -#if defined(QT_CHECK_RANGE) - if (base < 2 || base > 36) { - qWarning("QByteArray::setNum: Invalid base %d", base); - base = 10; - } -#endif - QLocale locale(QLocale::C); - *this = locale.d->unsLongLongToString(n, -1, base).toLatin1(); + const int buffsize = 66; // big enough for MAX_ULLONG in base 2 + char buff[buffsize]; + char *p = qulltoa2(buff + buffsize, n, base); + + clear(); + append(p, buffsize - (p - buff)); return *this; } diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 50e52a1ca7..860869e9c3 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -581,11 +581,11 @@ inline QByteArray &QByteArray::replace(const char *before, const char *after) { return replace(before, qstrlen(before), after, qstrlen(after)); } inline QByteArray &QByteArray::setNum(short n, int base) -{ return setNum(qlonglong(n), base); } +{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(ushort(n)), base); } inline QByteArray &QByteArray::setNum(ushort n, int base) { return setNum(qulonglong(n), base); } inline QByteArray &QByteArray::setNum(int n, int base) -{ return setNum(qlonglong(n), base); } +{ return base == 10 ? setNum(qlonglong(n), base) : setNum(qulonglong(uint(n)), base); } inline QByteArray &QByteArray::setNum(uint n, int base) { return setNum(qulonglong(n), base); } inline QByteArray &QByteArray::setNum(float n, char f, int prec) diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index bbfffb2063..1084b5a7b1 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -401,7 +401,24 @@ void tst_QByteArray::rightJustified() void tst_QByteArray::setNum() { QByteArray a; - QCOMPARE(a.setNum(123), QByteArray("123")); + QCOMPARE(a.setNum(-1), QByteArray("-1")); + QCOMPARE(a.setNum(0), QByteArray("0")); + QCOMPARE(a.setNum(0, 2), QByteArray("0")); + QCOMPARE(a.setNum(0, 36), QByteArray("0")); + QCOMPARE(a.setNum(1), QByteArray("1")); + QCOMPARE(a.setNum(35, 36), QByteArray("z")); + QCOMPARE(a.setNum(37, 2), QByteArray("100101")); + QCOMPARE(a.setNum(37, 36), QByteArray("11")); + + // Negative numbers are only properly supported for base 10. + QCOMPARE(a.setNum(short(-1), 16), QByteArray("ffff")); + QCOMPARE(a.setNum(int(-1), 16), QByteArray("ffffffff")); + QCOMPARE(a.setNum(qlonglong(-1), 16), QByteArray("ffffffffffffffff")); + + QCOMPARE(a.setNum(short(-1), 10), QByteArray("-1")); + QCOMPARE(a.setNum(int(-1), 10), QByteArray("-1")); + QCOMPARE(a.setNum(qlonglong(-1), 10), QByteArray("-1")); + QCOMPARE(a.setNum(-123), QByteArray("-123")); QCOMPARE(a.setNum(0x123,16), QByteArray("123")); QCOMPARE(a.setNum((short)123), QByteArray("123")); |