From d875a0417417d1921e5a4f967d6aff72edef71c7 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 17 Aug 2016 23:33:17 +0200 Subject: QtJson: simplify/add missing relational operators involving Latin1String As noted by Mat Sutcliffe , there were no relational operators for Latin1String/QLatin1String and String/QLatin1String mixed comparisons, leading to implicit conversions from QL1S to QString in Entry::op==(QL1S). This patch fixes half of the issue, by providing the operators for Latin1String/QLatin1String. In doing so, it cleans up their definition (non-members, non-friends, delegating to existing QL1S operators where possible, passing both {Q,}Latin1String by value, as they're both Trivially Copyable and small). A follow-up patch will deal with String/QLatin1String comparisons. It will be not quite as straight-forward as this patch, since we don't, yet, have QStringView, the UTF-16 equivalent of QL1S, available. Amends a5159cc50aa0f8a57b6f736621b359a3bcecbf7e. Change-Id: I596358eb3ccf847b7680f171f9992f3fad80132c Reviewed-by: Mat Sutcliffe Reviewed-by: Lars Knoll --- src/corelib/json/qjson_p.h | 50 +++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index 8051fa0e93..8eeff01b59 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -422,26 +422,10 @@ public: return *this; } - inline bool operator ==(const QString &str) const { - return QLatin1String(d->latin1, d->length) == str; - } - inline bool operator !=(const QString &str) const { - return !operator ==(str); - } - inline bool operator >=(const QString &str) const { - return QLatin1String(d->latin1, d->length) >= str; + QLatin1String toQLatin1String() const Q_DECL_NOTHROW { + return QLatin1String(d->latin1, d->length); } - inline bool operator ==(const Latin1String &str) const { - return d->length == str.d->length && !strcmp(d->latin1, str.d->latin1); - } - inline bool operator >=(const Latin1String &str) const { - int l = qMin(d->length, str.d->length); - int val = strncmp(d->latin1, str.d->latin1, l); - if (!val) - val = d->length - str.d->length; - return val >= 0; - } inline bool operator<(const String &str) const { const qle_ushort *uc = (qle_ushort *) str.d->utf16; @@ -472,6 +456,36 @@ public: } }; +#define DEF_OP(op) \ + inline bool operator op(Latin1String lhs, Latin1String rhs) Q_DECL_NOTHROW \ + { \ + return lhs.toQLatin1String() op rhs.toQLatin1String(); \ + } \ + inline bool operator op(QLatin1String lhs, Latin1String rhs) Q_DECL_NOTHROW \ + { \ + return lhs op rhs.toQLatin1String(); \ + } \ + inline bool operator op(Latin1String lhs, QLatin1String rhs) Q_DECL_NOTHROW \ + { \ + return lhs.toQLatin1String() op rhs; \ + } \ + inline bool operator op(const QString &lhs, Latin1String rhs) Q_DECL_NOTHROW \ + { \ + return lhs op rhs.toQLatin1String(); \ + } \ + inline bool operator op(Latin1String lhs, const QString &rhs) Q_DECL_NOTHROW \ + { \ + return lhs.toQLatin1String() op rhs; \ + } \ + /*end*/ +DEF_OP(==) +DEF_OP(!=) +DEF_OP(< ) +DEF_OP(> ) +DEF_OP(<=) +DEF_OP(>=) +#undef DEF_OP + inline bool String::operator ==(const Latin1String &str) const { if ((int)d->length != (int)str.d->length) -- cgit v1.2.3