From 8f166ccf4081e624a8d23b21f236151c9dc38f28 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 21 Oct 2015 22:20:13 +0200 Subject: [PATCH] QString: add resize(int, QChar) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will be used in QTextStream to speed up padding processing. [ChangeLog][QtCore][QString] Added resize(int, QChar) overload. Change-Id: Id51f8cdacb167310157100b05cacf20e9a5d2716 Reviewed-by: Topi Reiniƶ Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/doc/snippets/qstring/main.cpp | 2 +- src/corelib/tools/qstring.cpp | 23 ++++++++++++++++--- src/corelib/tools/qstring.h | 1 + .../corelib/tools/qstring/tst_qstring.cpp | 17 ++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/corelib/doc/snippets/qstring/main.cpp b/src/corelib/doc/snippets/qstring/main.cpp index 07ff9301bf9..c68d1859164 100644 --- a/src/corelib/doc/snippets/qstring/main.cpp +++ b/src/corelib/doc/snippets/qstring/main.cpp @@ -625,7 +625,7 @@ void Widget::resizeFunction() //! [46] QString t = "Hello"; - t += QString(10, 'X'); + r.resize(t.size() + 10, 'X'); // t == "HelloXXXXXXXXXX" //! [46] diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 2f340477fc0..4ffad7031a9 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -1652,9 +1652,7 @@ QString::QString(QChar ch) \snippet qstring/main.cpp 45 If you want to append a certain number of identical characters to - the string, use \l operator+=() as follows rather than resize(): - - \snippet qstring/main.cpp 46 + the string, use the \l {QString::}{resize(int, QChar)} overload. If you want to expand the string so that it reaches a certain width and fill the new positions with a particular character, use @@ -1694,6 +1692,25 @@ void QString::resize(int size) } } +/*! + \overload + \since 5.7 + + Unlike \l {QString::}{resize(int)}, this overload + initializes the new characters to \a fillChar: + + \snippet qstring/main.cpp 46 +*/ + +void QString::resize(int size, QChar fillChar) +{ + const int oldSize = length(); + resize(size); + const int difference = length() - oldSize; + if (difference > 0) + std::fill_n(d->begin() + oldSize, difference, fillChar.unicode()); +} + /*! \fn int QString::capacity() const Returns the maximum number of characters that can be stored in diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 75f94d7f939..9dc770d2c55 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -233,6 +233,7 @@ public: inline int length() const; inline bool isEmpty() const; void resize(int size); + void resize(int size, QChar fillChar); QString &fill(QChar c, int size = -1); void truncate(int pos); diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index f8a4d8a00a7..3d5167d5747 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -556,6 +556,7 @@ private slots: void nanAndInf(); void compare_data(); void compare(); + void resize(); void resizeAfterFromRawData(); void resizeAfterReserve(); void resizeWithNegative() const; @@ -6013,6 +6014,22 @@ void tst_QString::compare() } } +void tst_QString::resize() +{ + QString s = QLatin1String("hello world"); + + s.resize(5); + QCOMPARE(s, QLatin1String("hello")); + s.resize(8); + QCOMPARE(s.size(), 8); + QVERIFY(s.startsWith(QLatin1String("hello"))); + + s.resize(10, QLatin1Char('n')); + QCOMPARE(s.size(), 10); + QVERIFY(s.startsWith(QLatin1String("hello"))); + QCOMPARE(s.right(2), QLatin1String("nn")); +} + void tst_QString::resizeAfterFromRawData() { QString buffer("hello world");