From 3644e722c2162104dc7ff51a4effbeb62f26dfb0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 24 Apr 2017 11:33:02 +0200 Subject: [PATCH] QLatin1String: add startsWith()/endsWith() [ChangeLog][QtCore][QLatin1String] Added startsWith(), endsWith(). Change-Id: I7f75a5a1f0409f4b9d3e41b73f4fb5d137572b09 Reviewed-by: Edward Welbourne Reviewed-by: Anton Kudryavtsev Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.cpp | 40 +++++++++++++++++++ src/corelib/tools/qstring.h | 18 +++++++++ .../tst_qstringapisymmetry.cpp | 24 ++++++++++- 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 32847933c6..bfa6fb63b5 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8888,6 +8888,46 @@ QString &QString::setRawData(const QChar *unicode, int size) \sa front(), at(), operator[]() */ +/*! + \fn bool QLatin1String::startsWith(QStringView str, Qt::CaseSensitivity cs) const + \since 5.10 + \fn bool QLatin1String::startsWith(QLatin1String l1, Qt::CaseSensitivity cs) const + \since 5.10 + \fn bool QLatin1String::startsWith(QChar ch) const + \since 5.10 + \fn bool QLatin1String::startsWith(QChar ch, Qt::CaseSensitivity cs) const + \since 5.10 + + Returns \c true if this Latin-1 string 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 QLatin1String::endsWith(QStringView str, Qt::CaseSensitivity cs) const + \since 5.10 + \fn bool QLatin1String::endsWith(QLatin1String l1, Qt::CaseSensitivity cs) const + \since 5.10 + \fn bool QLatin1String::endsWith(QChar ch) const + \since 5.10 + \fn bool QLatin1String::endsWith(QChar ch, Qt::CaseSensitivity cs) const + \since 5.10 + + Returns \c true if this Latin-1 string 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 QLatin1String::const_iterator QLatin1String::begin() const \since 5.10 diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 3f9e4012c1..ca61d95793 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -110,6 +110,24 @@ public: Q_DECL_CONSTEXPR QLatin1Char front() const Q_REQUIRED_RESULT { return at(0); } Q_DECL_CONSTEXPR QLatin1Char back() const Q_REQUIRED_RESULT { return at(size() - 1); } + Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW + { return qStartsWith(*this, s, cs); } + Q_REQUIRED_RESULT bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW + { return qStartsWith(*this, s, cs); } + Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool startsWith(QChar c) const Q_DECL_NOTHROW + { return !isEmpty() && front() == c; } + Q_REQUIRED_RESULT inline 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 bool endsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW + { return qEndsWith(*this, s, cs); } + Q_REQUIRED_RESULT Q_DECL_CONSTEXPR bool endsWith(QChar c) const Q_DECL_NOTHROW + { return !isEmpty() && back() == c; } + Q_REQUIRED_RESULT inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW + { return qEndsWith(*this, QStringView(&c, 1), cs); } + using value_type = const char; using reference = value_type&; using const_reference = reference; diff --git a/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/tools/qstringapisymmetry/tst_qstringapisymmetry.cpp index b524a881b1..f8d4e52bf8 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, QStringView} x {QString, QStringRef, QStringView, QLatin1String, QChar}: + // test all combinations of {QString, QStringRef, QStringView, QLatin1String} 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(); } @@ -223,6 +223,17 @@ private Q_SLOTS: void startsWith_QStringView_QChar_data() { startsWith_data(false); } void startsWith_QStringView_QChar() { startsWith_impl(); } + void startsWith_QLatin1String_QString_data() { startsWith_data(); } + void startsWith_QLatin1String_QString() { startsWith_impl(); } + void startsWith_QLatin1String_QStringRef_data() { startsWith_data(); } + void startsWith_QLatin1String_QStringRef() { startsWith_impl(); } + void startsWith_QLatin1String_QStringView_data() { startsWith_data(); } + void startsWith_QLatin1String_QStringView() { startsWith_impl(); } + void startsWith_QLatin1String_QLatin1String_data() { startsWith_data(); } + void startsWith_QLatin1String_QLatin1String() { startsWith_impl(); } + void startsWith_QLatin1String_QChar_data() { startsWith_data(false); } + void startsWith_QLatin1String_QChar() { startsWith_impl(); } + void endsWith_QString_QString_data() { endsWith_data(); } void endsWith_QString_QString() { endsWith_impl(); } void endsWith_QString_QStringRef_data() { endsWith_data(); } @@ -256,6 +267,17 @@ private Q_SLOTS: void endsWith_QStringView_QChar_data() { endsWith_data(false); } void endsWith_QStringView_QChar() { endsWith_impl(); } + void endsWith_QLatin1String_QString_data() { endsWith_data(); } + void endsWith_QLatin1String_QString() { endsWith_impl(); } + void endsWith_QLatin1String_QStringRef_data() { endsWith_data(); } + void endsWith_QLatin1String_QStringRef() { endsWith_impl(); } + void endsWith_QLatin1String_QStringView_data() { endsWith_data(); } + void endsWith_QLatin1String_QStringView() { endsWith_impl(); } + void endsWith_QLatin1String_QLatin1String_data() { endsWith_data(); } + void endsWith_QLatin1String_QLatin1String() { endsWith_impl(); } + void endsWith_QLatin1String_QChar_data() { endsWith_data(false); } + void endsWith_QLatin1String_QChar() { endsWith_impl(); } + private: void mid_data(); template void mid_impl();