QString, QByteArray: add removeAt/First/Last() convenience methods
Requested in codereview.qt-project.org/c/qt/qtbase/+/441770 [ChangeLog][QtCore][Text] Add removeAt/First/Last() convenience methods to QString and QByteArray Change-Id: I48a803e456e70cc51d51726a5e3aa7c125aedb1c Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
4c975fd564
commit
4d84843822
@ -2270,6 +2270,38 @@ QByteArray &QByteArray::remove(qsizetype pos, qsizetype len)
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn QByteArray &QByteArray::removeAt(qsizetype pos)
|
||||
|
||||
\since 6.5
|
||||
|
||||
Removes the character at index \a pos. If \a pos is out of bounds
|
||||
(i.e. \a pos >= size()) this function does nothing.
|
||||
|
||||
\sa remove()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QByteArray &QByteArray::removeFirst()
|
||||
|
||||
\since 6.5
|
||||
|
||||
Removes the first character in this byte array. If the byte array is empty,
|
||||
this function does nothing.
|
||||
|
||||
\sa remove()
|
||||
*/
|
||||
/*!
|
||||
\fn QByteArray &QByteArray::removeLast()
|
||||
|
||||
\since 6.5
|
||||
|
||||
Removes the last character in this byte array. If the byte array is empty,
|
||||
this function does nothing.
|
||||
|
||||
\sa remove()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn template <typename Predicate> QByteArray &QByteArray::removeIf(Predicate pred)
|
||||
\since 6.1
|
||||
|
@ -233,6 +233,11 @@ public:
|
||||
{ return insert(i, QByteArrayView(s, len)); }
|
||||
|
||||
QByteArray &remove(qsizetype index, qsizetype len);
|
||||
QByteArray &removeAt(qsizetype pos)
|
||||
{ return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; }
|
||||
QByteArray &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; }
|
||||
QByteArray &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; }
|
||||
|
||||
template <typename Predicate>
|
||||
QByteArray &removeIf(Predicate pred)
|
||||
{
|
||||
|
@ -3385,6 +3385,39 @@ QString &QString::remove(QLatin1StringView str, Qt::CaseSensitivity cs)
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn QString &QString::removeAt(qsizetype pos)
|
||||
|
||||
\since 6.5
|
||||
|
||||
Removes the character at index \a pos. If \a pos is out of bounds
|
||||
(i.e. \a pos >= size()), this function does nothing.
|
||||
|
||||
\sa remove()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QString &QString::removeFirst()
|
||||
|
||||
\since 6.5
|
||||
|
||||
Removes the first character in this string. If the string is empty,
|
||||
this function does nothing.
|
||||
|
||||
\sa remove()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QString &QString::removeLast()
|
||||
|
||||
\since 6.5
|
||||
|
||||
Removes the last character in this string. If the string is empty,
|
||||
this function does nothing.
|
||||
|
||||
\sa remove()
|
||||
*/
|
||||
|
||||
/*!
|
||||
Removes every occurrence of the character \a ch in this string, and
|
||||
returns a reference to this string.
|
||||
|
@ -704,6 +704,12 @@ public:
|
||||
QString &remove(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||
QString &remove(QLatin1StringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||
QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
||||
|
||||
QString &removeAt(qsizetype pos)
|
||||
{ return size_t(pos) < size_t(size()) ? remove(pos, 1) : *this; }
|
||||
QString &removeFirst() { return !isEmpty() ? remove(0, 1) : *this; }
|
||||
QString &removeLast() { return !isEmpty() ? remove(size() - 1, 1) : *this; }
|
||||
|
||||
template <typename Predicate>
|
||||
QString &removeIf(Predicate pred)
|
||||
{
|
||||
|
@ -53,6 +53,7 @@ private slots:
|
||||
void insertExtended();
|
||||
void remove_data();
|
||||
void remove();
|
||||
void remove_extra();
|
||||
void removeIf();
|
||||
void erase();
|
||||
void erase_single_arg();
|
||||
@ -1070,6 +1071,22 @@ void tst_QByteArray::remove()
|
||||
QCOMPARE(ba2.remove(position, length), expected);
|
||||
}
|
||||
|
||||
void tst_QByteArray::remove_extra()
|
||||
{
|
||||
QByteArray ba = "Clock";
|
||||
ba.removeFirst();
|
||||
QCOMPARE(ba, "lock");
|
||||
ba.removeLast();
|
||||
QCOMPARE(ba, "loc");
|
||||
ba.removeAt(ba.indexOf('o'));
|
||||
QCOMPARE(ba, "lc");
|
||||
ba.clear();
|
||||
// No crash on empty byte arrays
|
||||
ba.removeFirst();
|
||||
ba.removeLast();
|
||||
ba.removeAt(2);
|
||||
}
|
||||
|
||||
void tst_QByteArray::removeIf()
|
||||
{
|
||||
auto removeA = [](const char c) { return c == 'a' || c == 'A'; };
|
||||
|
@ -3452,6 +3452,21 @@ void tst_QString::remove_extra()
|
||||
s1.remove(0, 1);
|
||||
QCOMPARE(s1, s);
|
||||
}
|
||||
|
||||
{
|
||||
QString s = "Clock";
|
||||
s.removeFirst();
|
||||
QCOMPARE(s, "lock");
|
||||
s.removeLast();
|
||||
QCOMPARE(s, "loc");
|
||||
s.removeAt(s.indexOf('o'));
|
||||
QCOMPARE(s, "lc");
|
||||
s.clear();
|
||||
// No crash on empty strings
|
||||
s.removeFirst();
|
||||
s.removeLast();
|
||||
s.removeAt(2);
|
||||
}
|
||||
}
|
||||
|
||||
void tst_QString::erase_single_arg()
|
||||
|
Loading…
Reference in New Issue
Block a user