From b09b87a664415ccf3574cfad94acd0cfd2af21c3 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 23 Sep 2020 12:37:29 +0200 Subject: Add QStringView::count(...) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also this one as a porting aid towards Qt 6. The implementation is using QString to keep things simple. Task-number: QTBUG-86516 Change-Id: Ic033b8678c76a608af8acfc5fab547aeb159933e Reviewed-by: Sona Kurazyan Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.h | 5 +++++ src/corelib/text/qstringview.cpp | 35 +++++++++++++++++++++++++++++++++++ src/corelib/text/qstringview.h | 3 +++ 3 files changed, 43 insertions(+) diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index f48777d7f1..7906aeaffc 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -2121,6 +2121,11 @@ QString QLatin1String::arg(Args &&...args) const return QtPrivate::argToQStringDispatch(*this, QtPrivate::qStringLikeToArg(args)...); } +inline qsizetype QStringView::count(QChar c, Qt::CaseSensitivity cs) const noexcept +{ return toString().count(c, cs); } +inline qsizetype QStringView::count(QStringView s, Qt::CaseSensitivity cs) const noexcept +{ return toString().count(s.toString(), cs); } + inline short QStringView::toShort(bool *ok, int base) const { return toString().toShort(ok, base); } inline ushort QStringView::toUShort(bool *ok, int base) const diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index f3712a412a..1cbfb90d0f 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -935,6 +935,41 @@ QT_BEGIN_NAMESPACE \sa QString::toWCharArray() */ +/*! + \fn qsizetype QStringView::count(QChar ch, Qt::CaseSensitivity cs) const noexcept + + \since 5.15.2 + + Returns the number of occurrences of the character \a ch in the + string reference. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::count(), contains(), indexOf() +*/ + +/*! + \fn qsizetype QStringView::count(QStringView str, Qt::CaseSensitivity cs) const noexcept + + \since 5.15.2 + \overload + + Returns the number of (potentially overlapping) occurrences of the + string reference \a str in this string reference. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \note This method has been added in 5.15.2 to simplify writing code that is portable + between Qt 5.15 and Qt 6. The implementation is not tuned for performance in Qt 5. + + \sa QString::count(), contains(), indexOf() +*/ + /*! \fn qint64 QStringView::toLongLong(bool *ok, int base) const diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 5e561d88a8..38a6a43219 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -300,6 +300,9 @@ public: { return indexOf(s, 0, cs) != qsizetype(-1); } Q_REQUIRED_RESULT inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + Q_REQUIRED_RESULT inline qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + Q_REQUIRED_RESULT inline qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; + Q_REQUIRED_RESULT qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } Q_REQUIRED_RESULT qsizetype lastIndexOf(QStringView s, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept -- cgit v1.2.3