QUuid: rewrite compareThreeWay() in idiomatic form

A lexicographical ordering chain with 3way-compare should call op<=>
(or it's stand-in, compareThreeWay()) exactly once per element pair in
the source ranges.

First checking for != and then with <=> means we do the check
twice.

In this case, when comparing built-in types, the optimizer will
probably fold everything for us, but code like this, at this time, has
a high chance of being the source of a CnP operation, and the target
may compare QStrings this way, so use the idiomatic form to let
copy-pasters fall into the pit of success.

Amends ef964c254c7a72bc05b1f4f0c6f270f9ad21fecd.

Change-Id: Ib8344087f23435fc58740165afecd499722d1f00
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 1e6b7e08cf5f5a36fa3007e315a9282054c1a140)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-06-13 16:48:01 +02:00 committed by Qt Cherry-pick Bot
parent 8bc09e9db3
commit d9f7efba82

View File

@ -138,14 +138,14 @@ private:
}
friend Qt::strong_ordering compareThreeWay(const QUuid &lhs, const QUuid &rhs) noexcept
{
if (lhs.variant() != rhs.variant())
return Qt::compareThreeWay(lhs.variant(), rhs.variant());
if (lhs.data1 != rhs.data1)
return Qt::compareThreeWay(lhs.data1, rhs.data1);
if (lhs.data2 != rhs.data2)
return Qt::compareThreeWay(lhs.data2, rhs.data2);
if (lhs.data3 != rhs.data3)
return Qt::compareThreeWay(lhs.data3, rhs.data3);
if (const auto c = Qt::compareThreeWay(lhs.variant(), rhs.variant()); !is_eq(c))
return c;
if (const auto c = Qt::compareThreeWay(lhs.data1, rhs.data1); !is_eq(c))
return c;
if (const auto c = Qt::compareThreeWay(lhs.data2, rhs.data2); !is_eq(c))
return c;
if (const auto c = Qt::compareThreeWay(lhs.data3, rhs.data3); !is_eq(c))
return c;
int c = std::memcmp(lhs.data4, rhs.data4, sizeof(lhs.data4));
return Qt::compareThreeWay(c, 0);