From 11ea1721786da2adb47a1d1b31a55e28aa3e0349 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 12 Nov 2021 12:08:04 -0800 Subject: [PATCH] QCborMap: merge the constFind methods into a template Change-Id: I5e52dc5b093c43a3b678fffd16b6e558c4e1f089 Reviewed-by: Edward Welbourne --- src/corelib/serialization/qcbormap.cpp | 28 +++++------------------- src/corelib/serialization/qcborvalue_p.h | 21 ++++++++++++++++++ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/corelib/serialization/qcbormap.cpp b/src/corelib/serialization/qcbormap.cpp index b178018b7c2..56068e333dc 100644 --- a/src/corelib/serialization/qcbormap.cpp +++ b/src/corelib/serialization/qcbormap.cpp @@ -932,12 +932,7 @@ QCborMap::iterator QCborMap::find(const QCborValue &key) */ QCborMap::const_iterator QCborMap::constFind(qint64 key) const { - for (qsizetype i = 0; i < 2 * size(); i += 2) { - const auto &e = d->elements.at(i); - if (e.type == QCborValue::Integer && e.value == key) - return { d.data(), i + 1 }; - } - return constEnd(); + return d ? d->findCborMapKey(key) : constEnd(); } /*! @@ -958,11 +953,7 @@ QCborMap::const_iterator QCborMap::constFind(qint64 key) const */ QCborMap::const_iterator QCborMap::constFind(QLatin1String key) const { - for (qsizetype i = 0; i < 2 * size(); i += 2) { - if (d->stringEqualsElement(i, key)) - return { d.data(), i + 1 }; - } - return constEnd(); + return d ? d->findCborMapKey(key) : constEnd(); } /*! @@ -981,13 +972,9 @@ QCborMap::const_iterator QCborMap::constFind(QLatin1String key) const remove(const QString &), contains(const QString &) value(qint64), value(QLatin1String), value(const QCborValue &) */ -QCborMap::const_iterator QCborMap::constFind(const QString & key) const +QCborMap::const_iterator QCborMap::constFind(const QString &key) const { - for (qsizetype i = 0; i < 2 * size(); i += 2) { - if (d->stringEqualsElement(i, key)) - return { d.data(), i + 1 }; - } - return constEnd(); + return d ? d->findCborMapKey(qToStringViewIgnoringNull(key)) : constEnd(); } /*! @@ -1008,12 +995,7 @@ QCborMap::const_iterator QCborMap::constFind(const QString & key) const */ QCborMap::const_iterator QCborMap::constFind(const QCborValue &key) const { - for (qsizetype i = 0; i < 2 * size(); i += 2) { - int cmp = d->compareElement(i, key); - if (cmp == 0) - return { d.data(), i + 1 }; - } - return constEnd(); + return d ? d->findCborMapKey(key) : constEnd(); } /*! diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h index e23b9fccf98..55a5dbf8717 100644 --- a/src/corelib/serialization/qcborvalue_p.h +++ b/src/corelib/serialization/qcborvalue_p.h @@ -419,6 +419,27 @@ public: elements.remove(idx); } + // doesn't apply to JSON + template QCborValueRef findCborMapKey(KeyType key) + { + qsizetype i = 0; + for ( ; i < elements.size(); i += 2) { + const auto &e = elements.at(i); + bool equals; + if constexpr (std::is_same_v, QCborValue>) { + equals = (compareElement(i, key) == 0); + } else if constexpr (std::is_integral_v) { + equals = (e.type == QCborValue::Integer && e.value == key); + } else { + // assume it's a string + equals = stringEqualsElement(i, key); + } + if (equals) + break; + } + return QCborValueRef{ this, i + 1 }; + } + #if QT_CONFIG(cborstreamreader) void decodeValueFromCbor(QCborStreamReader &reader, int remainingStackDepth); void decodeStringFromCbor(QCborStreamReader &reader);