diff options
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 46 | ||||
-rw-r--r-- | src/corelib/tools/qbytearray.h | 25 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp | 15 |
3 files changed, 78 insertions, 8 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 0ed701f4fa..9bd3994cc9 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -919,6 +919,52 @@ static inline char qToLower(char c) \sa constBegin(), end() */ +/*! \fn QByteArray::reverse_iterator QByteArray::rbegin() + \since 5.6 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + character in the byte-array, in reverse order. + + \sa begin(), crbegin(), rend() +*/ + +/*! \fn QByteArray::const_reverse_iterator QByteArray::rbegin() const + \since 5.6 + \overload +*/ + +/*! \fn QByteArray::const_reverse_iterator QByteArray::crbegin() const + \since 5.6 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + character in the byte-array, in reverse order. + + \sa begin(), rbegin(), rend() +*/ + +/*! \fn QByteArray::reverse_iterator QByteArray::rend() + \since 5.6 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past + the last character in the byte-array, in reverse order. + + \sa end(), crend(), rbegin() +*/ + +/*! \fn QByteArray::const_reverse_iterator QByteArray::rend() const + \since 5.6 + \overload +*/ + +/*! \fn QByteArray::const_reverse_iterator QByteArray::crend() const + \since 5.6 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one + past the last character in the byte-array, in reverse order. + + \sa end(), rend(), rbegin() +*/ + /*! \fn void QByteArray::push_back(const QByteArray &other) This function is provided for STL compatibility. It is equivalent diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 7388c4eee5..f0032227e8 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -43,6 +43,7 @@ #include <stdarg.h> #include <string> +#include <iterator> #ifdef truncate #error qbytearray.h must be included before any header file that defines truncate @@ -390,14 +391,22 @@ public: typedef const char *const_iterator; typedef iterator Iterator; typedef const_iterator ConstIterator; - iterator begin(); - const_iterator begin() const; - const_iterator cbegin() const; - const_iterator constBegin() const; - iterator end(); - const_iterator end() const; - const_iterator cend() const; - const_iterator constEnd() const; + typedef std::reverse_iterator<iterator> reverse_iterator; + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + inline iterator begin(); + inline const_iterator begin() const; + inline const_iterator cbegin() const; + inline const_iterator constBegin() const; + inline iterator end(); + inline const_iterator end() const; + inline const_iterator cend() const; + inline const_iterator constEnd() const; + reverse_iterator rbegin() { return reverse_iterator(end()); } + reverse_iterator rend() { return reverse_iterator(begin()); } + const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } + const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } + const_reverse_iterator crbegin() const { return const_reverse_iterator(end()); } + const_reverse_iterator crend() const { return const_reverse_iterator(begin()); } // stl compatibility typedef int size_type; diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 410b34e894..f942eab800 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -67,6 +67,7 @@ private slots: void endsWith_data(); void endsWith(); void endsWith_char(); + void reverseIterators(); void split_data(); void split(); void base64_data(); @@ -540,6 +541,20 @@ void tst_QByteArray::endsWith_char() QVERIFY(!QByteArray().endsWith('\0')); } +void tst_QByteArray::reverseIterators() +{ + QByteArray s = "1234"; + QByteArray sr = s; + std::reverse(sr.begin(), sr.end()); + const QByteArray &csr = sr; + QVERIFY(std::equal(s.begin(), s.end(), sr.rbegin())); + QVERIFY(std::equal(s.begin(), s.end(), sr.crbegin())); + QVERIFY(std::equal(s.begin(), s.end(), csr.rbegin())); + QVERIFY(std::equal(sr.rbegin(), sr.rend(), s.begin())); + QVERIFY(std::equal(sr.crbegin(), sr.crend(), s.begin())); + QVERIFY(std::equal(csr.rbegin(), csr.rend(), s.begin())); +} + void tst_QByteArray::split_data() { QTest::addColumn<QByteArray>("sample"); |