From 3d51be1372fe07ee42dede1129f65a752910cdc0 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 5 Jul 2018 17:46:02 +1000 Subject: [PATCH] json/tree: correct object and array equality operators --- json/tree.cpp | 28 ++++++++++++++++------------ json/tree.hpp | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/json/tree.cpp b/json/tree.cpp index 81c1bafd..80f53847 100644 --- a/json/tree.cpp +++ b/json/tree.cpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010-2015 Danny Robson + * Copyright 2010-2018 Danny Robson */ @@ -22,6 +22,7 @@ #include "../debug.hpp" #include "../io.hpp" +#include "../iterator.hpp" #include "../maths.hpp" #include "../stream.hpp" @@ -520,15 +521,16 @@ json::tree::object::clone (void) const //----------------------------------------------------------------------------- bool -json::tree::object::operator ==(const json::tree::object &rhs) const +json::tree::object::operator== (const json::tree::object &rhs) const { - for (auto i = rhs.m_values.begin (), j = m_values.begin (); - i != rhs.m_values.end () && j != m_values.end (); - ++i, ++j) - { - if (i->first != j->first) + if (rhs.size () != size ()) + return false; + + for (auto const &[i,j]: util::zip (m_values, rhs.m_values)) { + if (i.first != j.first) return false; - if ((*i->second) != (*j->second)) + + if (*i.second != *j.second) return false; } @@ -679,10 +681,12 @@ json::tree::array::insert (std::unique_ptr &&_value) bool json::tree::array::operator==(const json::tree::array &rhs) const { - for (auto i = rhs.m_values.begin (), j = m_values.begin (); - i != rhs.m_values.end () && j != m_values.end (); - ++i, ++j) - { if ((**i) != (**j)) return false; } + if (rhs.size () != size ()) + return false; + + for (auto const &[i,j]: util::zip (m_values, rhs.m_values)) + if (*i != *j) + return false; return true; } diff --git a/json/tree.hpp b/json/tree.hpp index d8d723a9..7f32a5f3 100644 --- a/json/tree.hpp +++ b/json/tree.hpp @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * Copyright 2010-2015 Danny Robson + * Copyright 2010-2018 Danny Robson */ #ifndef __UTIL_JSON_TREE_HPP