Address the second review round of json_view element access

From gregmarr's review of #5622:

- begin()/end(): "a null value or a discarded view iterates as an empty
  range", singular like the primitive case before it.
- The class page's list of function groups has no trailing commas.
- document_data::key_equals() compares a key node with a string, for the
  key comparisons that were spelled out in each lookup.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-18 22:20:57 +02:00
1 parent 8abbc36d7d
commit 0660e6d482
5 files changed
+17 -12

No files matched your search

@@ -10,7 +10,7 @@
#include <array> // array
#include <cstddef> // size_t
#include <cstring> // memcpy
#include <cstring> // memcmp, memcpy
#include <limits> // numeric_limits
#include <new> // bad_alloc, operator new, placement new
#include <string> // string
@@ -141,6 +141,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
{
+2 -2
View File
@@ -105,7 +105,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;
}
@@ -120,7 +120,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;
}