diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-02-19 18:29:48 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-03-02 00:12:53 +0100 |
commit | 42b6fdfb523f47ba711138bb299d97823e7c64d2 (patch) | |
tree | ecce14e5fe4fe52e00a231e37d082e158cb0b794 /src/corelib/text/qstring.h | |
parent | 755581e2e78f017f2c783a637ddb2ab9108b083b (diff) |
QString: use comparison helper macros - comparison with byte arrays [2/3]
Use the comparison helper macros to replace the member relational
operators for comparison with QByteArray and const char *.
As QString and QByteArray are exported, we cannot simply remove the
inline methods, so wrap them into QT_CORE_REMOVED_SINCE.
Add relational operators with QByteArrayView.
Provide more unit-tests for the comparison with the byte array types.
This enables operator<=> for QString vs byte arrays in C++20 builds.
Task-number: QTBUG-117661
Change-Id: I305343e1b6c5d78b10f2976573db4e904ba6b44b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstring.h')
-rw-r--r-- | src/corelib/text/qstring.h | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 6c64f6356a..4a07a134ca 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -855,6 +855,7 @@ public: QT_ASCII_CAST_WARN inline QString &operator+=(const QByteArray &s) { return append(QUtf8StringView(s)); } +#if QT_CORE_REMOVED_SINCE(6, 8) QT_ASCII_CAST_WARN inline bool operator==(const char *s) const; QT_ASCII_CAST_WARN inline bool operator!=(const char *s) const; QT_ASCII_CAST_WARN inline bool operator<(const char *s) const; @@ -868,20 +869,41 @@ 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; +#else + friend bool comparesEqual(const QString &lhs, QByteArrayView rhs) noexcept + { + return QString::compare_helper(lhs.constData(), lhs.size(), + rhs.constData(), rhs.size()) == 0; + } + friend Qt::strong_ordering + compareThreeWay(const QString &lhs, QByteArrayView rhs) noexcept + { + const int res = QString::compare_helper(lhs.constData(), lhs.size(), + rhs.constData(), rhs.size()); + return Qt::compareThreeWay(res, 0); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QByteArrayView, QT_ASCII_CAST_WARN) - 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 + friend bool comparesEqual(const QString &lhs, const QByteArray &rhs) noexcept + { return comparesEqual(lhs, QByteArrayView(rhs)); } + friend Qt::strong_ordering + compareThreeWay(const QString &lhs, const QByteArray &rhs) noexcept + { + return compareThreeWay(lhs, QByteArrayView(rhs)); + } + Q_DECLARE_STRONGLY_ORDERED(QString, QByteArray, QT_ASCII_CAST_WARN) + + friend bool comparesEqual(const QString &lhs, const char *rhs) noexcept + { return comparesEqual(lhs, QByteArrayView(rhs)); } + friend Qt::strong_ordering + compareThreeWay(const QString &lhs, const char *rhs) noexcept + { + return compareThreeWay(lhs, QByteArrayView(rhs)); + } + Q_DECLARE_STRONGLY_ORDERED(QString, const char *, QT_ASCII_CAST_WARN) +#endif // QT_CORE_REMOVED_SINCE(6, 8) + +#endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) typedef QChar *iterator; typedef const QChar *const_iterator; @@ -1306,6 +1328,7 @@ bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept { return indexOf(s, 0, cs) != -1; } #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) +#if QT_CORE_REMOVED_SINCE(6, 8) bool QString::operator==(const char *s) const { return QString::compare_helper(constData(), size(), s, -1) == 0; } bool QString::operator!=(const char *s) const @@ -1344,6 +1367,7 @@ bool QByteArray::operator<=(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; } bool QByteArray::operator>=(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; } +#endif // QT_CORE_REMOVED_SINCE(6, 8) #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) |