1 #ifndef SIMDJSON_DOCUMENT_INL_H
2 #define SIMDJSON_DOCUMENT_INL_H
6 #include "simdjson/dom/base.h"
7 #include "simdjson/dom/document.h"
8 #include "simdjson/dom/element-inl.h"
9 #include "simdjson/internal/tape_ref-inl.h"
10 #include "simdjson/internal/jsonformatutils.h"
21 return element(internal::tape_ref(
this, 1));
24 inline size_t document::capacity() const noexcept {
25 return allocated_capacity;
29 inline error_code document::allocate(
size_t capacity) noexcept {
33 allocated_capacity = 0;
42 size_t tape_capacity = SIMDJSON_ROUNDUP_N(capacity + 3, 64);
45 size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * capacity / 3 +
SIMDJSON_PADDING, 64);
46 string_buf.reset(
new (std::nothrow) uint8_t[string_capacity]);
47 tape.reset(
new (std::nothrow) uint64_t[tape_capacity]);
48 if(!(string_buf && tape)) {
49 allocated_capacity = 0;
56 allocated_capacity = capacity;
60 inline bool document::dump_raw_tape(std::ostream &os)
const noexcept {
61 uint32_t string_length;
63 uint64_t tape_val = tape[tape_idx];
64 uint8_t type = uint8_t(tape_val >> 56);
65 os << tape_idx <<
" : " << type;
69 how_many = size_t(tape_val & internal::JSON_VALUE_MASK);
74 os <<
"\t// pointing to " << how_many <<
" (right after last node)\n";
76 for (; tape_idx < how_many; tape_idx++) {
77 os << tape_idx <<
" : ";
78 tape_val = tape[tape_idx];
79 payload = tape_val & internal::JSON_VALUE_MASK;
80 type = uint8_t(tape_val >> 56);
84 std::memcpy(&string_length, string_buf.get() + payload,
sizeof(uint32_t));
85 os << internal::escape_json_string(std::string_view(
86 reinterpret_cast<const char *
>(string_buf.get() + payload +
sizeof(uint32_t)),
93 if (tape_idx + 1 >= how_many) {
96 os <<
"integer " <<
static_cast<int64_t
>(tape[++tape_idx]) <<
"\n";
99 if (tape_idx + 1 >= how_many) {
102 os <<
"unsigned integer " << tape[++tape_idx] <<
"\n";
106 if (tape_idx + 1 >= how_many) {
110 std::memcpy(&answer, &tape[++tape_idx],
sizeof(answer));
111 os << answer <<
'\n';
123 os <<
"{\t// pointing to next tape location " << uint32_t(payload)
124 <<
" (first node after the scope), "
125 <<
" saturated count "
126 << ((payload >> 32) & internal::JSON_COUNT_MASK)<<
"\n";
128 os <<
"}\t// pointing to previous tape location " << uint32_t(payload)
129 <<
" (start of the scope)\n";
132 os <<
"[\t// pointing to next tape location " << uint32_t(payload)
133 <<
" (first node after the scope), "
134 <<
" saturated count "
135 << ((payload >> 32) & internal::JSON_COUNT_MASK)<<
"\n";
138 os <<
"]\t// pointing to previous tape location " << uint32_t(payload)
139 <<
" (start of the scope)\n";
148 tape_val = tape[tape_idx];
149 payload = tape_val & internal::JSON_VALUE_MASK;
150 type = uint8_t(tape_val >> 56);
151 os << tape_idx <<
" : " << type <<
"\t// pointing to " << payload
152 <<
" (start root)\n";
element root() const noexcept
Get the root element of this document as a JSON array.
The top level simdjson namespace, containing everything the library provides.
error_code
All possible errors returned by simdjson.
@ MEMALLOC
Error allocating memory, most likely out of memory.
constexpr size_t SIMDJSON_PADDING
The amount of padding needed in a buffer to parse JSON.