QString, QByteArray: add erase(iterator) method

Fixes: QTBUG-106182
Change-Id: Idc74cc643b90252838ca1a9ca40a330315da421f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ahmad Samir 2022-10-20 13:25:58 +02:00
parent 2ab347b0dd
commit dcfab7e28e
6 changed files with 91 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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