QChar: prepare relational operators for constexpr'ification

Make sure the relational operators are in a constexpr'able form
by removing the use of the const/non-const-overloaded unicode()
function, which in the relational operators is resolved to the
non-const version, which isn't constexpr'able.

Replaced with direct member access for op== and op< (required
making them friends) and reformulating the other operators in
terms of these two.

Since I managed to introduce a bug while doing this change,
add a simple test for QChar operators, too.

Change-Id: I69f3da849e71abc2a17152f797694950914adebc
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2014-10-15 23:02:43 +02:00
parent c9db6e52bc
commit cb93117d06
2 changed files with 39 additions and 6 deletions

View File

@ -493,6 +493,9 @@ private:
QChar(char c);
QChar(uchar c);
#endif
friend bool operator==(QChar, QChar);
friend bool operator< (QChar, QChar);
ushort ucs;
};
@ -534,12 +537,13 @@ inline bool QChar::isUpper(uint ucs4)
inline bool QChar::isTitleCase(uint ucs4)
{ return ucs4 > 127 && QChar::category(ucs4) == Letter_Titlecase; }
inline bool operator==(QChar c1, QChar c2) { return c1.unicode() == c2.unicode(); }
inline bool operator!=(QChar c1, QChar c2) { return c1.unicode() != c2.unicode(); }
inline bool operator<=(QChar c1, QChar c2) { return c1.unicode() <= c2.unicode(); }
inline bool operator>=(QChar c1, QChar c2) { return c1.unicode() >= c2.unicode(); }
inline bool operator<(QChar c1, QChar c2) { return c1.unicode() < c2.unicode(); }
inline bool operator>(QChar c1, QChar c2) { return c1.unicode() > c2.unicode(); }
inline bool operator==(QChar c1, QChar c2) { return c1.ucs == c2.ucs; }
inline bool operator< (QChar c1, QChar c2) { return c1.ucs < c2.ucs; }
inline bool operator!=(QChar c1, QChar c2) { return !operator==(c1, c2); }
inline bool operator>=(QChar c1, QChar c2) { return !operator< (c1, c2); }
inline bool operator> (QChar c1, QChar c2) { return operator< (c2, c1); }
inline bool operator<=(QChar c1, QChar c2) { return !operator< (c2, c1); }
#ifndef QT_NO_DATASTREAM
Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, QChar);

View File

@ -47,6 +47,8 @@ public slots:
void init();
void cleanup();
private slots:
void operators_data();
void operators();
void toUpper();
void toLower();
void toTitle();
@ -99,6 +101,33 @@ void tst_QChar::cleanup()
#endif
}
void tst_QChar::operators_data()
{
QTest::addColumn<QChar>("lhs");
QTest::addColumn<QChar>("rhs");
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j)
QTest::newRow(qPrintable(QString().sprintf("'\\%d' (op) '\\%d'", i, j)))
<< QChar(ushort(i)) << QChar(ushort(j));
}
}
void tst_QChar::operators()
{
QFETCH(QChar, lhs);
QFETCH(QChar, rhs);
#define CHECK(op) QCOMPARE((lhs op rhs), (lhs.unicode() op rhs.unicode()))
CHECK(==);
CHECK(!=);
CHECK(< );
CHECK(> );
CHECK(<=);
CHECK(>=);
#undef CHECK
}
void tst_QChar::toUpper()
{
QVERIFY(QChar('a').toUpper() == 'A');