From 05c38a9111320da311e5993faf12ce8e1f1713e8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 23 Jan 2015 12:03:36 +0100 Subject: QtCore: assorted migrations to QString::asprintf Change-Id: Ie99d3eeeced89dd8336717954fd5ca7117bb20b4 Reviewed-by: Thiago Macieira --- src/corelib/tools/qhash.cpp | 5 ++--- src/corelib/tools/qvsnprintf.cpp | 5 +---- .../corelib/global/qgetputenv/tst_qgetputenv.cpp | 8 ++++---- .../corelib/kernel/qmetaobject/tst_qmetaobject.cpp | 2 +- .../qstatemachine/tst_qstatemachine.cpp | 22 +++++++++++----------- .../corelib/tools/qalgorithms/tst_qalgorithms.cpp | 8 ++++---- tests/auto/corelib/tools/qchar/tst_qchar.cpp | 2 +- tests/auto/corelib/tools/qregexp/tst_qregexp.cpp | 3 +-- 8 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp index 1b1d9af187..a8a461e868 100644 --- a/src/corelib/tools/qhash.cpp +++ b/src/corelib/tools/qhash.cpp @@ -617,12 +617,11 @@ void QHashData::dump() numBuckets); qDebug(" %p (fakeNode = %p)", this, fakeNext); for (int i = 0; i < numBuckets; ++i) { - QString line; Node *n = buckets[i]; if (n != reinterpret_cast(this)) { - line.sprintf("%d:", i); + QString line = QString::asprintf("%d:", i); while (n != reinterpret_cast(this)) { - line += QString().sprintf(" -> [%p]", n); + line += QString::asprintf(" -> [%p]", n); if (!n) { line += " (CORRUPT)"; break; diff --git a/src/corelib/tools/qvsnprintf.cpp b/src/corelib/tools/qvsnprintf.cpp index e9590c3971..6f18d21d2b 100644 --- a/src/corelib/tools/qvsnprintf.cpp +++ b/src/corelib/tools/qvsnprintf.cpp @@ -69,10 +69,7 @@ int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap) if (!str || !fmt) return -1; - QString buf; - buf.vsprintf(fmt, ap); - - QByteArray ba = buf.toLocal8Bit(); + const QByteArray ba = QString::vasprintf(fmt, ap).toLocal8Bit(); if (n > 0) { size_t blen = qMin(size_t(ba.length()), size_t(n - 1)); diff --git a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp index 4b03c9a56d..71a3419963 100644 --- a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp +++ b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp @@ -117,13 +117,13 @@ void tst_QGetPutEnv::intValue_data() ROW(0xffffffff, 0, false); const int bases[] = {10, 8, 16}; for (size_t i = 0; i < sizeof bases / sizeof *bases; ++i) { - QTest::newRow(qPrintable(QString().sprintf("INT_MAX, base %d", bases[i]))) + QTest::newRow(qPrintable(QString::asprintf("INT_MAX, base %d", bases[i]))) << QByteArray::number(INT_MAX) << INT_MAX << true; - QTest::newRow(qPrintable(QString().sprintf("INT_MAX+1, base %d", bases[i]))) + QTest::newRow(qPrintable(QString::asprintf("INT_MAX+1, base %d", bases[i]))) << QByteArray::number(qlonglong(INT_MAX) + 1) << 0 << false; - QTest::newRow(qPrintable(QString().sprintf("INT_MIN, base %d", bases[i]))) + QTest::newRow(qPrintable(QString::asprintf("INT_MIN, base %d", bases[i]))) << QByteArray::number(INT_MIN) << INT_MIN << true; - QTest::newRow(qPrintable(QString().sprintf("INT_MIN-1, base %d", bases[i]))) + QTest::newRow(qPrintable(QString::asprintf("INT_MIN-1, base %d", bases[i]))) << QByteArray::number(qlonglong(INT_MIN) - 1) << 0 << false; }; } diff --git a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp index 4fb508a65c..42dee6f1f8 100644 --- a/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp +++ b/tests/auto/corelib/kernel/qmetaobject/tst_qmetaobject.cpp @@ -469,7 +469,7 @@ void QtTestObject::testLongLong(qint64 ll1, quint64 ll2) void QtTestObject::testSender() { - slotResult.sprintf("%p", sender()); + slotResult = QString::asprintf("%p", sender()); } void QtTestObject::slotWithUnregisteredParameterType(MyUnregisteredType) diff --git a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp index 39e333dcc8..1e195b364a 100644 --- a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp @@ -1198,9 +1198,9 @@ void tst_QStateMachine::addAndRemoveState() { QStateMachine machine2; { - QString warning; - warning.sprintf("QStateMachine::removeState: state %p's machine (%p) is different from this machine (%p)", - &machine2, (void*)0, &machine); + const QString warning + = QString::asprintf("QStateMachine::removeState: state %p's machine (%p) is different from this machine (%p)", + &machine2, (void*)0, &machine); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); machine.removeState(&machine2); } @@ -1261,8 +1261,8 @@ void tst_QStateMachine::stateEntryAndExit() QCOMPARE(trans->sourceState(), (QState*)s2); QCOMPARE(trans->targetState(), (QAbstractState*)s3); { - QString warning; - warning.sprintf("QState::removeTransition: transition %p's source state (%p) is different from this state (%p)", trans, s2, s1); + const QString warning + = QString::asprintf("QState::removeTransition: transition %p's source state (%p) is different from this state (%p)", trans, s2, s1); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); s1->removeTransition(trans); } @@ -1286,8 +1286,8 @@ void tst_QStateMachine::stateEntryAndExit() machine.setInitialState(s1); QCOMPARE(machine.initialState(), (QAbstractState*)s1); { - QString warning; - warning.sprintf("QState::setInitialState: state %p is not a child of this state (%p)", &machine, &machine); + QString warning + = QString::asprintf("QState::setInitialState: state %p is not a child of this state (%p)", &machine, &machine); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); machine.setInitialState(&machine); QCOMPARE(machine.initialState(), (QAbstractState*)s1); @@ -2013,8 +2013,8 @@ void tst_QStateMachine::parallelStates() s1_2_1->addTransition(s1_2_f); s1_2->setInitialState(s1_2_1); { - QString warning; - warning.sprintf("QState::setInitialState: ignoring attempt to set initial state of parallel state group %p", s1); + const QString warning + = QString::asprintf("QState::setInitialState: ignoring attempt to set initial state of parallel state group %p", s1); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); s1->setInitialState(0); } @@ -3055,8 +3055,8 @@ void tst_QStateMachine::historyStates() QCOMPARE(s0h->defaultState(), (QAbstractState*)0); s0h->setDefaultState(s00); QCOMPARE(s0h->defaultState(), (QAbstractState*)s00); - QString warning; - warning.sprintf("QHistoryState::setDefaultState: state %p does not belong to this history state's group (%p)", s0, s0); + const QString warning + = QString::asprintf("QHistoryState::setDefaultState: state %p does not belong to this history state's group (%p)", s0, s0); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning)); s0h->setDefaultState(s0); QState *s1 = new QState(root); diff --git a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp index 0a06129ab1..9bce948140 100644 --- a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -1054,22 +1054,22 @@ void tst_QAlgorithms::popCount_data_impl(size_t sizeof_T_Int) const uint bits = bitsSetInByte(byte); const quint64 value = static_cast(byte); const quint64 input = value << ((i % sizeof_T_Int) * 8U); - newRow(qPrintable(QString().sprintf("0x%016llx", input))) << input << bits; + newRow(qPrintable(QString::asprintf("0x%016llx", input))) << input << bits; } // and some random ones: if (sizeof_T_Int >= 8) for (size_t i = 0; i < 1000; ++i) { const quint64 input = quint64(qrand()) << 32 | quint32(qrand()); - newRow(qPrintable(QString().sprintf("0x%016llx", input))) << input << bitsSetInInt64(input); + newRow(qPrintable(QString::asprintf("0x%016llx", input))) << input << bitsSetInInt64(input); } else if (sizeof_T_Int >= 2) for (size_t i = 0; i < 1000 ; ++i) { const quint32 input = qrand(); if (sizeof_T_Int >= 4) - newRow(qPrintable(QString().sprintf("0x%08x", input))) << quint64(input) << bitsSetInInt(input); + newRow(qPrintable(QString::asprintf("0x%08x", input))) << quint64(input) << bitsSetInInt(input); else - newRow(qPrintable(QString().sprintf("0x%04x", quint16(input & 0xFFFF)))) << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); + newRow(qPrintable(QString::asprintf("0x%04x", quint16(input & 0xFFFF)))) << quint64(input & 0xFFFF) << bitsSetInShort(input & 0xFFFF); } } diff --git a/tests/auto/corelib/tools/qchar/tst_qchar.cpp b/tests/auto/corelib/tools/qchar/tst_qchar.cpp index e729338819..1ca7a213ad 100644 --- a/tests/auto/corelib/tools/qchar/tst_qchar.cpp +++ b/tests/auto/corelib/tools/qchar/tst_qchar.cpp @@ -108,7 +108,7 @@ void tst_QChar::operators_data() for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) - QTest::newRow(qPrintable(QString().sprintf("'\\%d' (op) '\\%d'", i, j))) + QTest::newRow(qPrintable(QString::asprintf("'\\%d' (op) '\\%d'", i, j))) << QChar(ushort(i)) << QChar(ushort(j)); } } diff --git a/tests/auto/corelib/tools/qregexp/tst_qregexp.cpp b/tests/auto/corelib/tools/qregexp/tst_qregexp.cpp index a9f9ca363d..fefdec7496 100644 --- a/tests/auto/corelib/tools/qregexp/tst_qregexp.cpp +++ b/tests/auto/corelib/tools/qregexp/tst_qregexp.cpp @@ -316,8 +316,7 @@ void tst_QRegExp::indexIn_addMoreRows(const QByteArray &stri) while ( squareRegExp[ii] != 0 ) { for ( int j = 0; j < 100; j++ ) { - QString name; - name.sprintf( "square%.1d%.2d", ii, j ); + const QString name = QString::asprintf( "square%.1d%.2d", ii, j ); QString target = ""; target.fill( 'a', j ); -- cgit v1.2.3