From 5771591fbce7d3f4359aea7342d036c8f287bb64 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sat, 17 Oct 2020 02:25:53 +0200 Subject: [PATCH] QString/QByteArray: add erase() for iterators Otherwise they're not usable with iterator-based algorithms that e.g. remove, partition and the like. [ChangeLog][QtCore][QString] Added erase(). [ChangeLog][QtCore][QByteArray] Added erase(). Change-Id: I78829b1a5365dd53b6b6423ceedbc52edeafbc63 Reviewed-by: Thiago Macieira Reviewed-by: Andrei Golubev --- src/corelib/text/qbytearray.cpp | 15 +++++++++++++++ src/corelib/text/qbytearray.h | 1 + src/corelib/text/qstring.cpp | 15 +++++++++++++++ src/corelib/text/qstring.h | 1 + 4 files changed, 32 insertions(+) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 4679c5a05d..3845946ce9 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1115,6 +1115,21 @@ QByteArray qUncompress(const uchar* data, qsizetype nbytes) squeeze(). */ +/*! + \since 6.1 + + Removes from the byte array 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. +*/ +QByteArray::iterator QByteArray::erase(QByteArray::const_iterator first, QByteArray::const_iterator last) +{ + const auto start = std::distance(cbegin(), first); + const auto len = std::distance(first, last); + remove(start, len); + return begin() + start; +} + /*! \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 9f646aaa62..e185887ad7 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -497,6 +497,7 @@ public: void push_front(QByteArrayView a) { prepend(a); } void shrink_to_fit() { squeeze(); } + iterator erase(const_iterator first, const_iterator last); static inline QByteArray fromStdString(const std::string &s); inline std::string toStdString() const; diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index dd00456fb2..cc2c093cea 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -8512,6 +8512,21 @@ bool QString::isRightToLeft() const Appends the given \a ch character onto the end of this string. */ +/*! + \since 6.1 + + 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. +*/ +QString::iterator QString::erase(QString::const_iterator first, QString::const_iterator last) +{ + const auto start = std::distance(cbegin(), first); + const auto len = std::distance(first, last); + remove(start, len); + return begin() + start; +} + /*! \fn void QString::shrink_to_fit() \since 5.10 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 013b367b5c..fa4eeb0367 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1016,6 +1016,7 @@ public: inline void push_front(QChar c) { prepend(c); } inline void push_front(const QString &s) { prepend(s); } void shrink_to_fit() { squeeze(); } + iterator erase(const_iterator first, const_iterator last); static inline QString fromStdString(const std::string &s); inline std::string toStdString() const;