diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-05-22 09:57:33 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-05-30 23:11:09 +0200 |
commit | a1056096fc5ec316a650fd8ae1c2a22b6944d091 (patch) | |
tree | 8586103ce9378295addf3f7d3fc0beae2d9e6664 /src/corelib/text | |
parent | 2b7b75f721b6786a6dc35e2f9b693bb2e2dfac01 (diff) |
Add support for count() to QStringView
Make the API more symmetric with regards to both QString and QStringRef.
Change-Id: Ia67c53ba708f6c33874d1a127de8e2857ad9b5b8
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qstring.cpp | 19 | ||||
-rw-r--r-- | src/corelib/text/qstringalgorithms.h | 3 | ||||
-rw-r--r-- | src/corelib/text/qstringview.cpp | 30 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 5 |
4 files changed, 46 insertions, 11 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 12759c4a5b..cda1c380bd 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -151,8 +151,6 @@ static inline qsizetype qLastIndexOf(Haystack haystack, QChar needle, qsizetype template <> inline qsizetype qLastIndexOf(QString haystack, QChar needle, qsizetype from, Qt::CaseSensitivity cs) noexcept = delete; // unwanted, would detach -static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); -static inline qsizetype qt_string_count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); static inline bool qt_starts_with(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); static inline bool qt_starts_with(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs); @@ -4021,7 +4019,7 @@ QString &QString::replace(const QRegularExpression &re, const QString &after) int QString::count(const QString &str, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qt_string_count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); + return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); } /*! @@ -4038,7 +4036,7 @@ int QString::count(const QString &str, Qt::CaseSensitivity cs) const int QString::count(QChar ch, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qt_string_count(QStringView(unicode(), size()), ch, cs)); + return int(QtPrivate::count(QStringView(unicode(), size()), ch, cs)); } /*! @@ -4055,7 +4053,7 @@ int QString::count(QChar ch, Qt::CaseSensitivity cs) const int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qt_string_count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); + return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); } #if QT_STRINGVIEW_LEVEL < 2 @@ -10932,7 +10930,7 @@ int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qt_string_count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); + return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); } /*! @@ -10950,7 +10948,7 @@ int QStringRef::count(const QString &str, Qt::CaseSensitivity cs) const int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qt_string_count(QStringView(unicode(), size()), ch, cs)); + return int(QtPrivate::count(QStringView(unicode(), size()), ch, cs)); } /*! @@ -10968,7 +10966,7 @@ int QStringRef::count(QChar ch, Qt::CaseSensitivity cs) const int QStringRef::count(const QStringRef &str, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(qt_string_count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); + return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); } /*! @@ -11221,7 +11219,7 @@ bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ -static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) +qsizetype QtPrivate::count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) noexcept { qsizetype num = 0; qsizetype i = -1; @@ -11236,8 +11234,7 @@ static inline qsizetype qt_string_count(QStringView haystack, QStringView needle return num; } -static inline qsizetype qt_string_count(QStringView haystack, QChar ch, - Qt::CaseSensitivity cs) +qsizetype QtPrivate::count(QStringView haystack, QChar ch, Qt::CaseSensitivity cs) noexcept { qsizetype num = 0; if (cs == Qt::CaseSensitive) { diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h index 86bacdccad..f1aa052eac 100644 --- a/src/corelib/text/qstringalgorithms.h +++ b/src/corelib/text/qstringalgorithms.h @@ -88,6 +88,9 @@ Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype lastIndexOf(QLati Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QStringView trimmed(QStringView s) noexcept; Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION QLatin1String trimmed(QLatin1String s) noexcept; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION qsizetype count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept; + Q_REQUIRED_RESULT Q_CORE_EXPORT QByteArray convertToLatin1(QStringView str); Q_REQUIRED_RESULT Q_CORE_EXPORT QByteArray convertToUtf8(QStringView str); Q_REQUIRED_RESULT Q_CORE_EXPORT QByteArray convertToLocal8Bit(QStringView str); diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 8f87943204..abb7015b68 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -914,6 +914,36 @@ QT_BEGIN_NAMESPACE */ /*! + \fn qsizetype QStringView::count(QChar ch, Qt::CaseSensitivity cs) const noexcept + + \since 6.0 + \overload count() + + 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. + + \sa QString::count(), contains(), indexOf() +*/ + +/*! + \fn qsizetype QStringView::count(QStringView str, Qt::CaseSensitivity cs) const noexcept + + \since 6.0 + \overload count() + + 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. + + \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 2b7863e156..e2e1abb62d 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -309,6 +309,11 @@ 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 qsizetype count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::count(*this, c, cs); } + Q_REQUIRED_RESULT qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return QtPrivate::count(*this, s, cs); } + 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 |