From 69415638ae488b8dd045b85db5a75eb80610c64b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 6 Mar 2017 23:06:20 +0100 Subject: [PATCH] QLatin1String: add iterators [ChangeLog][QtCore][QLatin1String] Added iterators, {c,}{r,}{begin,end}(). Change-Id: I0222a3ff44f2aa827c737e2fa3cfbb8aad69b5a7 Reviewed-by: Edward Welbourne Reviewed-by: Anton Kudryavtsev Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 161 ++++++++++++++++++ src/corelib/tools/qstring.h | 21 +++ .../tools/qlatin1string/tst_qlatin1string.cpp | 17 ++ 3 files changed, 199 insertions(+) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 13555042f8..ab9d3dbb3a 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8313,6 +8313,76 @@ QString &QString::setRawData(const QChar *unicode, int size) \sa QString, QLatin1Char, {QStringLiteral()}{QStringLiteral} */ +/*! + \typedef QLatin1String::value_type + \since 5.10 + + Alias for \c{const char}. Provided for compatibility with the STL. +*/ + +/*! + \typedef QLatin1String::difference_type + \since 5.10 + + Alias for \c{int}. Provided for compatibility with the STL. +*/ + +/*! + \typedef QLatin1String::size_type + \since 5.10 + + Alias for \c{int}. Provided for compatibility with the STL. +*/ + +/*! + \typedef QLatin1String::reference + \since 5.10 + + Alias for \c{value_type &}. Provided for compatibility with the STL. +*/ + +/*! + \typedef QLatin1String::iterator + \since 5.10 + + This typedef provides an STL-style const iterator for QLatin1String. + + QLatin1String does not support mutable iterators, so this is the same + as const_iterator. + + \sa const_iterator, reverse_iterator +*/ + +/*! + \typedef QLatin1String::const_iterator + \since 5.10 + + This typedef provides an STL-style const iterator for QLatin1String. + + \sa iterator, const_reverse_iterator +*/ + +/*! + \typedef QLatin1String::reverse_iterator + \since 5.10 + + This typedef provides an STL-style const reverse iterator for QLatin1String. + + QLatin1String does not support mutable reverse iterators, so this is the + same as const_reverse_iterator. + + \sa const_reverse_iterator, iterator +*/ + +/*! + \typedef QLatin1String::const_reverse_iterator + \since 5.10 + + This typedef provides an STL-style const reverse iterator for QLatin1String. + + \sa reverse_iterator, const_iterator +*/ + /*! \fn QLatin1String::QLatin1String() \since 5.6 @@ -8407,6 +8477,97 @@ QString &QString::setRawData(const QChar *unicode, int size) \sa at() */ +/*! + \fn QLatin1String::const_iterator QLatin1String::begin() const + \since 5.10 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character in + the string. + + This function is provided for STL compatibility. + + \sa end(), cbegin(), rbegin(), data() +*/ + +/*! + \fn QLatin1String::const_iterator QLatin1String::cbegin() const + \since 5.10 + + Same as begin(). + + This function is provided for STL compatibility. + + \sa cend(), begin(), crbegin(), data() +*/ + +/*! + \fn QLatin1String::const_iterator QLatin1String::end() const + \since 5.10 + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + character after the last character in the list. + + This function is provided for STL compatibility. + + \sa begin(), cend(), rend() +*/ + +/*! \fn QLatin1String::const_iterator QLatin1String::cend() const + \since 5.10 + + Same as end(). + + This function is provided for STL compatibility. + + \sa cbegin(), end(), crend() +*/ + +/*! + \fn QLatin1String::const_reverse_iterator QLatin1String::rbegin() const + \since 5.10 + + Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first + character in the string, in reverse order. + + This function is provided for STL compatibility. + + \sa rend(), crbegin(), begin() +*/ + +/*! + \fn QLatin1String::const_reverse_iterator QLatin1String::crbegin() const + \since 5.10 + + Same as rbegin(). + + This function is provided for STL compatibility. + + \sa crend(), rbegin(), cbegin() +*/ + +/*! + \fn QLatin1String::const_reverse_iterator QLatin1String::rend() const + \since 5.10 + + Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past + the last character in the string, in reverse order. + + This function is provided for STL compatibility. + + \sa rbegin(), crend(), end() +*/ + +/*! + \fn QLatin1String::const_reverse_iterator QLatin1String::crend() const + \since 5.10 + + Same as rend(). + + This function is provided for STL compatibility. + + \sa crbegin(), rend(), cend() +*/ + /*! \fn QLatin1String QLatin1String::mid(int start) const \since 5.8 diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 36edcc7242..37f32c8c43 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -103,6 +103,27 @@ public: Q_DECL_CONSTEXPR QLatin1Char at(int i) const { return QLatin1Char(m_data[i]); } Q_DECL_CONSTEXPR QLatin1Char operator[](int i) const { return at(i); } + using value_type = const char; + using reference = value_type&; + using const_reference = reference; + using iterator = value_type*; + using const_iterator = iterator; + using difference_type = int; // violates Container concept requirements + using size_type = int; // violates Container concept requirements + + Q_DECL_CONSTEXPR const_iterator begin() const Q_DECL_NOTHROW { return data(); } + Q_DECL_CONSTEXPR const_iterator cbegin() const Q_DECL_NOTHROW { return data(); } + Q_DECL_CONSTEXPR const_iterator end() const Q_DECL_NOTHROW { return data() + size(); } + Q_DECL_CONSTEXPR const_iterator cend() const Q_DECL_NOTHROW { return data() + size(); } + + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = reverse_iterator; + + const_reverse_iterator rbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); } + const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); } + const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); } + const_reverse_iterator crend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); } + Q_DECL_CONSTEXPR QLatin1String mid(int pos) const { return QLatin1String(m_data + pos, m_size - pos); } Q_DECL_CONSTEXPR QLatin1String mid(int pos, int n) const diff --git a/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp b/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp index a68671d899..c8373b6ae9 100644 --- a/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp +++ b/tests/auto/corelib/tools/qlatin1string/tst_qlatin1string.cpp @@ -48,6 +48,7 @@ private Q_SLOTS: void midLeftRight(); void nullString(); void emptyString(); + void iterators(); void relationalOperators_data(); void relationalOperators(); }; @@ -155,6 +156,22 @@ void tst_QLatin1String::emptyString() } } +void tst_QLatin1String::iterators() +{ + QLatin1String hello("hello"); + QLatin1String olleh("olleh"); + + QVERIFY(std::equal(hello.begin(), hello.end(), + olleh.rbegin())); + QVERIFY(std::equal(hello.rbegin(), hello.rend(), + QT_MAKE_CHECKED_ARRAY_ITERATOR(olleh.begin(), olleh.size()))); + + QVERIFY(std::equal(hello.cbegin(), hello.cend(), + olleh.rbegin())); + QVERIFY(std::equal(hello.crbegin(), hello.crend(), + QT_MAKE_CHECKED_ARRAY_ITERATOR(olleh.begin(), olleh.size()))); +} + void tst_QLatin1String::relationalOperators_data() { QTest::addColumn("lhs");