Move the QString comparison operator as non member function

The operator== and similar should not be member of the class. This
ensure a symertry.

Indeed, consider this code
  string == string1 + string2;
  string1 + string2 == string;

The first line compile fine even if QStringBuilder is used, because
QStringBuilder will be converted to QString implicitly.

But the second line do not compile if the operator== is a member of
QString, because the implicit conversion rules do not apply.

For this reason, the symetric operators should not be declared as
member.

Change-Id: I3f7c11fab45a9133f7a424bdfcb894f97da9282b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Olivier Goffart 2012-02-25 17:00:15 +01:00 committed by Qt by Nokia
parent 67ac8d72d5
commit c06e932c73
2 changed files with 31 additions and 25 deletions

View File

@ -2139,7 +2139,8 @@ QString &QString::replace(QChar c, const QLatin1String &after, Qt::CaseSensitivi
/*!
Returns true if string \a other is equal to this string; otherwise
\relates QString
Returns true if string \a s1 is equal to string \a s2; otherwise
returns false.
The comparison is based exclusively on the numeric Unicode values of
@ -2147,12 +2148,12 @@ QString &QString::replace(QChar c, const QLatin1String &after, Qt::CaseSensitivi
expect. Consider sorting user-interface strings with
localeAwareCompare().
*/
bool QString::operator==(const QString &other) const
bool operator==(const QString &s1, const QString &s2)
{
if (d->size != other.d->size)
if (s1.d->size != s2.d->size)
return false;
return qMemEquals(d->data(), other.d->data(), d->size);
return qMemEquals(s1.d->data(), s2.d->data(), s1.d->size);
}
/*!
@ -2207,17 +2208,18 @@ bool QString::operator==(const QLatin1String &other) const
*/
/*!
Returns true if this string is lexically less than string \a
other; otherwise returns false.
\relates QString
Returns true if string \a s1 is lexically less than string
\a s2; otherwise returns false.
The comparison is based exclusively on the numeric Unicode values
of the characters and is very fast, but is not what a human would
expect. Consider sorting user-interface strings using the
QString::localeAwareCompare() function.
*/
bool QString::operator<(const QString &other) const
bool operator<(const QString &s1, const QString &s2)
{
return ucstrcmp(constData(), length(), other.constData(), other.length()) < 0;
return ucstrcmp(s1.constData(), s1.length(), s2.constData(), s2.length()) < 0;
}
/*!
@ -2268,10 +2270,11 @@ bool QString::operator<(const QLatin1String &other) const
go through QObject::tr(), for example.
*/
/*! \fn bool QString::operator<=(const QString &other) const
/*! \fn bool operator<=(const QString &s1, const QString &s2)
\relates QString
Returns true if this string is lexically less than or equal to
string \a other; otherwise returns false.
Returns true if string \a s1 is lexically less than or equal to
string \a s2; otherwise returns false.
The comparison is based exclusively on the numeric Unicode values
of the characters and is very fast, but is not what a human would
@ -2311,10 +2314,11 @@ bool QString::operator<(const QLatin1String &other) const
go through QObject::tr(), for example.
*/
/*! \fn bool QString::operator>(const QString &other) const
/*! \fn bool operator>(const QString &s1, const QString &s2)
\relates QString
Returns true if this string is lexically greater than string \a
other; otherwise returns false.
Returns true if string \a s1 is lexically greater than string \a
s2; otherwise returns false.
The comparison is based exclusively on the numeric Unicode values
of the characters and is very fast, but is not what a human would
@ -2370,10 +2374,11 @@ bool QString::operator>(const QLatin1String &other) const
for example.
*/
/*! \fn bool QString::operator>=(const QString &other) const
/*! \fn bool operator>=(const QString &s1, const QString &s2)
\relates QString
Returns true if this string is lexically greater than or equal to
string \a other; otherwise returns false.
Returns true if string \a s1 is lexically greater than or equal to
string \a s2; otherwise returns false.
The comparison is based exclusively on the numeric Unicode values
of the characters and is very fast, but is not what a human would
@ -2413,9 +2418,10 @@ bool QString::operator>(const QLatin1String &other) const
for example.
*/
/*! \fn bool QString::operator!=(const QString &other) const
/*! \fn bool operator!=(const QString &s1, const QString &s2)
\relates QString
Returns true if this string is not equal to string \a other;
Returns true if string \a s1 is not equal to string \a s2;
otherwise returns false.
The comparison is based exclusively on the numeric Unicode values

View File

@ -486,12 +486,12 @@ public:
static QString number(qulonglong, int base=10);
static QString number(double, char f='g', int prec=6);
bool operator==(const QString &s) const;
bool operator<(const QString &s) const;
inline bool operator>(const QString &s) const { return s < *this; }
inline bool operator!=(const QString &s) const { return !operator==(s); }
inline bool operator<=(const QString &s) const { return !operator>(s); }
inline bool operator>=(const QString &s) const { return !operator<(s); }
friend Q_CORE_EXPORT bool operator==(const QString &s1, const QString &s2);
friend Q_CORE_EXPORT bool operator<(const QString &s1, const QString &s2);
friend inline bool operator>(const QString &s1, const QString &s2) { return s2 < s1; }
friend inline bool operator!=(const QString &s1, const QString &s2) { return !(s1 == s2); }
friend inline bool operator<=(const QString &s1, const QString &s2) { return !(s1 > s2); }
friend inline bool operator>=(const QString &s1, const QString &s2) { return !(s1 < s2); }
bool operator==(const QLatin1String &s) const;
bool operator<(const QLatin1String &s) const;