From dcfab7e28ee9bbdf09138a321e1f8db2b770d4b7 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 20 Oct 2022 13:25:58 +0200 Subject: [PATCH] QString, QByteArray: add erase(iterator) method Fixes: QTBUG-106182 Change-Id: Idc74cc643b90252838ca1a9ca40a330315da421f Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 15 +++++++ src/corelib/text/qbytearray.h | 1 + src/corelib/text/qstring.cpp | 18 ++++++++- src/corelib/text/qstring.h | 1 + .../text/qbytearray/tst_qbytearray.cpp | 17 ++++++++ .../auto/corelib/text/qstring/tst_qstring.cpp | 40 +++++++++++++++++++ 6 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index a696cb8dbb5..55c622b6cb1 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1271,6 +1271,21 @@ QByteArray::iterator QByteArray::erase(QByteArray::const_iterator first, QByteAr return begin() + start; } +/*! + \fn QByteArray::iterator QByteArray::erase(QByteArray::const_iterator it) + + \since 6.5 + + Removes the character denoted by \c it from the byte array. + Returns an iterator to the character immediately after the + erased character. + + \code + QByteArray ba = "abcdefg"; + auto it = ba.erase(ba.cbegin()); // ba is now "bcdefg" and it points to "b" + \endcode +*/ + /*! \fn QByteArray::QByteArray(const QByteArray &other) Constructs a copy of \a other. diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 1079555c84c..17fd0f20509 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -429,6 +429,7 @@ public: { prepend(a); } void shrink_to_fit() { squeeze(); } iterator erase(const_iterator first, const_iterator last); + inline iterator erase(const_iterator it) { return erase(it, it + 1); } static QByteArray fromStdString(const std::string &s); std::string toStdString() const; diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 43b64f399e9..4d7522a2b85 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -8935,7 +8935,8 @@ bool QString::isRightToLeft() const Removes from the string the characters in the half-open range [ \a first , \a last ). Returns an iterator to the character - referred to by \a last before the erase. + immediately after the last erased character (i.e. the character + referred to by \a last before the erase). */ QString::iterator QString::erase(QString::const_iterator first, QString::const_iterator last) { @@ -8945,6 +8946,21 @@ QString::iterator QString::erase(QString::const_iterator first, QString::const_i return begin() + start; } +/*! + \fn QString::iterator QString::erase(QString::const_iterator it) + + \since 6.5 + + Removes the character denoted by \c it from the string. + Returns an iterator to the character immediately after the + erased character. + + \code + QString c = "abcdefg"; + auto it = c.erase(c.cbegin()); // c is now "bcdefg"; "it" points to "b" + \endcode +*/ + /*! \fn void QString::shrink_to_fit() \since 5.10 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index d30bc0b8514..9c67558edd8 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1086,6 +1086,7 @@ public: inline void push_front(const QString &s) { prepend(s); } void shrink_to_fit() { squeeze(); } iterator erase(const_iterator first, const_iterator last); + inline iterator erase(const_iterator it) { return erase(it, it + 1); } static inline QString fromStdString(const std::string &s); inline std::string toStdString() const; diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index bf21cf4c9a3..60bddc20df5 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -68,6 +68,7 @@ private slots: void remove_data(); void remove(); void removeIf(); + void erase_single_arg(); void replace_data(); void replace(); void replaceWithSpecifiedLength(); @@ -1272,6 +1273,22 @@ void tst_QByteArray::removeIf() QCOMPARE(a.removeIf(removeA), QByteArray("BcbC")); } +void tst_QByteArray::erase_single_arg() +{ + QByteArray ba = "abcdefg"; + ba.erase(ba.cend()); + auto it = ba.erase(ba.cbegin()); + QCOMPARE_EQ(ba, "bcdefg"); + QCOMPARE(it, ba.cbegin()); + + it = ba.erase(std::prev(ba.end())); + QCOMPARE_EQ(ba, "bcdef"); + QCOMPARE(it, ba.cend()); + + it = ba.erase(std::find(ba.begin(), ba.end(), QChar('d'))); + QCOMPARE(it, ba.begin() + 2); +} + void tst_QByteArray::replace_data() { // Try to cover both the index and specific char cases. diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 9775a7422f6..80bc6c92082 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -366,6 +366,8 @@ private slots: void remove_regexp(); #endif void remove_extra(); + void erase_single_arg(); + void erase(); void swap(); void prepend_qstring() { prepend_impl(); } @@ -3424,6 +3426,44 @@ void tst_QString::remove_extra() } } +void tst_QString::erase_single_arg() +{ + QString s = "abcdefg"; + auto it = s.erase(s.cbegin()); + QCOMPARE_EQ(s, "bcdefg"); + QCOMPARE(it, s.cbegin()); + + it = s.erase(std::prev(s.end())); + QCOMPARE_EQ(s, "bcdef"); + QCOMPARE(it, s.cend()); + + it = s.erase(std::find(s.begin(), s.end(), QChar('d'))); + QCOMPARE(it, s.begin() + 2); +} + +void tst_QString::erase() +{ + QString str = "abcdefg"; + + QString s = str; + auto it = s.erase(s.begin(), s.end()); + QCOMPARE_EQ(s, ""); + QCOMPARE(it, s.end()); + + s = str; + it = s.erase(std::prev(s.end())); + QCOMPARE_EQ(s, "abcdef"); + QCOMPARE(it, s.end()); + + it = s.erase(s.begin() + 2, s.end()); + QCOMPARE_EQ(s, "ab"); + QCOMPARE(it, s.end()); + + it = s.erase(s.begin(), s.begin() + 1); + QCOMPARE_EQ(s, "b"); + QCOMPARE(it, s.begin()); +} + void tst_QString::toNum() { #if defined (Q_OS_WIN) && defined (Q_CC_MSVC)