From cac2fc81b75f60d84b850fec81b432b020e89ed3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 24 Apr 2017 11:26:25 +0200 Subject: [PATCH] Long live qStartsWith()/qEndsWith()! Following the established pattern of implementing non-mutating string algorithms as free functions over views with a public qFoo() and a static qt_foo() split, add qStartsWith() and qEndsWith(), calling the existing qt_{starts,ends}_with_impl() templates. [ChangeLog][QtCore][QStringAlgorithms] Added qStartsWith(), qEndsWith(). Change-Id: Ic95c2a9c13883a99f1a96319083fb249f20de5f1 Reviewed-by: Edward Welbourne Reviewed-by: Anton Kudryavtsev Reviewed-by: Lars Knoll Reviewed-by: Martin Smith Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 80 +++++++++++++++++++++++++++ src/corelib/tools/qstringalgorithms.h | 12 ++++ 2 files changed, 92 insertions(+) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index a46ff1e881..d6a87378ee 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -10907,6 +10907,46 @@ static inline bool qt_starts_with(QStringView haystack, QChar needle, Qt::CaseSe : foldCase(haystack.front()) == foldCase(needle)); } +/*! + \fn bool qStartsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) + \since 5.10 + \fn bool qStartsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) + \since 5.10 + \fn bool qStartsWith(QLatin1String haystack, QStringview needle, Qt::CaseSensitivity cs) + \since 5.10 + \fn bool qStartsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) + \since 5.10 + \relates QStringView + + Returns \c true if \a haystack starts with \a needle, + otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; + otherwise the search is case-insensitive. + + \sa qEndsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1String::endsWith() +*/ + +bool qStartsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_starts_with_impl(haystack, needle, cs); +} + +bool qStartsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_starts_with_impl(haystack, needle, cs); +} + +bool qStartsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_starts_with_impl(haystack, needle, cs); +} + +bool qStartsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_starts_with_impl(haystack, needle, cs); +} + template bool qt_ends_with_impl(Haystack haystack, Needle needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW { @@ -10939,6 +10979,46 @@ static inline bool qt_ends_with(QStringView haystack, QChar needle, Qt::CaseSens : foldCase(haystack.back()) == foldCase(needle)); } +/*! + \fn bool qEndsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) + \since 5.10 + \fn bool qEndsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) + \since 5.10 + \fn bool qEndsWith(QLatin1String haystack, QStringview needle, Qt::CaseSensitivity cs) + \since 5.10 + \fn bool qEndsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) + \since 5.10 + \relates QStringView + + Returns \c true if \a haystack ends with \a needle, + otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (the default), the search is case-sensitive; + otherwise the search is case-insensitive. + + \sa qEndsWith(), QString::endsWith(), QStringView::endsWith(), QLatin1String::endsWith() +*/ + +bool qEndsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_ends_with_impl(haystack, needle, cs); +} + +bool qEndsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_ends_with_impl(haystack, needle, cs); +} + +bool qEndsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_ends_with_impl(haystack, needle, cs); +} + +bool qEndsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs) Q_DECL_NOTHROW +{ + return qt_ends_with_impl(haystack, needle, cs); +} + /*! \since 4.8 diff --git a/src/corelib/tools/qstringalgorithms.h b/src/corelib/tools/qstringalgorithms.h index 29fefd4cf5..91d85cb76b 100644 --- a/src/corelib/tools/qstringalgorithms.h +++ b/src/corelib/tools/qstringalgorithms.h @@ -58,6 +58,18 @@ Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int qCompareStrings(QStringView lhs, QLatin Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int qCompareStrings(QLatin1String lhs, QStringView rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW Q_REQUIRED_RESULT; Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int qCompareStrings(QLatin1String lhs, QLatin1String rhs, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW Q_REQUIRED_RESULT; + +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qStartsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qStartsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qStartsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qStartsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; + +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QStringView haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QLatin1String haystack, QStringView needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; +Q_REQUIRED_RESULT Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool qEndsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW; + + Q_CORE_EXPORT QByteArray qConvertToLatin1(QStringView str) Q_REQUIRED_RESULT; Q_CORE_EXPORT QByteArray qConvertToUtf8(QStringView str) Q_REQUIRED_RESULT; Q_CORE_EXPORT QByteArray qConvertToLocal8Bit(QStringView str) Q_REQUIRED_RESULT;