simdjson 5.0.2
Ridiculously Fast JSON
Loading...
Searching...
No Matches
dom_parser_implementation.h
1#ifndef SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
2#define SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
3
4#include "simdjson/base.h"
5#include "simdjson/error.h"
6#include <memory>
7
8namespace simdjson {
9
10namespace dom {
11class document;
12} // namespace dom
13
22enum class stage1_mode {
23 regular,
24 streaming_partial, streaming_final,
25 json_sequence_partial, json_sequence_final,
26 comma_delimited_partial, comma_delimited_final
27};
28
32inline bool is_streaming(stage1_mode mode) {
33 // performance note: it is probably faster to check that mode is different
34 // from regular than checking that it is either streaming_partial or streaming_final.
35 return (mode != stage1_mode::regular);
36 // return (mode == stage1_mode::streaming_partial || mode == stage1_mode::streaming_final);
37}
38
39namespace internal {
40
41
48class dom_parser_implementation {
49public:
50
64 simdjson_warn_unused virtual error_code parse(const uint8_t *buf, size_t len, dom::document &doc) noexcept = 0;
65
80 simdjson_warn_unused virtual error_code stage1(const uint8_t *buf, size_t len, stage1_mode streaming) noexcept = 0;
81
94 simdjson_warn_unused virtual error_code stage2(dom::document &doc) noexcept = 0;
95
107 simdjson_warn_unused virtual error_code stage2_next(dom::document &doc) noexcept = 0;
108
124 simdjson_warn_unused virtual uint8_t *parse_string(const uint8_t *src, uint8_t *dst, bool allow_replacement) const noexcept = 0;
125
140 simdjson_warn_unused virtual uint8_t *parse_wobbly_string(const uint8_t *src, uint8_t *dst) const noexcept = 0;
141
154 virtual error_code set_capacity(size_t capacity) noexcept = 0;
155
165 virtual error_code set_max_depth(size_t max_depth) noexcept = 0;
166
170 virtual ~dom_parser_implementation() = default;
171
173 uint32_t n_structural_indexes{0};
175 std::unique_ptr<uint32_t[]> structural_indexes{};
177 uint32_t next_structural_index{0};
178
184 simdjson_pure simdjson_inline size_t capacity() const noexcept;
185
191 simdjson_pure simdjson_inline size_t max_depth() const noexcept;
192
201 simdjson_warn_unused inline error_code allocate(size_t capacity, size_t max_depth) noexcept;
202
203
204protected:
210 size_t _capacity{0};
211
217 size_t _max_depth{0};
218
219public:
221 bool _number_as_string{false};
222
231 bool _unpadded{false};
232
233protected:
234
235 // Declaring these so that subclasses can use them to implement their constructors.
236 simdjson_inline dom_parser_implementation() noexcept;
237 simdjson_inline dom_parser_implementation(dom_parser_implementation &&other) noexcept;
238 simdjson_inline dom_parser_implementation &operator=(dom_parser_implementation &&other) noexcept;
239
240 simdjson_inline dom_parser_implementation(const dom_parser_implementation &) noexcept = delete;
241 simdjson_inline dom_parser_implementation &operator=(const dom_parser_implementation &other) noexcept = delete;
242}; // class dom_parser_implementation
243
244simdjson_inline dom_parser_implementation::dom_parser_implementation() noexcept = default;
245simdjson_inline dom_parser_implementation::dom_parser_implementation(dom_parser_implementation &&other) noexcept = default;
246simdjson_inline dom_parser_implementation &dom_parser_implementation::operator=(dom_parser_implementation &&other) noexcept = default;
247
248simdjson_pure simdjson_inline size_t dom_parser_implementation::capacity() const noexcept {
249 return _capacity;
250}
251
252simdjson_pure simdjson_inline size_t dom_parser_implementation::max_depth() const noexcept {
253 return _max_depth;
254}
255
256simdjson_warn_unused
257inline error_code dom_parser_implementation::allocate(size_t capacity, size_t max_depth) noexcept {
258 if (this->max_depth() != max_depth) {
259 error_code err = set_max_depth(max_depth);
260 if (err) { return err; }
261 }
262 if (_capacity != capacity) {
263 error_code err = set_capacity(capacity);
264 if (err) { return err; }
265 }
266 return SUCCESS;
267}
268
269} // namespace internal
270} // namespace simdjson
271
272#endif // SIMDJSON_INTERNAL_DOM_PARSER_IMPLEMENTATION_H
The top level simdjson namespace, containing everything the library provides.
Definition base.h:8
bool is_streaming(stage1_mode mode)
Returns true if mode == streaming_partial or mode == streaming_final.
error_code
All possible errors returned by simdjson.
Definition error.h:19
@ SUCCESS
No error.
Definition error.h:20
stage1_mode
This enum is used with the dom_parser_implementation::stage1 function.