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:
parent
b8c61c6bb3
commit
d40dcee642
@ -58,7 +58,7 @@ int main()
|
||||
{
|
||||
//! [0]
|
||||
QString string(QStringLiteral("a string"));
|
||||
QStringIterator i(string);
|
||||
QStringIterator i(string); // implicitly converted to QStringView
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
@ -71,8 +71,7 @@ while (i.hasNext())
|
||||
|
||||
{
|
||||
//! [2]
|
||||
QString string(QStringLiteral("𝄞 is the G clef"));
|
||||
QStringIterator i(string);
|
||||
QStringIterator i(u"𝄞 is the 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 69 (U+0069, LATIN SMALL LETTER I)
|
||||
|
@ -55,7 +55,7 @@
|
||||
that may be present in a QString, and return the individual Unicode code points.
|
||||
|
||||
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
|
||||
|
||||
@ -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
|
||||
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.
|
||||
*/
|
||||
|
||||
/*!
|
||||
|
@ -60,12 +60,12 @@ QT_BEGIN_NAMESPACE
|
||||
class QStringIterator
|
||||
{
|
||||
QString::const_iterator i, pos, e;
|
||||
|
||||
Q_STATIC_ASSERT((std::is_same<QString::const_iterator, const QChar *>::value));
|
||||
public:
|
||||
inline explicit QStringIterator(const QString &string)
|
||||
: i(string.constBegin()),
|
||||
pos(string.constBegin()),
|
||||
e(string.constEnd())
|
||||
explicit QStringIterator(QStringView string, QStringView::size_type idx = 0)
|
||||
: i(string.begin()),
|
||||
pos(i + idx),
|
||||
e(string.end())
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -326,22 +326,22 @@ void tst_QStringIterator::position()
|
||||
QLatin1Char('p')
|
||||
// codeunit count: 35
|
||||
};
|
||||
static const int stringDataSize = sizeof(stringData) / sizeof(stringData[0]);
|
||||
|
||||
const QString string(stringData, sizeof(stringData) / sizeof(stringData[0]));
|
||||
QStringIterator i(string);
|
||||
QStringIterator i(QStringView(stringData, stringDataSize));
|
||||
|
||||
QCOMPARE(i.position(), string.constBegin());
|
||||
QCOMPARE(i.position(), stringData);
|
||||
QVERIFY(i.hasNext());
|
||||
QVERIFY(!i.hasPrevious());
|
||||
|
||||
i.setPosition(string.constEnd());
|
||||
QCOMPARE(i.position(), string.constEnd());
|
||||
i.setPosition(stringData + stringDataSize);
|
||||
QCOMPARE(i.position(), stringData + stringDataSize);
|
||||
QVERIFY(!i.hasNext());
|
||||
QVERIFY(i.hasPrevious());
|
||||
|
||||
#define QCHAR_UNICODE_VALUE(x) ((uint)(QChar(x).unicode()))
|
||||
|
||||
const QString::const_iterator begin = string.constBegin();
|
||||
const QChar *begin = stringData;
|
||||
i.setPosition(begin);
|
||||
QCOMPARE(i.position(), begin);
|
||||
QCOMPARE(i.peekNext(), QCHAR_UNICODE_VALUE(QLatin1Char('a')));
|
||||
|
Loading…
x
Reference in New Issue
Block a user