Address the rest of the review of editable json documents

From gregmarr's review of #5630:

- alloc_nodes() gives a request of half of the next chunk or more a chunk
  of its own, so that the rest of the current chunk stays available for
  smaller requests instead of being abandoned.
- The comments on edit_state, append_text(), and reserve_moved() say that
  views stay valid across every edit (not just the first), that n is 0 for
  empty strings, and why both capacities are checked.
- The architecture page names flag bits 3, 4, and 5 (linked) separately
  and says that a discarded view has no node; erase.md rewords overload 3
  and says why it never throws invalid_iterator.202.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-20 08:52:48 +02:00
1 parent d8678c5213
commit fcf2366062
6 files changed
+86 -26

No files matched your search

+24
View File
@@ -26,6 +26,7 @@ using ptr_t = ordered_json::json_pointer;
#include <functional>
#include <iterator>
#include <limits>
#include <memory>
#include <random>
#include <set>
#include <string>
@@ -915,6 +916,29 @@ TEST_CASE("json_view edits: the size of a text arena")
}
#endif
TEST_CASE("json_view edits: the chunks of new nodes")
{
using nlohmann::detail::view::alloc_nodes;
using nlohmann::detail::view::document_data;
const std::unique_ptr<document_data, document_data::deleter> d(document_data::create(0));
// the first chunk has room for 64 nodes, the next one for 128
const nlohmann::detail::view::node* const first = alloc_nodes(*d, 10);
CHECK(d->edits->chunks.size() == 1);
// a request of half of the next chunk (128 nodes) or more gets a chunk of
// its own
const nlohmann::detail::view::node* const large = alloc_nodes(*d, 100);
CHECK(d->edits->chunks.size() == 2);
CHECK(large == d->edits->chunks.back().get());
// and the rest of the first chunk is still used
CHECK(alloc_nodes(*d, 50) == first + 10);
CHECK(d->edits->chunks.size() == 2);
// a smaller request that does not fit starts a new (doubled) chunk
CHECK(alloc_nodes(*d, 10) == d->edits->chunks.back().get());
CHECK(d->edits->chunks.size() == 3);
CHECK(d->edits->chunk_end - d->edits->chunk_cur == 128 - 10);
CHECK(d->edits->bytes == (64 + 100 + 128) * sizeof(nlohmann::detail::view::node));
}
TEST_CASE("json_view edits: replacing arrays and objects by scalars")
{
// the first assignment switches the parent to links; the later ones do