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 <lars.knoll@theqtcompany.com>
bb10
Marc Mutz 2016-04-06 22:38:58 +02:00
parent ee0951d69b
commit 6958e189a3
3 changed files with 134 additions and 36 deletions

View File

@ -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
*/
/*!
@ -8882,42 +8895,88 @@ ownership of it, no memory is freed when instances are destroyed.
Same as unicode().
*/
/*!
\fn const QChar *QStringRef::begin() const
\since 5.4
Same as unicode().
*/
/*!
\fn const QChar *QStringRef::cbegin() const
\since 5.4
Same as unicode().
*/
/*!
\fn const QChar *QStringRef::end() 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()}.)
*/
/*!
\fn const QChar *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()}.)
*/
/*!
\fn const QChar *QStringRef::constData() const
Same as unicode().
*/
/*!
\fn QStringRef::const_iterator QStringRef::begin() const
\since 5.4
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in
the string.
\sa cbegin(), end(), rbegin(), rend()
*/
/*!
\fn QStringRef::const_iterator QStringRef::cbegin() const
\since 5.4
Same as begin().
\sa begin(), cend(), rbegin(), rend()
*/
/*!
\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
Same as end().
\sa end(), cbegin(), rbegin(), rend()
*/
/*!
\fn QStringRef::const_reverse_iterator QStringRef::rbegin() const
\since 5.7
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()
*/
/*!
Returns a copy of the string reference as a QString object.

View File

@ -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_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

View File

@ -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<QString>("haystack");