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>
This commit is contained in:
Marc Mutz 2016-08-17 23:33:17 +02:00
parent 13680ceb9a
commit d875a04174

View File

@ -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)