diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-26 15:13:32 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-26 20:10:48 +0100 |
commit | 5cb150be46f92dd9a1d3cca4ae67cfefd7967bda (patch) | |
tree | 29fdc7a86fba17acba04b1ecaa2eb550e177311a /src/corelib | |
parent | bcc7d3454063520ec2a8d3c7a579a696ae2cc069 (diff) |
Make remaining String binary comparisons hidden friends
Also forced the introduction of explicit comparisons with
char16_t* and std::nullptr_t.
Change-Id: I8e32c14a1d3aeec234ee070b9fefc6af06db0fda
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/text/qstring.cpp | 20 | ||||
-rw-r--r-- | src/corelib/text/qstring.h | 110 | ||||
-rw-r--r-- | src/corelib/text/qstringview.h | 23 |
3 files changed, 85 insertions, 68 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index d6c115397c..26d0a7acc8 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -5773,10 +5773,9 @@ QString& QString::fill(QChar ch, qsizetype size) */ /*! - \fn bool operator==(const char *s1, const QString &s2) + \fn bool QString::operator==(const char *s1, const QString &s2) - \overload operator==() - \relates QString + \overload operator==() Returns \c true if \a s1 is equal to \a s2; otherwise returns \c false. Note that no string is equal to \a s1 being 0. @@ -5785,8 +5784,7 @@ QString& QString::fill(QChar ch, qsizetype size) */ /*! - \fn bool operator!=(const char *s1, const QString &s2) - \relates QString + \fn bool QString::operator!=(const char *s1, const QString &s2) Returns \c true if \a s1 is not equal to \a s2; otherwise returns \c false. @@ -5796,8 +5794,7 @@ QString& QString::fill(QChar ch, qsizetype size) */ /*! - \fn bool operator<(const char *s1, const QString &s2) - \relates QString + \fn bool QString::operator<(const char *s1, const QString &s2) Returns \c true if \a s1 is lexically less than \a s2; otherwise returns \c false. For \a s1 != 0, this is equivalent to \c @@ -5807,8 +5804,7 @@ QString& QString::fill(QChar ch, qsizetype size) */ /*! - \fn bool operator<=(const char *s1, const QString &s2) - \relates QString + \fn bool QString::operator<=(const char *s1, const QString &s2) Returns \c true if \a s1 is lexically less than or equal to \a s2; otherwise returns \c false. For \a s1 != 0, this is equivalent to \c @@ -5818,8 +5814,7 @@ QString& QString::fill(QChar ch, qsizetype size) */ /*! - \fn bool operator>(const char *s1, const QString &s2) - \relates QString + \fn bool QString::operator>(const char *s1, const QString &s2) Returns \c true if \a s1 is lexically greater than \a s2; otherwise returns \c false. Equivalent to \c {compare(s1, s2) > 0}. @@ -5828,8 +5823,7 @@ QString& QString::fill(QChar ch, qsizetype size) */ /*! - \fn bool operator>=(const char *s1, const QString &s2) - \relates QString + \fn bool QString::operator>=(const char *s1, const QString &s2) Returns \c true if \a s1 is lexically greater than or equal to \a s2; otherwise returns \c false. For \a s1 != 0, this is equivalent to \c diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index be71bf8699..761549582a 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -285,9 +285,19 @@ public: QT_ASCII_CAST_WARN inline bool operator>(const QByteArray &s) const; QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const; QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const; + + QT_ASCII_CAST_WARN friend bool operator==(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) == 0; } + QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) != 0; } + QT_ASCII_CAST_WARN friend bool operator< (const char *s1, QLatin1String s2) { return compare_helper(s2, s1) > 0; } + QT_ASCII_CAST_WARN friend bool operator> (const char *s1, QLatin1String s2) { return compare_helper(s2, s1) < 0; } + QT_ASCII_CAST_WARN friend bool operator<=(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) >= 0; } + QT_ASCII_CAST_WARN friend bool operator>=(const char *s1, QLatin1String s2) { return compare_helper(s2, s1) <= 0; } #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) private: +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + static inline int compare_helper(const QLatin1String &s1, const char *s2); +#endif Q_ALWAYS_INLINE constexpr void verify(qsizetype pos, qsizetype n = 0) const { Q_ASSERT(pos >= 0); @@ -853,6 +863,34 @@ public: friend bool operator<=(QLatin1String s1, const QString &s2) noexcept { return s2 >= s1; } friend bool operator>=(QLatin1String s1, const QString &s2) noexcept { return s2 <= s1; } + // Check isEmpty() instead of isNull() for backwards compatibility. + friend bool operator==(const QString &s1, std::nullptr_t) noexcept { return s1.isEmpty(); } + friend bool operator!=(const QString &s1, std::nullptr_t) noexcept { return !s1.isEmpty(); } + friend bool operator< (const QString & , std::nullptr_t) noexcept { return false; } + friend bool operator> (const QString &s1, std::nullptr_t) noexcept { return !s1.isEmpty(); } + friend bool operator<=(const QString &s1, std::nullptr_t) noexcept { return s1.isEmpty(); } + friend bool operator>=(const QString & , std::nullptr_t) noexcept { return true; } + friend bool operator==(std::nullptr_t, const QString &s2) noexcept { return s2 == nullptr; } + friend bool operator!=(std::nullptr_t, const QString &s2) noexcept { return s2 != nullptr; } + friend bool operator< (std::nullptr_t, const QString &s2) noexcept { return s2 > nullptr; } + friend bool operator> (std::nullptr_t, const QString &s2) noexcept { return s2 < nullptr; } + friend bool operator<=(std::nullptr_t, const QString &s2) noexcept { return s2 >= nullptr; } + friend bool operator>=(std::nullptr_t, const QString &s2) noexcept { return s2 <= nullptr; } + + friend bool operator==(const QString &s1, const char16_t *s2) { return s1 == QString::fromUtf16(s2); } + friend bool operator!=(const QString &s1, const char16_t *s2) { return s1 != QString::fromUtf16(s2); } + friend bool operator< (const QString &s1, const char16_t *s2) { return s1 < QString::fromUtf16(s2); } + friend bool operator> (const QString &s1, const char16_t *s2) { return s1 > QString::fromUtf16(s2); } + friend bool operator<=(const QString &s1, const char16_t *s2) { return s1 <= QString::fromUtf16(s2); } + friend bool operator>=(const QString &s1, const char16_t *s2) { return s1 >= QString::fromUtf16(s2); } + + friend bool operator==(const char16_t *s1, const QString &s2) { return s2 == s1; } + friend bool operator!=(const char16_t *s1, const QString &s2) { return s2 != s1; } + friend bool operator< (const char16_t *s1, const QString &s2) { return s2 > s1; } + friend bool operator> (const char16_t *s1, const QString &s2) { return s2 < s1; } + friend bool operator<=(const char16_t *s1, const QString &s2) { return s2 >= s1; } + friend bool operator>=(const char16_t *s1, const QString &s2) { return s2 <= s1; } + // QChar <> QString friend inline bool operator==(QChar lhs, const QString &rhs) noexcept { return rhs.size() == 1 && lhs == rhs.front(); } @@ -930,12 +968,18 @@ public: QT_ASCII_CAST_WARN inline bool operator<=(const QByteArray &s) const; QT_ASCII_CAST_WARN inline bool operator>=(const QByteArray &s) const; - friend inline bool operator==(const char *s1, const QString &s2); - friend inline bool operator!=(const char *s1, const QString &s2); - friend inline bool operator<(const char *s1, const QString &s2); - friend inline bool operator>(const char *s1, const QString &s2); - friend inline bool operator<=(const char *s1, const QString &s2); - friend inline bool operator>=(const char *s1, const QString &s2); + QT_ASCII_CAST_WARN friend bool operator==(const char *s1, const QString &s2) + { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; } + QT_ASCII_CAST_WARN friend bool operator!=(const char *s1, const QString &s2) + { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } + QT_ASCII_CAST_WARN friend bool operator< (const char *s1, const QString &s2) + { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } + QT_ASCII_CAST_WARN friend bool operator> (const char *s1, const QString &s2) + { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } + QT_ASCII_CAST_WARN friend bool operator<=(const char *s1, const QString &s2) + { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } + QT_ASCII_CAST_WARN friend bool operator>=(const char *s1, const QString &s2) + { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } #endif typedef QChar *iterator; @@ -1288,32 +1332,6 @@ inline bool QString::operator<=(const char *s) const inline bool QString::operator>=(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) >= 0; } -QT_ASCII_CAST_WARN inline bool operator==(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) == 0; } -QT_ASCII_CAST_WARN inline bool operator!=(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) != 0; } -QT_ASCII_CAST_WARN inline bool operator<(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) > 0; } -QT_ASCII_CAST_WARN inline bool operator>(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) < 0; } -QT_ASCII_CAST_WARN inline bool operator<=(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) >= 0; } -QT_ASCII_CAST_WARN inline bool operator>=(const char *s1, const QString &s2) -{ return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } - -QT_ASCII_CAST_WARN inline bool operator==(const char *s1, QLatin1String s2) -{ return QString::fromUtf8(s1) == s2; } -QT_ASCII_CAST_WARN inline bool operator!=(const char *s1, QLatin1String s2) -{ return QString::fromUtf8(s1) != s2; } -QT_ASCII_CAST_WARN inline bool operator<(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) < s2); } -QT_ASCII_CAST_WARN inline bool operator>(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) > s2); } -QT_ASCII_CAST_WARN inline bool operator<=(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) <= s2); } -QT_ASCII_CAST_WARN inline bool operator>=(const char *s1, QLatin1String s2) -{ return (QString::fromUtf8(s1) >= s2); } - QT_ASCII_CAST_WARN inline bool QLatin1String::operator==(const char *s) const { return QString::fromUtf8(s) == *this; } QT_ASCII_CAST_WARN inline bool QLatin1String::operator!=(const char *s) const @@ -1340,6 +1358,11 @@ QT_ASCII_CAST_WARN inline bool QLatin1String::operator<=(const QByteArray &s) co QT_ASCII_CAST_WARN inline bool QLatin1String::operator>=(const QByteArray &s) const { return QString::fromUtf8(s) <= *this; } +inline int QLatin1String::compare_helper(const QLatin1String &s1, const char *s2) +{ + return QString::compare(s1, QString::fromUtf8(s2)); +} + QT_ASCII_CAST_WARN inline bool QString::operator==(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) == 0; } QT_ASCII_CAST_WARN inline bool QString::operator!=(const QByteArray &s) const @@ -1437,29 +1460,6 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(QString::SectionFlags) inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept { return -s.compare(*this, cs); } -// QStringView <> QStringView -inline bool operator==(QStringView lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); } -inline bool operator!=(QStringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); } -inline bool operator< (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } -inline bool operator<=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } -inline bool operator> (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } -inline bool operator>=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } - -// QStringView <> QChar -inline bool operator==(QStringView lhs, QChar rhs) noexcept { return lhs == QStringView(&rhs, 1); } -inline bool operator!=(QStringView lhs, QChar rhs) noexcept { return lhs != QStringView(&rhs, 1); } -inline bool operator< (QStringView lhs, QChar rhs) noexcept { return lhs < QStringView(&rhs, 1); } -inline bool operator<=(QStringView lhs, QChar rhs) noexcept { return lhs <= QStringView(&rhs, 1); } -inline bool operator> (QStringView lhs, QChar rhs) noexcept { return lhs > QStringView(&rhs, 1); } -inline bool operator>=(QStringView lhs, QChar rhs) noexcept { return lhs >= QStringView(&rhs, 1); } - -inline bool operator==(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) == rhs; } -inline bool operator!=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) != rhs; } -inline bool operator< (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) < rhs; } -inline bool operator<=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) <= rhs; } -inline bool operator> (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) > rhs; } -inline bool operator>=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) >= rhs; } - inline int QString::localeAwareCompare(QStringView s) const { return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } inline int QString::localeAwareCompare(QStringView s1, QStringView s2) diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h index d29e6be4f8..05966ec9df 100644 --- a/src/corelib/text/qstringview.h +++ b/src/corelib/text/qstringview.h @@ -380,6 +380,29 @@ public: Qt::SplitBehavior behavior = Qt::KeepEmptyParts) const; #endif + // QStringView <> QStringView + friend bool operator==(QStringView lhs, QStringView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::equalStrings(lhs, rhs); } + friend bool operator!=(QStringView lhs, QStringView rhs) noexcept { return !(lhs == rhs); } + friend bool operator< (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) < 0; } + friend bool operator<=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) <= 0; } + friend bool operator> (QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) > 0; } + friend bool operator>=(QStringView lhs, QStringView rhs) noexcept { return QtPrivate::compareStrings(lhs, rhs) >= 0; } + + // QStringView <> QChar + friend bool operator==(QStringView lhs, QChar rhs) noexcept { return lhs == QStringView(&rhs, 1); } + friend bool operator!=(QStringView lhs, QChar rhs) noexcept { return lhs != QStringView(&rhs, 1); } + friend bool operator< (QStringView lhs, QChar rhs) noexcept { return lhs < QStringView(&rhs, 1); } + friend bool operator<=(QStringView lhs, QChar rhs) noexcept { return lhs <= QStringView(&rhs, 1); } + friend bool operator> (QStringView lhs, QChar rhs) noexcept { return lhs > QStringView(&rhs, 1); } + friend bool operator>=(QStringView lhs, QChar rhs) noexcept { return lhs >= QStringView(&rhs, 1); } + + friend bool operator==(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) == rhs; } + friend bool operator!=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) != rhs; } + friend bool operator< (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) < rhs; } + friend bool operator<=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) <= rhs; } + friend bool operator> (QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) > rhs; } + friend bool operator>=(QChar lhs, QStringView rhs) noexcept { return QStringView(&lhs, 1) >= rhs; } + // // STL compatibility API: // |