mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 18:27:37 +02:00
Implement array comparison out of class
This commit is contained in:
@ -37,17 +37,6 @@ class ArrayData : public CollectionData {
|
||||
array->removeElement(index, resources);
|
||||
}
|
||||
|
||||
bool equals(const ArrayData&) const;
|
||||
|
||||
static bool equals(const ArrayData* lhs, const ArrayData* rhs) {
|
||||
if (lhs == rhs)
|
||||
return true;
|
||||
if (!lhs || !rhs)
|
||||
return false;
|
||||
|
||||
return lhs->equals(*rhs);
|
||||
}
|
||||
|
||||
bool copyFrom(const ArrayData& src, ResourceManager* resources);
|
||||
|
||||
static bool copy(ArrayData* dst, const ArrayData* src,
|
||||
|
@ -40,22 +40,6 @@ inline bool ArrayData::copyFrom(const ArrayData& src,
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool ArrayData::equals(const ArrayData& other) const {
|
||||
auto a = begin();
|
||||
auto b = other.begin();
|
||||
|
||||
for (;;) {
|
||||
if (!a && !b) // both ended
|
||||
return true;
|
||||
if (!a || !b) // one ended
|
||||
return false;
|
||||
if (compare(a.data(), b.data()) != COMPARE_RESULT_EQUAL)
|
||||
return false;
|
||||
++a;
|
||||
++b;
|
||||
}
|
||||
}
|
||||
|
||||
inline VariantData* ArrayData::getOrAddElement(size_t index,
|
||||
ResourceManager* resources) {
|
||||
auto it = begin();
|
||||
|
@ -83,11 +83,6 @@ class JsonArray : public detail::VariantOperators<JsonArray> {
|
||||
return detail::ArrayData::copy(data_, src.data_, resources_);
|
||||
}
|
||||
|
||||
// Compares the content of two arrays.
|
||||
FORCE_INLINE bool operator==(JsonArray rhs) const {
|
||||
return detail::ArrayData::equals(data_, rhs.data_);
|
||||
}
|
||||
|
||||
// Removes the element at the specified iterator.
|
||||
// ⚠️ Doesn't release the memory associated with the removed element.
|
||||
// https://arduinojson.org/v6/api/jsonarray/remove/
|
||||
|
@ -41,12 +41,6 @@ class JsonArrayConst : public detail::VariantOperators<JsonArrayConst> {
|
||||
// INTERNAL USE ONLY
|
||||
FORCE_INLINE JsonArrayConst(const detail::ArrayData* data) : data_(data) {}
|
||||
|
||||
// Compares the content of two arrays.
|
||||
// Returns true if the two arrays are equal.
|
||||
FORCE_INLINE bool operator==(JsonArrayConst rhs) const {
|
||||
return detail::ArrayData::equals(data_, rhs.data_);
|
||||
}
|
||||
|
||||
// Returns the element at the specified index.
|
||||
// https://arduinojson.org/v6/api/jsonarrayconst/subscript/
|
||||
FORCE_INLINE JsonVariantConst operator[](size_t index) const {
|
||||
@ -113,4 +107,27 @@ struct Converter<JsonArrayConst> : private detail::VariantAttorney {
|
||||
}
|
||||
};
|
||||
|
||||
// Compares the content of two arrays.
|
||||
// Returns true if the two arrays are equal.
|
||||
inline bool operator==(JsonArrayConst lhs, JsonArrayConst rhs) {
|
||||
if (!lhs && !rhs)
|
||||
return true;
|
||||
if (!lhs || !rhs)
|
||||
return false;
|
||||
|
||||
auto a = lhs.begin();
|
||||
auto b = rhs.begin();
|
||||
|
||||
for (;;) {
|
||||
if (a == b) // same pointer or both null
|
||||
return true;
|
||||
if (a == lhs.end() || b == rhs.end())
|
||||
return false;
|
||||
if (*a != *b)
|
||||
return false;
|
||||
++a;
|
||||
++b;
|
||||
}
|
||||
}
|
||||
|
||||
ARDUINOJSON_END_PUBLIC_NAMESPACE
|
||||
|
@ -84,7 +84,7 @@ struct ArrayComparer : ComparerBase {
|
||||
explicit ArrayComparer(const ArrayData& rhs) : rhs_(&rhs) {}
|
||||
|
||||
CompareResult visitArray(const ArrayData& lhs) {
|
||||
if (rhs_->equals(lhs))
|
||||
if (JsonArrayConst(&lhs) == JsonArrayConst(rhs_))
|
||||
return COMPARE_RESULT_EQUAL;
|
||||
else
|
||||
return COMPARE_RESULT_DIFFER;
|
||||
|
Reference in New Issue
Block a user