mirror of
https://github.com/nlohmann/json.git
synced 2026-10-20 21:18:15 +00:00
Address the review of erase() in editable json documents
From gregmarr's review of #5630: - invalid_iterator.202 is thrown by overloads 1 and 2 only; the page said "every overload" and then excluded 3 in parentheses. - The note on links says what moves (the links after the removed element, as insert() moves them the other way) and that every iterator over the container is invalidated, not only those after the removed element. - edit.hpp compares keys with document_data::key_equals(). Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
2 files changed
+10
-9
No files matched your search
@@ -69,10 +69,10 @@ has `erase`; calling it on a read-only `basic_json_document` fails to compile (`
|
|||||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token); otherwise (an
|
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token); otherwise (an
|
||||||
object, or a primitive value the pointer's parent resolves to) throws what 1. throws.
|
object, or a primitive value the pointer's parent resolves to) throws what 1. throws.
|
||||||
|
|
||||||
Every overload also throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) ("view
|
Overloads 1. and 2. also throw [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202)
|
||||||
does not belong to this document") if `object`/`array` is a [discarded](../basic_json_view/is_discarded.md) view or a
|
("view does not belong to this document") if `object`/`array` is a [discarded](../basic_json_view/is_discarded.md)
|
||||||
view of a *different* document (overloads 1-2 only; overload 3 always starts from this document's own
|
view or a view of a *different* document. Overload 3. takes no view: it always starts from this document's own
|
||||||
[`root()`](root.md)).
|
[`root()`](root.md).
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
@@ -92,9 +92,10 @@ view of a *different* document (overloads 1-2 only; overload 3 always starts fro
|
|||||||
|
|
||||||
Like [`set`](set.md) and [`push_back`](push_back.md), `erase` never moves an element's *value*: a view still
|
Like [`set`](set.md) and [`push_back`](push_back.md), `erase` never moves an element's *value*: a view still
|
||||||
referring to a removed member or element keeps showing what it last held (see [Edits](index.md#edits)) -- it just no
|
referring to a removed member or element keeps showing what it last held (see [Edits](index.md#edits)) -- it just no
|
||||||
longer appears when `array`/`object` is read, dumped, or iterated. Removing an element of `array` (2.) does shift the
|
longer appears when `array`/`object` is read, dumped, or iterated. What does change is the container's sequence of
|
||||||
*links* to the elements after it, the same way `insert`, `set`, or `push_back` on the same array would; any iterator
|
*links* to its elements: removing an element of `array` (2.) shifts the links to the elements after it by one, as
|
||||||
already taken over `array`/`object` is invalidated by an erase, since it was walking the old layout.
|
[`insert`](insert.md) does in the other direction. *Every* iterator already taken over `array`/`object` is invalidated
|
||||||
|
by an erase, not only those at or after the removed element, since the container may now use a different sequence.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
#include <cmath> // isinf, isnan
|
#include <cmath> // isinf, isnan
|
||||||
#include <cstddef> // size_t
|
#include <cstddef> // size_t
|
||||||
#include <cstdint> // int64_t, uint8_t, uint32_t, uint64_t
|
#include <cstdint> // int64_t, uint8_t, uint32_t, uint64_t
|
||||||
#include <cstring> // memcmp, memmove
|
#include <cstring> // memmove
|
||||||
#include <limits> // numeric_limits
|
#include <limits> // numeric_limits
|
||||||
#include <string> // string, to_string
|
#include <string> // string, to_string
|
||||||
#include <type_traits> // decay, enable_if, integral_constant, is_arithmetic, is_convertible, is_floating_point, is_same, is_signed
|
#include <type_traits> // decay, enable_if, integral_constant, is_arithmetic, is_convertible, is_floating_point, is_same, is_signed
|
||||||
@@ -313,7 +313,7 @@ class editor
|
|||||||
|
|
||||||
bool key_equals(const node& k, string_view_t key) const noexcept
|
bool key_equals(const node& k, string_view_t key) const noexcept
|
||||||
{
|
{
|
||||||
return k.len == key.size() && (key.size() == 0 || std::memcmp(m_doc.str(k), key.data(), key.size()) == 0);
|
return m_doc.key_equals(k, key.data(), key.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// remove the members with this key (all, or all but the first) from an object
|
/// remove the members with this key (all, or all but the first) from an object
|
||||||
|
|||||||
Reference in new issue
Block a user