diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-03-03 17:36:49 +0100 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2022-03-09 15:58:47 +0100 |
commit | 82e12c79b2b97bec6b56656c1cb977f2ba942d49 (patch) | |
tree | 8e454dc9ce768fa7cb258ced2b0ae39b13db2198 /src/corelib/text | |
parent | 30d28810ee73052338e478a5472933c7b9c7d725 (diff) |
Add an overload of QStringView::count() for QLatin1String
Required for the API symmetry between QStringView and QLatin1String.
[ChangeLog][QtCore][QStringView] Added an overload of
QStringView::count() for QLatin1String.
Change-Id: Ic49a4b31e8f6f0969eff0f792654d23a60e06c49
Task-numer: QTBUG-98431
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qstring.cpp | 11 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 3 | ||||
-rw-r--r-- | src/corelib/text/qstringalgorithms.h | 1 | ||||
-rw-r--r-- | src/corelib/text/qstringview.cpp | 15 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 1 |
5 files changed, 31 insertions, 0 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 3aa86c49a9..c0fed806a6 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -10441,6 +10441,17 @@ qsizetype QtPrivate::count(QLatin1String haystack, QStringView needle, Qt::CaseS return num; } +qsizetype QtPrivate::count(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) +{ + if (haystack.size() < needle.size()) + return -1; + + QVarLengthArray<char16_t> s(needle.size()); + qt_from_latin1(s.data(), needle.latin1(), size_t(needle.size())); + + return QtPrivate::count(haystack, QStringView(s.data(), s.size()), cs); +} + qsizetype QtPrivate::count(QLatin1String haystack, QChar needle, Qt::CaseSensitivity cs) noexcept { // non-L1 needles cannot possibly match in L1-only haystacks diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index db4e0cbd8a..59ddcc1beb 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -396,6 +396,9 @@ qsizetype QStringView::lastIndexOf(QLatin1String s, Qt::CaseSensitivity cs) cons { return QtPrivate::lastIndexOf(*this, size(), s, cs); } qsizetype QStringView::lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::lastIndexOf(*this, from, s, cs); } +qsizetype QStringView::count(QLatin1String s, Qt::CaseSensitivity cs) const +{ return QtPrivate::count(*this, s, cs); } + // // QAnyStringView members that require QLatin1String diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h index 1fa32c8bc2..490e4376db 100644 --- a/src/corelib/text/qstringalgorithms.h +++ b/src/corelib/text/qstringalgorithms.h @@ -123,6 +123,7 @@ namespace QtPrivate { [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; +[[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive); [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive); [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive); [[nodiscard]] Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QLatin1String haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index faaed16044..c65d3aa4bf 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -1153,6 +1153,21 @@ QT_BEGIN_NAMESPACE */ /*! + \fn qsizetype QStringView::count(QLatin1String l1, Qt::CaseSensitivity cs) const noexcept + + \since 6.4 + \overload count() + + Returns the number of (potentially overlapping) occurrences of the + Latin-1 string \a l1 in this string view. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa QString::count(), contains(), indexOf() +*/ + +/*! \fn qint64 QStringView::toLongLong(bool *ok, int base) const Returns the string view converted to a \c{long long} using base \a diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index f229df9c21..ede684e3f5 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -341,6 +341,7 @@ public: { return QtPrivate::count(*this, c, cs); } [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::count(*this, s, cs); } + [[nodiscard]] inline qsizetype count(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return lastIndexOf(c, -1, cs); } |