diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 108021cfc4..3f9e4012c1 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -179,6 +179,14 @@ Q_DECLARE_TYPEINFO(QLatin1String, Q_MOVABLE_TYPE); // Qt 4.x compatibility typedef QLatin1String QLatin1Literal; +// +// QStringView members that require QLatin1String: +// +bool QStringView::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW +{ return qStartsWith(*this, s, cs); } +bool QStringView::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW +{ return qEndsWith(*this, s, cs); } + class Q_CORE_EXPORT QString { public: diff --git a/src/corelib/tools/qstringview.cpp b/src/corelib/tools/qstringview.cpp index 3b36ed9bf8..258b05ada6 100644 --- a/src/corelib/tools/qstringview.cpp +++ b/src/corelib/tools/qstringview.cpp @@ -653,6 +653,38 @@ QT_BEGIN_NAMESPACE \sa mid(), left(), right(), chopped(), truncate() */ +/*! + \fn bool QStringView::startsWith(QStringView str, Qt::CaseSensitivity cs) const + \fn bool QStringView::startsWith(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn bool QStringView::startsWith(QChar ch) const + \fn bool QStringView::startsWith(QChar ch, Qt::CaseSensitivity cs) const + + Returns \c true if this string-view starts with string-view \a str, + Latin-1 string \a l1, or character \a ch, respectively; + otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; + otherwise the search is case-insensitive. + + \sa endsWith(), qStartsWith() +*/ + +/*! + \fn bool QStringView::endsWith(QStringView str, Qt::CaseSensitivity cs) const + \fn bool QStringView::endsWith(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn bool QStringView::endsWith(QChar ch) const + \fn bool QStringView::endsWith(QChar ch, Qt::CaseSensitivity cs) const + + Returns \c true if this string-view ends with string-view \a str, + Latin-1 string \a l1, or character \a ch, respectively; + otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; + otherwise the search is case-insensitive. + + \sa startsWith(), qEndsWith() +*/ + /*! \fn QByteArray QStringView::toLatin1() const diff --git a/src/corelib/tools/qstringview.h b/src/corelib/tools/qstringview.h index 527f1d48e7..62173f9765 100644 --- a/src/corelib/tools/qstringview.h +++ b/src/corelib/tools/qstringview.h @@ -241,6 +241,22 @@ public: Q_DECL_RELAXED_CONSTEXPR void chop(qssize_t n) { Q_ASSERT(n >= 0); Q_ASSERT(n <= size()); m_size -= n; } + Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW + { return qStartsWith(*this, s, cs); } + Q_REQUIRED_RESULT inline bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; + Q_REQUIRED_RESULT bool startsWith(QChar c) const Q_DECL_NOTHROW + { return !empty() && front() == c; } + Q_REQUIRED_RESULT bool startsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW + { return qStartsWith(*this, QStringView(&c, 1), cs); } + + Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW + { return qEndsWith(*this, s, cs); } + Q_REQUIRED_RESULT inline bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; + Q_REQUIRED_RESULT bool endsWith(QChar c) const Q_DECL_NOTHROW + { return !empty() && back() == c; } + Q_REQUIRED_RESULT bool endsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW + { return qEndsWith(*this, QStringView(&c, 1), cs); } + // // STL compatibility API: // diff --git a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp index 890cca7a55..b524a881b1 100644 --- a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -189,7 +189,7 @@ private: template void endsWith_impl() const; private Q_SLOTS: - // test all combinations of {QString, QStringRef} x {QString, QStringRef, QStringView, QLatin1String, QChar}: + // test all combinations of {QString, QStringRef, QStringView} x {QString, QStringRef, QStringView, QLatin1String, QChar}: void startsWith_QString_QString_data() { startsWith_data(); } void startsWith_QString_QString() { startsWith_impl(); } void startsWith_QString_QStringRef_data() { startsWith_data(); } @@ -212,6 +212,17 @@ private Q_SLOTS: void startsWith_QStringRef_QChar_data() { startsWith_data(false); } void startsWith_QStringRef_QChar() { startsWith_impl(); } + void startsWith_QStringView_QString_data() { startsWith_data(); } + void startsWith_QStringView_QString() { startsWith_impl(); } + void startsWith_QStringView_QStringRef_data() { startsWith_data(); } + void startsWith_QStringView_QStringRef() { startsWith_impl(); } + void startsWith_QStringView_QStringView_data() { startsWith_data(); } + void startsWith_QStringView_QStringView() { startsWith_impl(); } + void startsWith_QStringView_QLatin1String_data() { startsWith_data(); } + void startsWith_QStringView_QLatin1String() { startsWith_impl(); } + void startsWith_QStringView_QChar_data() { startsWith_data(false); } + void startsWith_QStringView_QChar() { startsWith_impl(); } + void endsWith_QString_QString_data() { endsWith_data(); } void endsWith_QString_QString() { endsWith_impl(); } void endsWith_QString_QStringRef_data() { endsWith_data(); } @@ -234,6 +245,17 @@ private Q_SLOTS: void endsWith_QStringRef_QChar_data() { endsWith_data(false); } void endsWith_QStringRef_QChar() { endsWith_impl(); } + void endsWith_QStringView_QString_data() { endsWith_data(); } + void endsWith_QStringView_QString() { endsWith_impl(); } + void endsWith_QStringView_QStringRef_data() { endsWith_data(); } + void endsWith_QStringView_QStringRef() { endsWith_impl(); } + void endsWith_QStringView_QStringView_data() { endsWith_data(); } + void endsWith_QStringView_QStringView() { endsWith_impl(); } + void endsWith_QStringView_QLatin1String_data() { endsWith_data(); } + void endsWith_QStringView_QLatin1String() { endsWith_impl(); } + void endsWith_QStringView_QChar_data() { endsWith_data(false); } + void endsWith_QStringView_QChar() { endsWith_impl(); } + private: void mid_data(); template void mid_impl();