summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2024-03-25 21:06:50 -0700
committerThiago Macieira <thiago.macieira@intel.com>2024-04-08 13:31:22 -0700
commit0aa04fe8a4cb6aa654fb50df60ba192327e29d1d (patch)
treebe68a351eaa059649426604a5cc20f2230ee836c /src
parentc0c8b1be35973fbbc3328603bd3164d73cdd2858 (diff)
CBOR: inline the contents of compare() into comparesEqual/ThreeWay()
This removes all conversions between types and compares the internals only. Change-Id: I5f663c2f9f4149af84fefffd17c034e384071aa9 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/serialization/qcborvalue.cpp70
-rw-r--r--src/corelib/serialization/qcborvalue.h3
2 files changed, 53 insertions, 20 deletions
diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp
index d6e352cb35..8e12cf64bc 100644
--- a/src/corelib/serialization/qcborvalue.cpp
+++ b/src/corelib/serialization/qcborvalue.cpp
@@ -904,12 +904,12 @@ static void writeDoubleToCbor(QCborStreamWriter &writer, double d, QCborValue::E
}
#endif // QT_CONFIG(cborstreamwriter)
-static inline int typeOrder(Element e1, Element e2)
+static inline int typeOrder(QCborValue::Type e1, QCborValue::Type e2)
{
- auto comparable = [](Element e) {
- if (e.type >= 0x10000) // see QCborValue::isTag_helper()
+ auto comparable = [](QCborValue::Type type) {
+ if (type >= 0x10000) // see QCborValue::isTag_helper()
return QCborValue::Tag;
- return e.type;
+ return type;
};
return comparable(e1) - comparable(e2);
}
@@ -1136,7 +1136,7 @@ static int compareElementNoData(const Element &e1, const Element &e2)
static int compareElementRecursive(const QCborContainerPrivate *c1, const Element &e1,
const QCborContainerPrivate *c2, const Element &e2)
{
- int cmp = typeOrder(e1, e2);
+ int cmp = typeOrder(e1.type, e2.type);
if (cmp != 0)
return cmp;
@@ -1270,7 +1270,9 @@ inline int QCborContainerPrivate::compareElement_helper(const QCborContainerPriv
bool comparesEqual(const QCborValue &lhs,
const QCborValue &rhs) noexcept
{
- return lhs.compare(rhs) == 0;
+ Element e1 = QCborContainerPrivate::elementFromValue(lhs);
+ Element e2 = QCborContainerPrivate::elementFromValue(rhs);
+ return compareElementRecursive(lhs.container, e1, rhs.container, e2) == 0;
}
/*!
@@ -1397,7 +1399,7 @@ int QCborValue::compare(const QCborValue &other) const
bool comparesEqual(const QCborArray &lhs, const QCborArray &rhs) noexcept
{
- return lhs.compare(rhs) == 0;
+ return compareContainer(lhs.d.constData(), rhs.d.constData()) == 0;
}
int QCborArray::compare(const QCborArray &other) const noexcept
@@ -1407,19 +1409,23 @@ int QCborArray::compare(const QCborArray &other) const noexcept
bool QCborArray::comparesEqual_helper(const QCborArray &lhs, const QCborValue &rhs) noexcept
{
- return lhs.compare(rhs.toArray()) == 0;
+ if (typeOrder(QCborValue::Array, rhs.type()))
+ return false;
+ return compareContainer(lhs.d.constData(), rhs.container) == 0;
}
Qt::strong_ordering
QCborArray::compareThreeWay_helper(const QCborArray &lhs, const QCborValue &rhs) noexcept
{
- int c = lhs.compare(rhs.toArray());
+ int c = typeOrder(QCborValue::Array, rhs.type());
+ if (c == 0)
+ c = compareContainer(lhs.d.constData(), rhs.container);
return Qt::compareThreeWay(c, 0);
}
bool comparesEqual(const QCborMap &lhs, const QCborMap &rhs) noexcept
{
- return lhs.compare(rhs) == 0;
+ return compareContainer(lhs.d.constData(), rhs.d.constData()) == 0;
}
int QCborMap::compare(const QCborMap &other) const noexcept
@@ -1429,13 +1435,17 @@ int QCborMap::compare(const QCborMap &other) const noexcept
bool QCborMap::comparesEqual_helper(const QCborMap &lhs, const QCborValue &rhs) noexcept
{
- return lhs.compare(rhs.toMap()) == 0;
+ if (typeOrder(QCborValue::Map, rhs.type()))
+ return false;
+ return compareContainer(lhs.d.constData(), rhs.container) == 0;
}
Qt::strong_ordering
QCborMap::compareThreeWay_helper(const QCborMap &lhs, const QCborValue &rhs) noexcept
{
- int c = lhs.compare(rhs.toMap());
+ int c = typeOrder(QCborValue::Map, rhs.type());
+ if (c == 0)
+ c = compareContainer(lhs.d.constData(), rhs.container);
return Qt::compareThreeWay(c, 0);
}
@@ -2781,50 +2791,70 @@ QString QCborValueConstRef::concreteString(QCborValueConstRef self, const QStrin
bool
QCborValueConstRef::comparesEqual_helper(QCborValueConstRef lhs, QCborValueConstRef rhs) noexcept
{
- return lhs.compare(rhs.concrete()) == 0;
+ QtCbor::Element e1 = lhs.d->elements.at(lhs.i);
+ QtCbor::Element e2 = rhs.d->elements.at(rhs.i);
+ return compareElementRecursive(lhs.d, e1, rhs.d, e2) == 0;
}
Qt::strong_ordering
QCborValueConstRef::compareThreeWay_helper(QCborValueConstRef lhs, QCborValueConstRef rhs) noexcept
{
- int c = lhs.concrete().compare(rhs.concrete());
+ QtCbor::Element e1 = lhs.d->elements.at(lhs.i);
+ QtCbor::Element e2 = rhs.d->elements.at(rhs.i);
+ int c = compareElementRecursive(lhs.d, e1, rhs.d, e2);
return Qt::compareThreeWay(c, 0);
}
bool
QCborValueConstRef::comparesEqual_helper(QCborValueConstRef lhs, const QCborValue &rhs) noexcept
{
- return lhs.compare(rhs) == 0;
+ QtCbor::Element e1 = lhs.d->elements.at(lhs.i);
+ QtCbor::Element e2 = QCborContainerPrivate::elementFromValue(rhs);
+ return compareElementRecursive(lhs.d, e1, rhs.container, e2) == 0;
}
Qt::strong_ordering
QCborValueConstRef::compareThreeWay_helper(QCborValueConstRef lhs, const QCborValue &rhs) noexcept
{
- int c = lhs.concrete().compare(rhs);
+ QtCbor::Element e1 = lhs.d->elements.at(lhs.i);
+ QtCbor::Element e2 = QCborContainerPrivate::elementFromValue(rhs);
+ int c = compareElementRecursive(lhs.d, e1, rhs.container, e2);
return Qt::compareThreeWay(c, 0);
}
bool QCborArray::comparesEqual_helper(const QCborArray &lhs, QCborValueConstRef rhs) noexcept
{
- return lhs.compare(rhs.toArray()) == 0;
+ QtCbor::Element e2 = rhs.d->elements.at(rhs.i);
+ if (typeOrder(QCborValue::Array, e2.type))
+ return false;
+ return compareContainer(lhs.d.constData(), e2.container) == 0;
}
Qt::strong_ordering
QCborArray::compareThreeWay_helper(const QCborArray &lhs, QCborValueConstRef rhs) noexcept
{
- int c = lhs.compare(rhs.toArray());
+ QtCbor::Element e2 = rhs.d->elements.at(rhs.i);
+ int c = typeOrder(QCborValue::Array, e2.type);
+ if (c == 0)
+ c = compareContainer(lhs.d.constData(), e2.container);
return Qt::compareThreeWay(c, 0);
}
bool QCborMap::comparesEqual_helper(const QCborMap &lhs, QCborValueConstRef rhs) noexcept
{
- return lhs.compare(rhs.toMap()) == 0;
+ QtCbor::Element e2 = rhs.d->elements.at(rhs.i);
+ if (typeOrder(QCborValue::Array, e2.type))
+ return false;
+ return compareContainer(lhs.d.constData(), e2.container) == 0;
}
Qt::strong_ordering
QCborMap::compareThreeWay_helper(const QCborMap &lhs, QCborValueConstRef rhs) noexcept
{
- int c = lhs.compare(rhs.toMap());
+ QtCbor::Element e2 = rhs.d->elements.at(rhs.i);
+ int c = typeOrder(QCborValue::Map, e2.type);
+ if (c == 0)
+ c = compareContainer(lhs.d.constData(), e2.container);
return Qt::compareThreeWay(c, 0);
}
diff --git a/src/corelib/serialization/qcborvalue.h b/src/corelib/serialization/qcborvalue.h
index bb2c3ac0c5..93adbec344 100644
--- a/src/corelib/serialization/qcborvalue.h
+++ b/src/corelib/serialization/qcborvalue.h
@@ -259,6 +259,9 @@ private:
}
Q_DECLARE_STRONGLY_ORDERED(QCborValue)
+ friend class QCborArray;
+ friend class QCborMap;
+ friend class QCborValueConstRef;
friend class QCborValueRef;
friend class QCborContainerPrivate;
friend class QJsonPrivate::Value;