QStringIterator: port to QStringView

Pretty straightforward, as the implementation already used only
an iterator range internally.

Change-Id: I6e6b809329e2e2548bba6db414a3d107d09637d1
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2017-02-04 13:05:06 +01:00 committed by Edward Welbourne
parent b8c61c6bb3
commit d40dcee642
4 changed files with 17 additions and 18 deletions

View File

@ -58,7 +58,7 @@ int main()
{ {
//! [0] //! [0]
QString string(QStringLiteral("a string")); QString string(QStringLiteral("a string"));
QStringIterator i(string); QStringIterator i(string); // implicitly converted to QStringView
//! [0] //! [0]
//! [1] //! [1]
@ -71,8 +71,7 @@ while (i.hasNext())
{ {
//! [2] //! [2]
QString string(QStringLiteral("𝄞 is the G clef")); QStringIterator i(u"𝄞 is the G clef");
QStringIterator i(string);
qDebug() << hex << i.next(); // will print 1d11e (U+1D11E, MUSICAL SYMBOL G CLEF) qDebug() << hex << i.next(); // will print 1d11e (U+1D11E, MUSICAL SYMBOL G CLEF)
qDebug() << hex << i.next(); // will print 20 (U+0020, SPACE) qDebug() << hex << i.next(); // will print 20 (U+0020, SPACE)
qDebug() << hex << i.next(); // will print 69 (U+0069, LATIN SMALL LETTER I) qDebug() << hex << i.next(); // will print 69 (U+0069, LATIN SMALL LETTER I)

View File

@ -55,7 +55,7 @@
that may be present in a QString, and return the individual Unicode code points. that may be present in a QString, and return the individual Unicode code points.
You can create a QStringIterator that iterates over a given You can create a QStringIterator that iterates over a given
QString by passing the string to the QStringIterator's constructor: QStringView by passing the string to the QStringIterator's constructor:
\snippet code/src_corelib_tools_qstringiterator.cpp 0 \snippet code/src_corelib_tools_qstringiterator.cpp 0
@ -120,12 +120,12 @@
*/ */
/*! /*!
\fn QStringIterator::QStringIterator(const QString &string) \fn QStringIterator::QStringIterator(QStringView string, QStringView::size_type idx)
Constructs an iterator over the contents of \a string. The iterator will point Constructs an iterator over the contents of \a string. The iterator will point
before the first position in the string. before position \a idx in the string.
The string \a string must remain valid while the iterator is being used. The string view \a string must remain valid while the iterator is being used.
*/ */
/*! /*!

View File

@ -60,12 +60,12 @@ QT_BEGIN_NAMESPACE
class QStringIterator class QStringIterator
{ {
QString::const_iterator i, pos, e; QString::const_iterator i, pos, e;
Q_STATIC_ASSERT((std::is_same<QString::const_iterator, const QChar *>::value));
public: public:
inline explicit QStringIterator(const QString &string) explicit QStringIterator(QStringView string, QStringView::size_type idx = 0)
: i(string.constBegin()), : i(string.begin()),
pos(string.constBegin()), pos(i + idx),
e(string.constEnd()) e(string.end())
{ {
} }

View File

@ -326,22 +326,22 @@ void tst_QStringIterator::position()
QLatin1Char('p') QLatin1Char('p')
// codeunit count: 35 // codeunit count: 35
}; };
static const int stringDataSize = sizeof(stringData) / sizeof(stringData[0]);
const QString string(stringData, sizeof(stringData) / sizeof(stringData[0])); QStringIterator i(QStringView(stringData, stringDataSize));
QStringIterator i(string);
QCOMPARE(i.position(), string.constBegin()); QCOMPARE(i.position(), stringData);
QVERIFY(i.hasNext()); QVERIFY(i.hasNext());
QVERIFY(!i.hasPrevious()); QVERIFY(!i.hasPrevious());
i.setPosition(string.constEnd()); i.setPosition(stringData + stringDataSize);
QCOMPARE(i.position(), string.constEnd()); QCOMPARE(i.position(), stringData + stringDataSize);
QVERIFY(!i.hasNext()); QVERIFY(!i.hasNext());
QVERIFY(i.hasPrevious()); QVERIFY(i.hasPrevious());
#define QCHAR_UNICODE_VALUE(x) ((uint)(QChar(x).unicode())) #define QCHAR_UNICODE_VALUE(x) ((uint)(QChar(x).unicode()))
const QString::const_iterator begin = string.constBegin(); const QChar *begin = stringData;
i.setPosition(begin); i.setPosition(begin);
QCOMPARE(i.position(), begin); QCOMPARE(i.position(), begin);
QCOMPARE(i.peekNext(), QCHAR_UNICODE_VALUE(QLatin1Char('a'))); QCOMPARE(i.peekNext(), QCHAR_UNICODE_VALUE(QLatin1Char('a')));