diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-08-17 23:33:17 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-08-26 08:25:31 +0000 |
commit | d875a0417417d1921e5a4f967d6aff72edef71c7 (patch) | |
tree | 2a84aa15d1ab8911f4c34ec15d625b4dfc7f42f9 /src | |
parent | 13680ceb9a468f824e24e448e932eed3a4ad9007 (diff) |
QtJson: simplify/add missing relational operators involving Latin1String
As noted by Mat Sutcliffe <oktal3700@gmail.com>, 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 <oktal3700@gmail.com>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/json/qjson_p.h | 50 |
1 files 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) |