diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-09-23 12:37:29 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-10-15 11:29:25 +0200 |
commit | b09b87a664415ccf3574cfad94acd0cfd2af21c3 (patch) | |
tree | 12ed87bbe692f609f7c89d1c7021a4b23835aa8c /src | |
parent | f2b8412f91198ef1d00c06a05183de1768b85757 (diff) |
Add QStringView::count(...)
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 <sona.kurazyan@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qstring.h | 5 | ||||
-rw-r--r-- | src/corelib/text/qstringview.cpp | 35 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 3 |
3 files changed, 43 insertions, 0 deletions
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 @@ -936,6 +936,41 @@ QT_BEGIN_NAMESPACE */ /*! + \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 Returns the string converted to a \c{long long} using base \a 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 |