QString/QByteArray: add erase/erase_if

[ChangeLog][QtCore][QString] Added erase() and erase_if()
for consistent container erasure.

[ChangeLog][QtCore][QByteArray] Added erase() and erase_if()
for consistent container erasure.

Change-Id: I23e8565d39044c1f1d756500589c1f2b65e1a88f
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2020-10-16 20:10:11 +02:00
parent 11bde366e8
commit b2ea30dde0
4 changed files with 100 additions and 0 deletions

View File

@ -2066,6 +2066,16 @@ QByteArray &QByteArray::remove(qsizetype pos, qsizetype len)
return *this; return *this;
} }
/*!
\fn template <typename Predicate> QByteArray &QByteArray::removeIf(Predicate pred)
\since 6.1
Removes all bytes for which the predicate \a pred returns true
from the byte array. Returns a reference to the byte array.
\sa remove()
*/
/*! /*!
Replaces \a len bytes from index position \a pos with the byte Replaces \a len bytes from index position \a pos with the byte
array \a after, and returns a reference to this byte array. array \a after, and returns a reference to this byte array.
@ -4750,4 +4760,25 @@ size_t qHash(const QByteArray::FromBase64Result &key, size_t seed) noexcept
return qHashMulti(seed, key.decoded, static_cast<int>(key.decodingStatus)); return qHashMulti(seed, key.decoded, static_cast<int>(key.decodingStatus));
} }
/*! \fn template <typename T> qsizetype erase(QByteArray &ba, const T &t)
\relates QByteArray
\since 6.1
Removes all elements that compare equal to \a t from the
byte array \a ba. Returns the number of elements removed, if any.
\sa erase_if
*/
/*! \fn template <typename Predicate> qsizetype erase_if(QByteArray &ba, Predicate pred)
\relates QByteArray
\since 6.1
Removes all elements for which the predicate \a pred returns true
from the byte array \a ba. Returns the number of elements removed, if
any.
\sa erase
*/
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -309,6 +309,12 @@ public:
{ return insert(i, QByteArrayView(s, len)); } { return insert(i, QByteArrayView(s, len)); }
QByteArray &remove(qsizetype index, qsizetype len); QByteArray &remove(qsizetype index, qsizetype len);
template <typename Predicate>
QByteArray &removeIf(Predicate pred)
{
QtPrivate::sequential_erase_if(*this, pred);
return *this;
}
QByteArray &replace(qsizetype index, qsizetype len, const char *s, qsizetype alen) QByteArray &replace(qsizetype index, qsizetype len, const char *s, qsizetype alen)
{ return replace(index, len, QByteArrayView(s, alen)); } { return replace(index, len, QByteArrayView(s, alen)); }
@ -721,6 +727,18 @@ Q_DECLARE_SHARED(QByteArray::FromBase64Result)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray::FromBase64Result &key, size_t seed = 0) noexcept; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION size_t qHash(const QByteArray::FromBase64Result &key, size_t seed = 0) noexcept;
template <typename T>
qsizetype erase(QByteArray &ba, const T &t)
{
return QtPrivate::sequential_erase(ba, t);
}
template <typename Predicate>
qsizetype erase_if(QByteArray &ba, Predicate pred)
{
return QtPrivate::sequential_erase_if(ba, pred);
}
// //
// QByteArrayView members that require QByteArray: // QByteArrayView members that require QByteArray:
// //

View File

@ -3087,6 +3087,16 @@ QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
\sa indexOf(), lastIndexOf(), replace() \sa indexOf(), lastIndexOf(), replace()
*/ */
/*!
\fn template <typename Predicate> QString &QString::removeIf(Predicate pred)
\since 6.1
Removes all elements for which the predicate \a pred returns true
from the string. Returns a reference to the string.
\sa remove()
*/
/*! /*!
\fn QString &QString::replace(qsizetype position, qsizetype n, const QString &after) \fn QString &QString::replace(qsizetype position, qsizetype n, const QString &after)
@ -10138,4 +10148,27 @@ float QStringView::toFloat(bool *ok) const
return QLocaleData::convertDoubleToFloat(toDouble(ok), ok); return QLocaleData::convertDoubleToFloat(toDouble(ok), ok);
} }
/*!
\fn template <typename T> qsizetype erase(QString &s, const T &t)
\relates QString
\since 6.1
Removes all elements that compare equal to \a t from the
string \a s. Returns the number of elements removed, if any.
\sa erase_if
*/
/*!
\fn template <typename Predicate> qsizetype erase_if(QString &s, Predicate pred)
\relates QString
\since 6.1
Removes all elements for which the predicate \a pred returns true
from the string \a s. Returns the number of elements removed, if
any.
\sa erase
*/
QT_END_NAMESPACE QT_END_NAMESPACE

View File

@ -642,6 +642,12 @@ public:
QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &remove(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
template <typename Predicate>
QString &removeIf(Predicate pred)
{
QtPrivate::sequential_erase_if(*this, pred);
return *this;
}
QString &replace(qsizetype i, qsizetype len, QChar after); QString &replace(qsizetype i, qsizetype len, QChar after);
QString &replace(qsizetype i, qsizetype len, const QChar *s, qsizetype slen); QString &replace(qsizetype i, qsizetype len, const QChar *s, qsizetype slen);
QString &replace(qsizetype i, qsizetype len, const QString &after); QString &replace(qsizetype i, qsizetype len, const QString &after);
@ -1523,6 +1529,18 @@ QString QLatin1String::arg(Args &&...args) const
return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...); return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...);
} }
template <typename T>
qsizetype erase(QString &s, const T &t)
{
return QtPrivate::sequential_erase(s, t);
}
template <typename Predicate>
qsizetype erase_if(QString &s, Predicate pred)
{
return QtPrivate::sequential_erase_if(s, pred);
}
QT_END_NAMESPACE QT_END_NAMESPACE
#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER) #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)