diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index ca2ebe2b9e..a00383b304 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -4151,6 +4151,12 @@ qsizetype QString::lastIndexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) return qLastIndexOf(QStringView(*this), ch, from, cs); } +/*! + \fn QString::lastIndexOf(QChar ch, Qt::CaseSensitivity) const + \since 6.3 + \overload lastIndexOf() +*/ + /*! \fn qsizetype QString::lastIndexOf(QStringView str, qsizetype from, Qt::CaseSensitivity cs) const \since 5.14 @@ -9313,6 +9319,7 @@ QString &QString::setRawData(const QChar *unicode, qsizetype size) */ /*! + \fn qsizetype QLatin1String::lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const \fn qsizetype QLatin1String::lastIndexOf(QLatin1Char ch, qsizetype from, Qt::CaseSensitivity cs) const \since 6.3 \overload diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 5229c030e2..f53cb869c5 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -169,9 +169,13 @@ public: { return lastIndexOf(s, size(), cs); } [[nodiscard]] qsizetype lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, s, cs); } - [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(c, -1, cs); } + [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } - [[nodiscard]] qsizetype lastIndexOf(QLatin1Char c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + [[nodiscard]] qsizetype lastIndexOf(QLatin1Char c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(c, -1, cs); } + [[nodiscard]] qsizetype lastIndexOf(QLatin1Char c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { char ch = c.toLatin1(); return QtPrivate::lastIndexOf(*this, from, QLatin1String(&ch, 1), cs); } using value_type = const char; @@ -521,7 +525,9 @@ public: #endif [[nodiscard]] qsizetype indexOf(QStringView s, qsizetype from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::findString(*this, from, s, cs); } - [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(c, -1, cs); } + [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; [[nodiscard]] qsizetype lastIndexOf(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return lastIndexOf(s, size(), cs); } [[nodiscard]] qsizetype lastIndexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; diff --git a/src/corelib/text/qstringview.cpp b/src/corelib/text/qstringview.cpp index 79d0ccd819..faaed16044 100644 --- a/src/corelib/text/qstringview.cpp +++ b/src/corelib/text/qstringview.cpp @@ -915,6 +915,12 @@ QT_BEGIN_NAMESPACE \sa QString::lastIndexOf() */ +/*! + \fn QStringView::lastIndexOf(QChar c, Qt::CaseSensitivity cs) const + \since 6.3 + \overload lastIndexOf() +*/ + #if QT_CONFIG(regularexpression) /*! \fn qsizetype QStringView::indexOf(const QRegularExpression &re, qsizetype from, QRegularExpressionMatch *rmatch) const diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index 5c204fb7dd..5d05dd6edd 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -342,7 +342,9 @@ public: [[nodiscard]] qsizetype count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::count(*this, s, cs); } - [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + [[nodiscard]] qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return lastIndexOf(c, -1, cs); } + [[nodiscard]] qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::lastIndexOf(*this, from, QStringView(&c, 1), cs); } [[nodiscard]] qsizetype lastIndexOf(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return lastIndexOf(s, size(), cs); } diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index a64811cea4..c1795791c5 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -913,6 +913,15 @@ private Q_SLOTS: void isValidUtf8_QUtf8StringView() { isValidUtf8_impl(); } }; +namespace help { + +template constexpr qsizetype size(const T &s) { return qsizetype(s.size()); } + +template <> constexpr qsizetype size(const QChar&) { return 1; } +template <> constexpr qsizetype size(const QLatin1Char&) { return 1; } +template <> constexpr qsizetype size(const char16_t&) { return 1; } +} // namespace help + namespace { void overload_s_a(const QString &) {} @@ -2638,6 +2647,13 @@ void tst_QStringApiSymmetry::lastIndexOf_impl() const QCOMPARE(haystack.lastIndexOf(needle, startpos, Qt::CaseSensitive), size_type(resultCS)); QCOMPARE(haystack.lastIndexOf(needle, startpos, Qt::CaseInsensitive), size_type(resultCIS)); + if (startpos == haystack.size() || + (startpos == -1 && help::size(needle) > 0)) { // -1 skips past-the-end-match w/empty needle + // check that calls without an explicit 'from' argument work, too: + QCOMPARE(haystack.lastIndexOf(needle), size_type(resultCS)); + QCOMPARE(haystack.lastIndexOf(needle, Qt::CaseSensitive), size_type(resultCS)); + QCOMPARE(haystack.lastIndexOf(needle, Qt::CaseInsensitive), size_type(resultCIS)); + } } void tst_QStringApiSymmetry::indexOf_contains_lastIndexOf_count_regexp_data()