1 #ifndef SIMDJSON_ARRAY_INL_H
2 #define SIMDJSON_ARRAY_INL_H
6 #include "simdjson/dom/base.h"
7 #include "simdjson/dom/array.h"
8 #include "simdjson/dom/element.h"
9 #include "simdjson/error-inl.h"
10 #include "simdjson/internal/tape_ref-inl.h"
19 simdjson_inline simdjson_result<dom::array>::simdjson_result() noexcept
20 : internal::simdjson_result_base<dom::array>() {}
21 simdjson_inline simdjson_result<dom::array>::simdjson_result(dom::array value) noexcept
22 : internal::simdjson_result_base<dom::array>(std::forward<dom::array>(value)) {}
23 simdjson_inline simdjson_result<dom::array>::simdjson_result(
error_code error) noexcept
24 : internal::simdjson_result_base<dom::array>(error) {}
26 #if SIMDJSON_EXCEPTIONS
28 inline dom::array::iterator simdjson_result<dom::array>::begin() const noexcept(false) {
29 if (
error()) {
throw simdjson_error(
error()); }
32 inline dom::array::iterator simdjson_result<dom::array>::end() const noexcept(false) {
33 if (
error()) {
throw simdjson_error(
error()); }
36 inline size_t simdjson_result<dom::array>::size() const noexcept(false) {
37 if (
error()) {
throw simdjson_error(
error()); }
43 inline simdjson_result<dom::element> simdjson_result<dom::array>::at_pointer(std::string_view json_pointer)
const noexcept {
44 if (error()) {
return error(); }
45 return first.at_pointer(json_pointer);
47 inline simdjson_result<dom::element> simdjson_result<dom::array>::at(
size_t index)
const noexcept {
48 if (error()) {
return error(); }
49 return first.at(index);
58 simdjson_inline
array::array(
const internal::tape_ref &_tape) noexcept : tape{_tape} {}
60 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
61 return internal::tape_ref(tape.doc, tape.json_index + 1);
64 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
65 return internal::tape_ref(tape.doc, tape.after_element() - 1);
68 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
69 return tape.scope_count();
72 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
73 return tape.matching_brace_index() - tape.json_index;
76 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
77 if(json_pointer.empty()) {
79 }
else if(json_pointer[0] !=
'/') {
82 json_pointer = json_pointer.substr(1);
88 size_t array_index = 0;
90 for (i = 0; i < json_pointer.length() && json_pointer[i] !=
'/'; i++) {
91 uint8_t digit = uint8_t(json_pointer[i] -
'0');
94 array_index = array_index*10 + digit;
104 auto child =
array(tape).
at(array_index);
110 if (i < json_pointer.length()) {
111 child = child.at_pointer(json_pointer.substr(i));
117 SIMDJSON_DEVELOPMENT_ASSERT(tape.usable());
120 if (i == index) {
return element; }
129 simdjson_inline array::iterator::iterator(
const internal::tape_ref &_tape) noexcept : tape{_tape} { }
134 tape.json_index = tape.after_element();
143 return tape.json_index != other.tape.json_index;
145 inline bool array::iterator::operator==(
const array::iterator& other)
const noexcept {
146 return tape.json_index == other.tape.json_index;
148 inline bool array::iterator::operator<(
const array::iterator& other)
const noexcept {
149 return tape.json_index < other.tape.json_index;
151 inline bool array::iterator::operator<=(
const array::iterator& other)
const noexcept {
152 return tape.json_index <= other.tape.json_index;
154 inline bool array::iterator::operator>=(
const array::iterator& other)
const noexcept {
155 return tape.json_index >= other.tape.json_index;
157 inline bool array::iterator::operator>(
const array::iterator& other)
const noexcept {
158 return tape.json_index > other.tape.json_index;
166 #include "simdjson/dom/element-inl.h"
168 #if defined(__cpp_lib_ranges)
169 static_assert(std::ranges::view<simdjson::dom::array>);
170 static_assert(std::ranges::sized_range<simdjson::dom::array>);
171 #if SIMDJSON_EXCEPTIONS
bool operator!=(const iterator &other) const noexcept
Check if these values come from the same place in the JSON.
iterator & operator++() noexcept
Get the next value.
value_type operator*() const noexcept
Get the actual value.
size_t number_of_slots() const noexcept
Get the total number of slots used by this array on the tape.
iterator end() const noexcept
One past the last array element.
size_t size() const noexcept
Get the size of the array (number of immediate children).
simdjson_result< element > at(size_t index) const noexcept
Get the value at the given index.
simdjson_result< element > at_pointer(std::string_view json_pointer) const noexcept
Get the value associated with the given JSON pointer.
simdjson_inline array() noexcept
Create a new, invalid array.
iterator begin() const noexcept
Return the first array element.
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ INCORRECT_TYPE
JSON element has a different type than user expected.
@ INDEX_OUT_OF_BOUNDS
JSON array index too large.
@ INVALID_JSON_POINTER
Invalid JSON pointer reference.
The result of a simdjson operation that could fail.
simdjson_inline error_code error() const noexcept
The error.