diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index 3822c720c..0744861b5 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -94,7 +94,7 @@ #include // array #include // size_t -#include // memcpy +#include // memcmp, memcpy #include // numeric_limits #include // bad_alloc, operator new, placement new #include // string @@ -417,6 +417,12 @@ struct document_data return base[n.flags & node_flags::storage] + n.off; } + /// whether the string of node k (a key) is the n bytes at key + bool key_equals(const node& k, const char* key, std::size_t n) const noexcept + { + return k.len == n && (n == 0 || std::memcmp(str(k), key, n) == 0); + } + /// the node after n's subtree (containers span `next` nodes, scalars one) static NLOHMANN_VIEW_ALWAYS_INLINE const node* after(const node* n) noexcept { @@ -2438,7 +2444,7 @@ inline const node* find_member(const document_data& d, const node* object, const } for (const node* m = document_data::first_child(object); m != end; m = document_data::after(m + 1)) { - if (m->len == n && std::memcmp(d.str(*m), key, n) == 0) + if (d.key_equals(*m, key, n)) { return m; } @@ -2453,7 +2459,7 @@ inline std::size_t count_members(const document_data& d, const node* object, con const node* const end = document_data::child_end(object); for (const node* m = document_data::first_child(object); m != end; m = document_data::after(m + 1)) { - if (m->len == n && (n == 0 || std::memcmp(d.str(*m), key, n) == 0)) + if (d.key_equals(*m, key, n)) { ++result; }