QString/QByteArray: make all symmetry-checked member-compare() combinations noexcept

In QByteArray, they were just not marked as such.

In QString and QStringRef, the implicit conversion from QChar to
QString would destroy it. Add a QChar overload, delegating to
QStringView.

Added docs for the new overloads, copying from the nearest neighbor so
as to not look out of place. All string classes use different wording
for these functions. A cleanup of this state of affairs is out of the
scope of this patch.

Change-Id: I0b7b1d037aa229bcaf29b793841a18caf977d66b
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2019-07-27 15:47:54 +03:00
parent 9e46dd7719
commit 728cc964f3
4 changed files with 31 additions and 8 deletions

View File

@ -240,8 +240,8 @@ public:
int count(const char *a) const;
int count(const QByteArray &a) const;
inline int compare(const char *c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
inline int compare(const QByteArray &a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
inline int compare(const char *c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
inline int compare(const QByteArray &a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
Q_REQUIRED_RESULT QByteArray left(int len) const;
Q_REQUIRED_RESULT QByteArray right(int len) const;
@ -663,12 +663,12 @@ inline bool QByteArray::contains(const QByteArray &a) const
{ return indexOf(a) != -1; }
inline bool QByteArray::contains(char c) const
{ return indexOf(c) != -1; }
inline int QByteArray::compare(const char *c, Qt::CaseSensitivity cs) const
inline int QByteArray::compare(const char *c, Qt::CaseSensitivity cs) const noexcept
{
return cs == Qt::CaseSensitive ? qstrcmp(*this, c) :
qstrnicmp(data(), size(), c, -1);
}
inline int QByteArray::compare(const QByteArray &a, Qt::CaseSensitivity cs) const
inline int QByteArray::compare(const QByteArray &a, Qt::CaseSensitivity cs) const noexcept
{
return cs == Qt::CaseSensitive ? qstrcmp(*this, a) :
qstrnicmp(data(), size(), a.data(), a.size());

View File

@ -6276,6 +6276,16 @@ QString& QString::fill(QChar ch, int size)
sensitivity setting \a cs.
*/
/*!
\fn int QString::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
\since 5.14
\overload compare()
Performs a comparison of this with \a ch, using the case
sensitivity setting \a cs.
*/
#if QT_STRINGVIEW_LEVEL < 2
/*!
\overload compare()
@ -11073,6 +11083,19 @@ QStringRef QStringRef::appendTo(QString *string) const
Equivalent to \c {compare(*this, other, cs)}.
*/
/*!
\overload
\fn int QStringRef::compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
\since 5.14
Compares this string with \a ch and returns an
integer less than, equal to, or greater than zero if this string
is less than, equal to, or greater than \a ch, interpreted as a string of length one.
If \a cs is Qt::CaseSensitive, the comparison is case sensitive;
otherwise the comparison is case insensitive.
*/
/*!
\overload
\fn int QStringRef::compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

View File

@ -732,6 +732,8 @@ public:
#endif
int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
int compare(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
{ return compare(QStringView{&ch, 1}, cs); }
static inline int compare(const QString &s1, const QString &s2,
Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept
@ -1749,6 +1751,8 @@ public:
int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
int compare(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept
{ return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); }
int compare(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept;
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
int compare(const QByteArray &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

View File

@ -830,10 +830,6 @@ void tst_QStringApiSymmetry::member_compare_impl() const
QEXPECT_FAIL("", "Qt is missing a nothrow utf8-utf16 comparator", Continue); \
QVERIFY(noexcept(expr)); } while (0)
if (std::is_same<LHS, QByteArray>::value || // needs to simply be marked as noexcept
((std::is_same<LHS, QString>::value || std::is_same<LHS, QStringRef>::value)
&& std::is_same<RHS, QChar>::value)) // implict QChar -> QString conversion kills noexcept
QEXPECT_FAIL("", "known issues, will be fixed before 5.14 release", Continue);
QVERIFY_NOEXCEPT(lhs.compare(rhs, Qt::CaseSensitive));
QCOMPARE(sign(lhs.compare(rhs)), caseSensitiveCompareResult);