QStringView: add startsWith(), endsWith()

Change-Id: I72aef9236daedc3013c62d3f1d737159f85572b8
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Marc Mutz 2017-04-24 11:32:17 +02:00
parent 5f3d6ce570
commit d21a147e2b
4 changed files with 79 additions and 1 deletions

View File

@ -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:

View File

@ -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

View File

@ -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:
//

View File

@ -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();