From 6958e189a3743aae63314acfdc706e2c3283a99b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 6 Apr 2016 22:38:58 +0200 Subject: QStringRef: add missing {c,}r{begin,end}() Most containers have them in Qt 5.7, so add them to QStringRef, too. Brush up the docs, use the const_iterator typedef in the API, for consistency with QString's docs. [ChangeLog][QtCore][QStringRef] Added reverse iterators, rbegin(), rend(), crbegin(), crend(). Change-Id: I3d2884a1b2faae02c610ab3871552b65bc6e2521 Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.cpp | 85 ++++++++++++++++++---- src/corelib/tools/qstring.h | 16 ++-- .../corelib/tools/qstringref/tst_qstringref.cpp | 33 +++++++++ 3 files changed, 116 insertions(+), 18 deletions(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index c7cd1a7751..5cbad2554c 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8759,7 +8759,20 @@ QDataStream &operator>>(QDataStream &in, QString &str) /*! \typedef QStringRef::const_iterator - \internal + \since 5.4 + + This typedef provides an STL-style const iterator for QStringRef. + + \sa QStringRef::const_reverse_iterator +*/ + +/*! + \typedef QStringRef::const_reverse_iterator + \since 5.7 + + This typedef provides an STL-style const reverse iterator for QStringRef. + + \sa QStringRef::const_iterator */ /*! @@ -8883,39 +8896,85 @@ ownership of it, no memory is freed when instances are destroyed. */ /*! - \fn const QChar *QStringRef::begin() const - \since 5.4 + \fn const QChar *QStringRef::constData() const Same as unicode(). */ /*! - \fn const QChar *QStringRef::cbegin() const + \fn QStringRef::const_iterator QStringRef::begin() const \since 5.4 - Same as unicode(). + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in + the string. + + \sa cbegin(), end(), rbegin(), rend() */ /*! - \fn const QChar *QStringRef::end() const + \fn QStringRef::const_iterator QStringRef::cbegin() const \since 5.4 - Returns a pointer to one character past the last one in this string. - (It is the same as \c {unicode() + size()}.) + Same as begin(). + + \sa begin(), cend(), rbegin(), rend() */ /*! - \fn const QChar *QStringRef::cend() const + \fn QStringRef::const_iterator QStringRef::end() const + \since 5.4 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + character after the last character in the list. + + \sa cbegin(), end(), rbegin(), rend() +*/ + +/*! \fn QStringRef::const_iterator QStringRef::cend() const \since 5.4 - Returns a pointer to one character past the last one in this string. - (It is the same as \c {unicode() + size()}.) + Same as end(). + + \sa end(), cbegin(), rbegin(), rend() */ /*! - \fn const QChar *QStringRef::constData() const + \fn QStringRef::const_reverse_iterator QStringRef::rbegin() const + \since 5.7 - Same as unicode(). + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + character in the string, in reverse order. + + \sa begin(), crbegin(), rend() +*/ + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::crbegin() const + \since 5.7 + + Same as rbegin(). + + \sa begin(), rbegin(), rend() +*/ + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::rend() const + \since 5.7 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past + the last character in the string, in reverse order. + + \sa end(), crend(), rbegin() +*/ + + +/*! + \fn QStringRef::const_reverse_iterator QStringRef::crend() const + \since 5.7 + + Same as rend(). + + \sa end(), rend(), rbegin() */ /*! diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 6676efab41..793a859228 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1360,7 +1360,8 @@ class Q_CORE_EXPORT QStringRef { public: typedef QString::size_type size_type; typedef QString::value_type value_type; - typedef QString::const_iterator const_iterator; + typedef const QChar *const_iterator; + typedef std::reverse_iterator const_reverse_iterator; typedef QString::const_pointer const_pointer; typedef QString::const_reference const_reference; @@ -1439,10 +1440,15 @@ public: } inline const QChar *data() const { return unicode(); } inline const QChar *constData() const { return unicode(); } - inline const QChar *begin() const { return unicode(); } - inline const QChar *cbegin() const { return unicode(); } - inline const QChar *end() const { return unicode() + size(); } - inline const QChar *cend() const { return unicode() + size(); } + + inline const_iterator begin() const { return unicode(); } + inline const_iterator cbegin() const { return unicode(); } + inline const_iterator end() const { return unicode() + size(); } + inline const_iterator cend() const { return unicode() + size(); } + inline const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + inline const_reverse_iterator crbegin() const { return rbegin(); } + inline const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } + inline const_reverse_iterator crend() const { return rend(); } #if QT_DEPRECATED_SINCE(5, 0) QT_DEPRECATED QByteArray toAscii() const Q_REQUIRED_RESULT diff --git a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp index b98c2fb4de..25b97ceaa8 100644 --- a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp +++ b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp @@ -52,6 +52,7 @@ private slots: void indexOf(); void indexOf2_data(); void indexOf2(); + void iteration(); void length_data(); void length(); void isEmpty(); @@ -446,6 +447,38 @@ void tst_QStringRef::indexOf2() } } +void tst_QStringRef::iteration() +{ + QString hello = "Hello"; + QString olleh = "olleH"; + + QStringRef ref(&hello); + QStringRef rref(&olleh); + + const QStringRef &cref = ref; + const QStringRef &crref = rref; + + QVERIFY(std::equal( ref.begin(), ref.end(), hello.begin())); + QVERIFY(std::equal( rref.begin(), rref.end(), olleh.begin())); + QVERIFY(std::equal( cref.begin(), cref.end(), hello.begin())); + QVERIFY(std::equal(crref.begin(), crref.end(), olleh.begin())); + + QVERIFY(std::equal( ref.cbegin(), ref.cend(), hello.begin())); + QVERIFY(std::equal( rref.cbegin(), rref.cend(), olleh.begin())); + QVERIFY(std::equal( cref.cbegin(), cref.cend(), hello.begin())); + QVERIFY(std::equal(crref.cbegin(), crref.cend(), olleh.begin())); + + QVERIFY(std::equal( ref.rbegin(), ref.rend(), hello.rbegin())); + QVERIFY(std::equal( rref.rbegin(), rref.rend(), olleh.rbegin())); + QVERIFY(std::equal( cref.rbegin(), cref.rend(), hello.rbegin())); + QVERIFY(std::equal(crref.rbegin(), crref.rend(), olleh.rbegin())); + + QVERIFY(std::equal( ref.crbegin(), ref.crend(), hello.rbegin())); + QVERIFY(std::equal( rref.crbegin(), rref.crend(), olleh.rbegin())); + QVERIFY(std::equal( cref.crbegin(), cref.crend(), hello.rbegin())); + QVERIFY(std::equal(crref.crbegin(), crref.crend(), olleh.rbegin())); +} + void tst_QStringRef::lastIndexOf_data() { QTest::addColumn("haystack"); -- cgit v1.2.3