From 82e12c79b2b97bec6b56656c1cb977f2ba942d49 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 3 Mar 2022 17:36:49 +0100 Subject: [PATCH] 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 Reviewed-by: Edward Welbourne --- src/corelib/text/qstring.cpp | 11 +++++++++++ src/corelib/text/qstring.h | 3 +++ src/corelib/text/qstringalgorithms.h | 1 + src/corelib/text/qstringview.cpp | 15 +++++++++++++++ src/corelib/text/qstringview.h | 1 + .../qstringapisymmetry/tst_qstringapisymmetry.cpp | 5 ++--- 6 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 3aa86c49a9a..c0fed806a6d 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 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 db4e0cbd8a7..59ddcc1bebc 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 1fa32c8bc2d..490e4376db1 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 faaed160443..c65d3aa4bf9 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -1152,6 +1152,21 @@ QT_BEGIN_NAMESPACE \sa QString::count(), contains(), indexOf() */ +/*! + \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 diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index f229df9c210..ede684e3f51 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); } diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index 32d5e014bde..6c90d9ee1b2 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -738,9 +738,8 @@ private Q_SLOTS: void count_QStringView_QString_data() { count_data(); } void count_QStringView_QString() { count_impl(); } - // TODO: enable when QStringView::count(QLatin1String, ...) is implemented - // void count_QStringView_QLatin1String_data() { count_data(); } - // void count_QStringView_QLatin1String() { count_impl(); } + void count_QStringView_QLatin1String_data() { count_data(); } + void count_QStringView_QLatin1String() { count_impl(); } void count_QStringView_QStringView_data() { count_data(); } void count_QStringView_QStringView() { count_impl(); } void count_QStringView_QChar_data() { count_data(); }