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>
This commit is contained in:
Sona Kurazyan 2022-03-03 17:36:49 +01:00
parent 30d28810ee
commit 82e12c79b2
6 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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); }

View File

@ -738,9 +738,8 @@ private Q_SLOTS:
void count_QStringView_QString_data() { count_data(); }
void count_QStringView_QString() { count_impl<QStringView, QString>(); }
// TODO: enable when QStringView::count(QLatin1String, ...) is implemented
// void count_QStringView_QLatin1String_data() { count_data(); }
// void count_QStringView_QLatin1String() { count_impl<QStringView, QLatin1String>(); }
void count_QStringView_QLatin1String_data() { count_data(); }
void count_QStringView_QLatin1String() { count_impl<QStringView, QLatin1String>(); }
void count_QStringView_QStringView_data() { count_data(); }
void count_QStringView_QStringView() { count_impl<QStringView, QStringView>(); }
void count_QStringView_QChar_data() { count_data(); }