diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-04-24 11:32:17 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-04-28 16:41:36 +0000 |
commit | d21a147e2bad29906696125605328a563cd7ed2f (patch) | |
tree | c047b8e493cc3d6ae8cc638f25a8be1e145e2fb5 | |
parent | 5f3d6ce5709466d08b11b6f2c8608d131e7c3f43 (diff) |
QStringView: add startsWith(), endsWith()
Change-Id: I72aef9236daedc3013c62d3f1d737159f85572b8
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/corelib/tools/qstring.h | 8 | ||||
-rw-r--r-- | src/corelib/tools/qstringview.cpp | 32 | ||||
-rw-r--r-- | src/corelib/tools/qstringview.h | 16 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp | 24 |
4 files changed, 79 insertions, 1 deletions
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 @@ -654,6 +654,38 @@ QT_BEGIN_NAMESPACE */ /*! + \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 Returns a Latin-1 representation of the string as a QByteArray. 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 <typename Haystack, typename Needle> 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<QString, QString>(); } 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<QStringRef, QChar>(); } + void startsWith_QStringView_QString_data() { startsWith_data(); } + void startsWith_QStringView_QString() { startsWith_impl<QStringView, QString>(); } + void startsWith_QStringView_QStringRef_data() { startsWith_data(); } + void startsWith_QStringView_QStringRef() { startsWith_impl<QStringView, QStringRef>(); } + void startsWith_QStringView_QStringView_data() { startsWith_data(); } + void startsWith_QStringView_QStringView() { startsWith_impl<QStringView, QStringView>(); } + void startsWith_QStringView_QLatin1String_data() { startsWith_data(); } + void startsWith_QStringView_QLatin1String() { startsWith_impl<QStringView, QLatin1String>(); } + void startsWith_QStringView_QChar_data() { startsWith_data(false); } + void startsWith_QStringView_QChar() { startsWith_impl<QStringView, QChar>(); } + void endsWith_QString_QString_data() { endsWith_data(); } void endsWith_QString_QString() { endsWith_impl<QString, QString>(); } 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<QStringRef, QChar>(); } + void endsWith_QStringView_QString_data() { endsWith_data(); } + void endsWith_QStringView_QString() { endsWith_impl<QStringView, QString>(); } + void endsWith_QStringView_QStringRef_data() { endsWith_data(); } + void endsWith_QStringView_QStringRef() { endsWith_impl<QStringView, QStringRef>(); } + void endsWith_QStringView_QStringView_data() { endsWith_data(); } + void endsWith_QStringView_QStringView() { endsWith_impl<QStringView, QStringView>(); } + void endsWith_QStringView_QLatin1String_data() { endsWith_data(); } + void endsWith_QStringView_QLatin1String() { endsWith_impl<QStringView, QLatin1String>(); } + void endsWith_QStringView_QChar_data() { endsWith_data(false); } + void endsWith_QStringView_QChar() { endsWith_impl<QStringView, QChar>(); } + private: void mid_data(); template <typename String> void mid_impl(); |