From 7d35e31efb1a58c529ffefc4a5941511024eeabc Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 25 Aug 2020 13:24:07 +0200 Subject: [PATCH] Add back QByteArray's relational operators taking QString QString still has the overloads of relational operators taking QByteArray. Add back QByteArray's relational operators taking QString for symmetry. See also the comments of d7ccd8cb4565c8643b158891c9de3187c1586dc9 for more details. [ChangeLog][EDITORIAL] Remove the changelog about QString/QByteArray operators being removed. They're back. Change-Id: I22c95e727285cf8a5ef79b3a4f9d45cb66319252 Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 84 +++++++++++++++++++ src/corelib/text/qbytearray.h | 9 ++ src/corelib/text/qstring.h | 12 +++ .../tst_qstringapisymmetry.cpp | 1 + 4 files changed, 106 insertions(+) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index bcbbdc234f0..041fb065546 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2991,6 +2991,90 @@ QDataStream &operator>>(QDataStream &in, QByteArray &ba) } #endif // QT_NO_DATASTREAM +/*! \fn bool QByteArray::operator==(const QString &str) const + + Returns \c true if this byte array is equal to the UTF-8 encoding of \a str; + otherwise returns \c false. + + The comparison is case sensitive. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. You + then need to call QString::fromUtf8(), QString::fromLatin1(), + or QString::fromLocal8Bit() explicitly if you want to convert the byte + array to a QString before doing the comparison. +*/ + +/*! \fn bool QByteArray::operator!=(const QString &str) const + + Returns \c true if this byte array is not equal to the UTF-8 encoding of \a + str; otherwise returns \c false. + + The comparison is case sensitive. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. You + then need to call QString::fromUtf8(), QString::fromLatin1(), + or QString::fromLocal8Bit() explicitly if you want to convert the byte + array to a QString before doing the comparison. +*/ + +/*! \fn bool QByteArray::operator<(const QString &str) const + + Returns \c true if this byte array is lexically less than the UTF-8 encoding + of \a str; otherwise returns \c false. + + The comparison is case sensitive. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. You + then need to call QString::fromUtf8(), QString::fromLatin1(), + or QString::fromLocal8Bit() explicitly if you want to convert the byte + array to a QString before doing the comparison. +*/ + +/*! \fn bool QByteArray::operator>(const QString &str) const + + Returns \c true if this byte array is lexically greater than the UTF-8 + encoding of \a str; otherwise returns \c false. + + The comparison is case sensitive. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. You + then need to call QString::fromUtf8(), QString::fromLatin1(), + or QString::fromLocal8Bit() explicitly if you want to convert the byte + array to a QString before doing the comparison. +*/ + +/*! \fn bool QByteArray::operator<=(const QString &str) const + + Returns \c true if this byte array is lexically less than or equal to the + UTF-8 encoding of \a str; otherwise returns \c false. + + The comparison is case sensitive. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. You + then need to call QString::fromUtf8(), QString::fromLatin1(), + or QString::fromLocal8Bit() explicitly if you want to convert the byte + array to a QString before doing the comparison. +*/ + +/*! \fn bool QByteArray::operator>=(const QString &str) const + + Returns \c true if this byte array is greater than or equal to the UTF-8 + encoding of \a str; otherwise returns \c false. + + The comparison is case sensitive. + + You can disable this operator by defining \c + QT_NO_CAST_FROM_ASCII when you compile your applications. You + then need to call QString::fromUtf8(), QString::fromLatin1(), + or QString::fromLocal8Bit() explicitly if you want to convert the byte + array to a QString before doing the comparison. +*/ + /*! \fn bool operator==(const QByteArray &a1, const QByteArray &a2) \relates QByteArray diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 68945ec6c4f..a625b1846f3 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -308,6 +308,15 @@ public: Q_REQUIRED_RESULT QByteArray repeated(int times) const; +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + inline QT_ASCII_CAST_WARN bool operator==(const QString &s2) const; + inline QT_ASCII_CAST_WARN bool operator!=(const QString &s2) const; + inline QT_ASCII_CAST_WARN bool operator<(const QString &s2) const; + inline QT_ASCII_CAST_WARN bool operator>(const QString &s2) const; + inline QT_ASCII_CAST_WARN bool operator<=(const QString &s2) const; + inline QT_ASCII_CAST_WARN bool operator>=(const QString &s2) const; +#endif + short toShort(bool *ok = nullptr, int base = 10) const; ushort toUShort(bool *ok = nullptr, int base = 10) const; int toInt(bool *ok = nullptr, int base = 10) const; diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 1e0d7f480a2..7f5e460275e 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -1259,6 +1259,18 @@ inline QT_ASCII_CAST_WARN bool QString::operator<=(const QByteArray &s) const inline QT_ASCII_CAST_WARN bool QString::operator>=(const QByteArray &s) const { return QString::compare_helper(constData(), size(), s.constData(), s.size()) >= 0; } +inline bool QByteArray::operator==(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) == 0; } +inline bool QByteArray::operator!=(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) != 0; } +inline bool QByteArray::operator<(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) > 0; } +inline bool QByteArray::operator>(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) < 0; } +inline bool QByteArray::operator<=(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) >= 0; } +inline bool QByteArray::operator>=(const QString &s) const +{ return QString::compare_helper(s.constData(), s.size(), constData(), size()) <= 0; } #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp index bd20c59dde4..563fe4c64a0 100644 --- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp +++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp @@ -200,6 +200,7 @@ private Q_SLOTS: void compare_QByteArray_char16_t_data() { compare_data(false); } void compare_QByteArray_char16_t() { compare_impl(); } void compare_QByteArray_QString_data() { compare_data(); } + void compare_QByteArray_QString() { compare_impl(); } void compare_QByteArray_QLatin1String_data() { compare_data(); } void compare_QByteArray_QLatin1String() { compare_impl(); } void compare_QByteArray_QByteArray_data() { compare_data(); }