From 728cc964f3f25d23c72317a119b839701f5941e8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 27 Jul 2019 15:47:54 +0300 Subject: [PATCH] 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 Reviewed-by: Lars Knoll --- src/corelib/text/qbytearray.h | 8 +++---- src/corelib/text/qstring.cpp | 23 +++++++++++++++++++ src/corelib/text/qstring.h | 4 ++++ .../tst_qstringapisymmetry.cpp | 4 ---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 36cf580cd9..e3bb9ea937 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -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()); diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 68dec7cfff..f64f148f49 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -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 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index bfc190e9e9..d615e6646c 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -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 diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index bcf4e73108..e6884bc2e5 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -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::value || // needs to simply be marked as noexcept - ((std::is_same::value || std::is_same::value) - && std::is_same::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);