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 <edward.welbourne@qt.io>
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Martin Smith <martin.smith@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Marc Mutz 2017-04-24 11:26:25 +02:00
parent 99d809bd75
commit cac2fc81b7
2 changed files with 92 additions and 0 deletions

View File

@ -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 <typename Haystack, typename Needle>
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

View File

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