1 namespace simdjson {
2 namespace SIMDJSON_IMPLEMENTATION {
3 namespace ondemand {
4
allocate(size_t new_capacity,size_t new_max_depth)5 simdjson_warn_unused simdjson_really_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept {
6 if (string_buf && new_capacity == capacity() && new_max_depth == max_depth()) { return SUCCESS; }
7
8 // string_capacity copied from document::allocate
9 _capacity = 0;
10 size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * new_capacity / 3 + SIMDJSON_PADDING, 64);
11 string_buf.reset(new (std::nothrow) uint8_t[string_capacity]);
12 #ifdef SIMDJSON_DEVELOPMENT_CHECKS
13 start_positions.reset(new (std::nothrow) token_position[new_max_depth]);
14 #endif
15 if (implementation) {
16 SIMDJSON_TRY( implementation->set_capacity(new_capacity) );
17 SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) );
18 } else {
19 SIMDJSON_TRY( simdjson::active_implementation->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) );
20 }
21 _capacity = new_capacity;
22 _max_depth = new_max_depth;
23 return SUCCESS;
24 }
25
iterate(padded_string_view json)26 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(padded_string_view json) & noexcept {
27 if (json.padding() < SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; }
28
29 // Allocate if needed
30 if (capacity() < json.length() || !string_buf) {
31 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
32 }
33
34 // Run stage 1.
35 SIMDJSON_TRY( implementation->stage1(reinterpret_cast<const uint8_t *>(json.data()), json.length(), false) );
36 return document::start({ reinterpret_cast<const uint8_t *>(json.data()), this });
37 }
38
iterate(const char * json,size_t len,size_t allocated)39 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const char *json, size_t len, size_t allocated) & noexcept {
40 return iterate(padded_string_view(json, len, allocated));
41 }
42
iterate(const uint8_t * json,size_t len,size_t allocated)43 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const uint8_t *json, size_t len, size_t allocated) & noexcept {
44 return iterate(padded_string_view(json, len, allocated));
45 }
46
iterate(std::string_view json,size_t allocated)47 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(std::string_view json, size_t allocated) & noexcept {
48 return iterate(padded_string_view(json, allocated));
49 }
50
iterate(const std::string & json)51 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const std::string &json) & noexcept {
52 return iterate(padded_string_view(json));
53 }
54
iterate(const simdjson_result<padded_string_view> & result)55 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const simdjson_result<padded_string_view> &result) & noexcept {
56 // We don't presently have a way to temporarily get a const T& from a simdjson_result<T> without throwing an exception
57 SIMDJSON_TRY( result.error() );
58 padded_string_view json = result.value_unsafe();
59 return iterate(json);
60 }
61
iterate(const simdjson_result<padded_string> & result)62 simdjson_warn_unused simdjson_really_inline simdjson_result<document> parser::iterate(const simdjson_result<padded_string> &result) & noexcept {
63 // We don't presently have a way to temporarily get a const T& from a simdjson_result<T> without throwing an exception
64 SIMDJSON_TRY( result.error() );
65 const padded_string &json = result.value_unsafe();
66 return iterate(json);
67 }
68
iterate_raw(padded_string_view json)69 simdjson_warn_unused simdjson_really_inline simdjson_result<json_iterator> parser::iterate_raw(padded_string_view json) & noexcept {
70 if (json.padding() < SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; }
71
72 // Allocate if needed
73 if (capacity() < json.length()) {
74 SIMDJSON_TRY( allocate(json.length(), max_depth()) );
75 }
76
77 // Run stage 1.
78 SIMDJSON_TRY( implementation->stage1(reinterpret_cast<const uint8_t *>(json.data()), json.length(), false) );
79 return json_iterator(reinterpret_cast<const uint8_t *>(json.data()), this);
80 }
81
capacity()82 simdjson_really_inline size_t parser::capacity() const noexcept {
83 return _capacity;
84 }
max_depth()85 simdjson_really_inline size_t parser::max_depth() const noexcept {
86 return _max_depth;
87 }
88
89
90 } // namespace ondemand
91 } // namespace SIMDJSON_IMPLEMENTATION
92 } // namespace simdjson
93
94 namespace simdjson {
95
simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::parser && value)96 simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser>::simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::parser &&value) noexcept
97 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser>(std::forward<SIMDJSON_IMPLEMENTATION::ondemand::parser>(value)) {}
simdjson_result(error_code error)98 simdjson_really_inline simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::parser>::simdjson_result(error_code error) noexcept
99 : implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::parser>(error) {}
100
101 } // namespace simdjson
102