From 64fe66e7be9d30e523f766ee9822eba61f5014b3 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Mon, 30 Jul 2018 14:46:05 +0200 Subject: [PATCH] Add QString::compare(QStringView, CaseSensitivity) There was a QStringView::compare(QString, CaseSensitivity) but it's good that the symmetric also exists Change-Id: Ic789f11d41eb8cfa393cb51c19bd1f89bb87d912 Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.cpp | 13 +++++++++++++ src/corelib/tools/qstring.h | 9 ++++++++- tests/auto/corelib/tools/qstring/tst_qstring.cpp | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 6030d7058c..b3ed62982e 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -6225,7 +6225,17 @@ QString& QString::fill(QChar ch, int size) sensitivity setting \a cs. */ +/*! + \fn int QString::compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.12 + \overload compare() + + Performs a comparison of this with \a s, using the case + sensitivity setting \a cs. +*/ + +#if QT_STRINGVIEW_LEVEL < 2 /*! \overload compare() \since 4.2 @@ -6241,6 +6251,7 @@ int QString::compare(const QString &other, Qt::CaseSensitivity cs) const Q_DECL_ { return qt_compare_strings(*this, other, cs); } +#endif /*! \internal @@ -6267,6 +6278,7 @@ int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const Q_DECL_N return qt_compare_strings(*this, other, cs); } +#if QT_STRINGVIEW_LEVEL < 2 /*! \fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \overload compare() @@ -6275,6 +6287,7 @@ int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const Q_DECL_N an integer less than, equal to, or greater than zero if the string is less than, equal to, or greater than \a ref. */ +#endif /*! \internal diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index f18baf7065..6be3dcdbe1 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -608,8 +608,12 @@ public: QString &setUnicode(const QChar *unicode, int size); inline QString &setUtf16(const ushort *utf16, int size); +#if QT_STRINGVIEW_LEVEL < 2 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; + inline int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; +#endif int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; + inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; static inline int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW @@ -622,7 +626,6 @@ public: Qt::CaseSensitivity cs = Qt::CaseSensitive) Q_DECL_NOTHROW { return -s2.compare(s1, cs); } - inline int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Q_DECL_NOTHROW; static int compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity = Qt::CaseSensitive) Q_DECL_NOTHROW; @@ -1638,8 +1641,12 @@ inline bool operator> (const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW inline bool operator<=(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs >= lhs; } inline bool operator>=(const QStringRef &lhs, const QString &rhs) Q_DECL_NOTHROW { return rhs <= lhs; } +#if QT_STRINGVIEW_LEVEL < 2 inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW { return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); } +#endif +inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW +{ return -s.compare(*this, cs); } inline int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs) Q_DECL_NOTHROW { return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); } inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const Q_DECL_NOTHROW diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index e6dfe81ca9..f429bda804 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -6238,11 +6238,14 @@ void tst_QString::compare() QStringRef r1(&s1, 0, s1.length()); QStringRef r2(&s2, 0, s2.length()); + const QStringView v2(s2); + QCOMPARE(sign(QString::compare(s1, s2)), csr); QCOMPARE(sign(QStringRef::compare(r1, r2)), csr); QCOMPARE(sign(s1.compare(s2)), csr); QCOMPARE(sign(s1.compare(r2)), csr); QCOMPARE(sign(r1.compare(r2)), csr); + QCOMPARE(sign(s1.compare(v2)), csr); QCOMPARE(sign(s1.compare(s2, Qt::CaseSensitive)), csr); QCOMPARE(sign(s1.compare(s2, Qt::CaseInsensitive)), cir); @@ -6250,6 +6253,8 @@ void tst_QString::compare() QCOMPARE(sign(s1.compare(r2, Qt::CaseInsensitive)), cir); QCOMPARE(sign(r1.compare(r2, Qt::CaseSensitive)), csr); QCOMPARE(sign(r1.compare(r2, Qt::CaseInsensitive)), cir); + QCOMPARE(sign(s1.compare(v2, Qt::CaseSensitive)), csr); + QCOMPARE(sign(s1.compare(v2, Qt::CaseInsensitive)), cir); QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseSensitive)), csr); QCOMPARE(sign(QString::compare(s1, s2, Qt::CaseInsensitive)), cir);