/src/glaze/include/glaze/beve/lazy.hpp
Line | Count | Source |
1 | | // Glaze Library |
2 | | // For the license information refer to glaze.hpp |
3 | | |
4 | | #pragma once |
5 | | |
6 | | #include <string_view> |
7 | | |
8 | | #include "glaze/beve/header.hpp" |
9 | | #include "glaze/beve/read.hpp" |
10 | | #include "glaze/beve/skip.hpp" |
11 | | #include "glaze/beve/write.hpp" |
12 | | #include "glaze/util/expected.hpp" |
13 | | |
14 | | namespace glz |
15 | | { |
16 | | // Forward declarations |
17 | | template <auto Opts> |
18 | | struct lazy_beve_document; |
19 | | template <auto Opts> |
20 | | class lazy_beve_iterator; |
21 | | template <auto Opts> |
22 | | struct indexed_lazy_beve_view; |
23 | | template <auto Opts> |
24 | | class indexed_lazy_beve_iterator; |
25 | | |
26 | | // ============================================================================ |
27 | | // Helper functions for BEVE lazy parsing |
28 | | // ============================================================================ |
29 | | |
30 | | namespace detail |
31 | | { |
32 | | // Skip a BEVE value and return the new position (no context needed for position tracking). |
33 | | // |
34 | | // A failed skip leaves `it` part way through the value, and every caller here treats the |
35 | | // returned pointer as the start of the next one, so handing that position back would resolve |
36 | | // later lookups against the middle of a value. Return `end` instead: the caller's scan stops |
37 | | // and reports nothing found rather than something wrong. Nesting deeper than |
38 | | // max_recursive_depth_limit is the reachable case -- skip_value<BEVE> refuses it, which is what |
39 | | // keeps this from being a stack overflow -- so a lazy view over such a value can no longer |
40 | | // reach the members past it. Distinguishing "too deep" from "absent" needs an error channel |
41 | | // through the lazy API, which these accessors do not have. |
42 | | template <auto Opts> |
43 | | GLZ_ALWAYS_INLINE const char* skip_value_beve_lazy(const char* p, const char* end) noexcept |
44 | | { |
45 | | if (p >= end) return p; |
46 | | |
47 | | context ctx{}; |
48 | | auto it = p; |
49 | | skip_value<BEVE>::op<Opts>(ctx, it, end); |
50 | | if (bool(ctx.error)) [[unlikely]] { |
51 | | return end; |
52 | | } |
53 | | return it; |
54 | | } |
55 | | |
56 | | // Read compressed integer without modifying iterator, returns {value, bytes_consumed} |
57 | | GLZ_ALWAYS_INLINE std::pair<size_t, size_t> peek_compressed_int(const char* p, const char* end) noexcept |
58 | 0 | { |
59 | 0 | if (p >= end) return {0, 0}; |
60 | 0 |
|
61 | 0 | uint8_t header; |
62 | 0 | std::memcpy(&header, p, 1); |
63 | 0 | const uint8_t config = header & 0b000000'11; |
64 | 0 |
|
65 | 0 | switch (config) { |
66 | 0 | case 0: |
67 | 0 | return {header >> 2, 1}; |
68 | 0 | case 1: { |
69 | 0 | if (p + 2 > end) return {0, 0}; |
70 | 0 | uint16_t h; |
71 | 0 | std::memcpy(&h, p, 2); |
72 | 0 | if constexpr (std::endian::native == std::endian::big) { |
73 | 0 | h = std::byteswap(h); |
74 | 0 | } |
75 | 0 | return {h >> 2, 2}; |
76 | 0 | } |
77 | 0 | case 2: { |
78 | 0 | if (p + 4 > end) return {0, 0}; |
79 | 0 | uint32_t h; |
80 | 0 | std::memcpy(&h, p, 4); |
81 | 0 | if constexpr (std::endian::native == std::endian::big) { |
82 | 0 | h = std::byteswap(h); |
83 | 0 | } |
84 | 0 | return {h >> 2, 4}; |
85 | 0 | } |
86 | 0 | case 3: { |
87 | 0 | if constexpr (sizeof(size_t) > sizeof(uint32_t)) { |
88 | 0 | if (p + 8 > end) return {0, 0}; |
89 | 0 | uint64_t h; |
90 | 0 | std::memcpy(&h, p, 8); |
91 | 0 | if constexpr (std::endian::native == std::endian::big) { |
92 | 0 | h = std::byteswap(h); |
93 | 0 | } |
94 | 0 | return {static_cast<size_t>(h >> 2), 8}; |
95 | 0 | } |
96 | 0 | else { |
97 | 0 | return {0, 0}; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | default: |
101 | 0 | return {0, 0}; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | | // Read compressed integer and advance pointer |
106 | | GLZ_ALWAYS_INLINE size_t read_compressed_int(const char*& p, const char* end) noexcept |
107 | 0 | { |
108 | 0 | auto [value, bytes] = peek_compressed_int(p, end); |
109 | 0 | p += bytes; |
110 | 0 | return value; |
111 | 0 | } |
112 | | |
113 | | // Skip a BEVE string (tag already consumed) - returns position after string |
114 | | GLZ_ALWAYS_INLINE const char* skip_string_content(const char* p, const char* end) noexcept |
115 | 0 | { |
116 | 0 | const auto len = read_compressed_int(p, end); |
117 | 0 | if (static_cast<size_t>(end - p) < len) return end; |
118 | 0 | return p + len; |
119 | 0 | } |
120 | | |
121 | | // Get size of a BEVE number based on tag |
122 | 0 | GLZ_ALWAYS_INLINE size_t number_size_from_tag(uint8_t t) noexcept { return byte_count_lookup[t >> 5]; } |
123 | | } // namespace detail |
124 | | |
125 | | // ============================================================================ |
126 | | // lazy_beve_view - Truly lazy view into BEVE data |
127 | | // ============================================================================ |
128 | | |
129 | | /** |
130 | | * @brief A truly lazy view into BEVE data. |
131 | | * |
132 | | * No upfront processing - all navigation uses on-demand scanning. |
133 | | * |
134 | | * For objects, parse_pos_ tracks the current scan position to enable |
135 | | * efficient sequential key access (O(n) total instead of O(n²)). |
136 | | */ |
137 | | template <auto Opts = opts{}> |
138 | | struct lazy_beve_view |
139 | | { |
140 | | private: |
141 | | const lazy_beve_document<Opts>* doc_{}; |
142 | | const char* data_{}; |
143 | | mutable const char* parse_pos_{}; // Current scan position (advances on key access) |
144 | | std::string_view key_{}; |
145 | | error_code error_{error_code::none}; |
146 | | uint8_t synthetic_tag_{}; // Non-zero for typed array elements (tag info without tag byte in data) |
147 | | |
148 | | lazy_beve_view(error_code ec) noexcept : error_(ec) {} |
149 | | |
150 | | public: |
151 | | lazy_beve_view() = default; |
152 | | lazy_beve_view(const lazy_beve_document<Opts>* doc, const char* data) noexcept : doc_(doc), data_(data) {} |
153 | | |
154 | | [[nodiscard]] static lazy_beve_view make_error(error_code ec) noexcept { return lazy_beve_view{ec}; } |
155 | | |
156 | | [[nodiscard]] bool has_error() const noexcept { return error_ != error_code::none; } |
157 | | [[nodiscard]] error_code error() const noexcept { return error_; } |
158 | | |
159 | | // Whether this view's cursor still addresses a byte of the message. The scanners derive it |
160 | | // from element counts and widths read off the wire, so a container that declares more than |
161 | | // the buffer holds leaves a cursor at or past the end; the accessors below all read a byte |
162 | | // through data_, so they share this bound with the null check they already perform. |
163 | | [[nodiscard]] bool in_bounds() const noexcept; |
164 | | |
165 | | // Type checking - direct from BEVE tag byte |
166 | | [[nodiscard]] bool is_null() const noexcept |
167 | | { |
168 | | if (has_error() || !in_bounds()) return true; |
169 | | const uint8_t t = uint8_t(*data_); |
170 | | // null tag is 0, but boolean also uses low bits differently |
171 | | // null: tag == 0 |
172 | | return t == tag::null; |
173 | | } |
174 | | |
175 | | [[nodiscard]] bool is_boolean() const noexcept |
176 | | { |
177 | | if (has_error() || !in_bounds()) return false; |
178 | | const uint8_t t = uint8_t(*data_); |
179 | | // boolean: (tag & 0b0000'1111) == tag::boolean (which is 0b00001'000) |
180 | | return (t & 0b0000'1111) == tag::boolean; |
181 | | } |
182 | | |
183 | | [[nodiscard]] bool is_number() const noexcept |
184 | | { |
185 | | if (has_error() || !in_bounds()) return false; |
186 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
187 | | return t == tag::number; |
188 | | } |
189 | | |
190 | | [[nodiscard]] bool is_string() const noexcept |
191 | | { |
192 | | if (has_error() || !in_bounds()) return false; |
193 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
194 | | return t == tag::string; |
195 | | } |
196 | | |
197 | | [[nodiscard]] bool is_object() const noexcept |
198 | | { |
199 | | if (has_error() || !in_bounds()) return false; |
200 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
201 | | return t == tag::object; |
202 | | } |
203 | | |
204 | | [[nodiscard]] bool is_array() const noexcept |
205 | | { |
206 | | if (has_error() || !in_bounds()) return false; |
207 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
208 | | return t == tag::typed_array || t == tag::generic_array; |
209 | | } |
210 | | |
211 | | [[nodiscard]] bool is_typed_array() const noexcept |
212 | | { |
213 | | if (has_error() || !in_bounds()) return false; |
214 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
215 | | return t == tag::typed_array; |
216 | | } |
217 | | |
218 | | [[nodiscard]] bool is_generic_array() const noexcept |
219 | | { |
220 | | if (has_error() || !in_bounds()) return false; |
221 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
222 | | return t == tag::generic_array; |
223 | | } |
224 | | |
225 | | explicit operator bool() const noexcept { return !has_error() && data_ && !is_null(); } |
226 | | |
227 | | [[nodiscard]] const char* data() const noexcept { return data_; } |
228 | | [[nodiscard]] const char* beve_end() const noexcept; |
229 | | |
230 | | /// @brief Get the raw BEVE bytes for this value |
231 | | [[nodiscard]] std::string_view raw_beve() const noexcept |
232 | | { |
233 | | if (has_error() || !in_bounds()) return {}; |
234 | | const char* end_ptr = detail::skip_value_beve_lazy<Opts>(data_, beve_end()); |
235 | | return {data_, static_cast<size_t>(end_ptr - data_)}; |
236 | | } |
237 | | |
238 | | /// @brief Parse this value directly into a C++ type |
239 | | template <class T> |
240 | | [[nodiscard]] error_ctx read_into(T& value) const |
241 | | { |
242 | | if (has_error()) { |
243 | | return error_ctx{0, error_}; |
244 | | } |
245 | | if (!in_bounds()) { |
246 | | return error_ctx{0, error_code::unexpected_end}; |
247 | | } |
248 | | context ctx{}; |
249 | | auto it = data_; |
250 | | auto end = beve_end(); |
251 | | parse<BEVE>::op<Opts>(value, ctx, it, end); |
252 | | finalize_read_context(ctx); |
253 | | if (bool(ctx.error)) { |
254 | | return error_ctx{static_cast<size_t>(it - data_), ctx.error, ctx.custom_error_message}; |
255 | | } |
256 | | return {}; |
257 | | } |
258 | | |
259 | | template <class T> |
260 | | [[nodiscard]] expected<T, error_ctx> get() const; |
261 | | |
262 | | [[nodiscard]] lazy_beve_view operator[](size_t index) const; |
263 | | [[nodiscard]] lazy_beve_view operator[](std::string_view key) const; |
264 | | [[nodiscard]] bool contains(std::string_view key) const; |
265 | | [[nodiscard]] size_t size() const; |
266 | | [[nodiscard]] bool empty() const noexcept; |
267 | | |
268 | | // Key access for object iteration |
269 | | [[nodiscard]] std::string_view key() const noexcept { return key_; } |
270 | | |
271 | | [[nodiscard]] lazy_beve_iterator<Opts> begin() const; |
272 | | [[nodiscard]] lazy_beve_iterator<Opts> end() const; |
273 | | |
274 | | /// @brief Build an index for O(1) iteration and random access |
275 | | [[nodiscard]] indexed_lazy_beve_view<Opts> index() const; |
276 | | |
277 | | private: |
278 | | friend struct lazy_beve_document<Opts>; |
279 | | friend class lazy_beve_iterator<Opts>; |
280 | | friend struct indexed_lazy_beve_view<Opts>; |
281 | | friend class indexed_lazy_beve_iterator<Opts>; |
282 | | |
283 | | // Constructor with key for iteration |
284 | | lazy_beve_view(const lazy_beve_document<Opts>* doc, const char* data, std::string_view key) noexcept |
285 | | : doc_(doc), data_(data), key_(key) |
286 | | {} |
287 | | |
288 | | // Constructor with synthetic tag for typed array elements |
289 | | lazy_beve_view(const lazy_beve_document<Opts>* doc, const char* data, std::string_view key, |
290 | | uint8_t synthetic_tag) noexcept |
291 | | : doc_(doc), data_(data), key_(key), synthetic_tag_(synthetic_tag) |
292 | | {} |
293 | | |
294 | | void set_synthetic_tag(uint8_t tag) noexcept { synthetic_tag_ = tag; } |
295 | | |
296 | | // Helper to read numeric value directly given tag info (for typed array elements) |
297 | | template <class T> |
298 | | [[nodiscard]] expected<T, error_ctx> read_numeric_from_tag(uint8_t tag, const char* value_ptr, |
299 | | const char* end) const; |
300 | | }; |
301 | | |
302 | | // ============================================================================ |
303 | | // lazy_beve_document - Minimal container |
304 | | // ============================================================================ |
305 | | |
306 | | template <auto Opts = opts{}> |
307 | | struct lazy_beve_document |
308 | | { |
309 | | private: |
310 | | const char* beve_{}; |
311 | | size_t len_{}; |
312 | | const char* root_data_{}; |
313 | | mutable lazy_beve_view<Opts> root_view_{}; |
314 | | |
315 | | friend struct lazy_beve_view<Opts>; |
316 | | friend class lazy_beve_iterator<Opts>; |
317 | | |
318 | | template <auto O, class Buffer> |
319 | | friend expected<lazy_beve_document<O>, error_ctx> lazy_beve(Buffer&&); |
320 | | |
321 | | void init_root_view() noexcept { root_view_ = lazy_beve_view<Opts>{this, root_data_}; } |
322 | | |
323 | | public: |
324 | | lazy_beve_document() = default; |
325 | | |
326 | | lazy_beve_document(const lazy_beve_document& other) |
327 | | : beve_(other.beve_), len_(other.len_), root_data_(other.root_data_), size{this} |
328 | | { |
329 | | init_root_view(); |
330 | | root_view_.parse_pos_ = other.root_view_.parse_pos_; |
331 | | } |
332 | | |
333 | | lazy_beve_document& operator=(const lazy_beve_document& other) |
334 | | { |
335 | | if (this != &other) { |
336 | | beve_ = other.beve_; |
337 | | len_ = other.len_; |
338 | | root_data_ = other.root_data_; |
339 | | init_root_view(); |
340 | | root_view_.parse_pos_ = other.root_view_.parse_pos_; |
341 | | size = {this}; |
342 | | } |
343 | | return *this; |
344 | | } |
345 | | |
346 | | lazy_beve_document(lazy_beve_document&& other) noexcept |
347 | | : beve_(other.beve_), len_(other.len_), root_data_(other.root_data_), size{this} |
348 | | { |
349 | | init_root_view(); |
350 | | root_view_.parse_pos_ = other.root_view_.parse_pos_; |
351 | | } |
352 | | |
353 | | lazy_beve_document& operator=(lazy_beve_document&& other) noexcept |
354 | | { |
355 | | if (this != &other) { |
356 | | beve_ = other.beve_; |
357 | | len_ = other.len_; |
358 | | root_data_ = other.root_data_; |
359 | | init_root_view(); |
360 | | root_view_.parse_pos_ = other.root_view_.parse_pos_; |
361 | | size = {this}; |
362 | | } |
363 | | return *this; |
364 | | } |
365 | | |
366 | | [[nodiscard]] lazy_beve_view<Opts>& root() noexcept { return root_view_; } |
367 | | [[nodiscard]] const lazy_beve_view<Opts>& root() const noexcept { return root_view_; } |
368 | | |
369 | | [[nodiscard]] lazy_beve_view<Opts> operator[](std::string_view key) const { return root_view_[key]; } |
370 | | [[nodiscard]] lazy_beve_view<Opts> operator[](size_t index) const { return root_view_[index]; } |
371 | | |
372 | | [[nodiscard]] bool is_null() const noexcept { return !root_data_ || uint8_t(*root_data_) == tag::null; } |
373 | | |
374 | | [[nodiscard]] bool is_array() const noexcept |
375 | | { |
376 | | if (!root_data_) return false; |
377 | | const uint8_t t = uint8_t(*root_data_) & 0b00000'111; |
378 | | return t == tag::typed_array || t == tag::generic_array; |
379 | | } |
380 | | |
381 | | [[nodiscard]] bool is_object() const noexcept |
382 | | { |
383 | | if (!root_data_) return false; |
384 | | const uint8_t t = uint8_t(*root_data_) & 0b00000'111; |
385 | | return t == tag::object; |
386 | | } |
387 | | |
388 | | explicit operator bool() const noexcept { return !is_null(); } |
389 | | |
390 | | [[nodiscard]] const char* beve_data() const noexcept { return beve_; } |
391 | | [[nodiscard]] size_t beve_size() const noexcept { return len_; } |
392 | | |
393 | | void reset_parse_pos() noexcept { root_view_.parse_pos_ = nullptr; } |
394 | | |
395 | | /// @brief Size accessor proxy for getting value sizes without parsing |
396 | | /// Usage: doc.size["key"] returns the string length or element count |
397 | | struct size_accessor |
398 | | { |
399 | | const lazy_beve_document* doc; |
400 | | |
401 | | [[nodiscard]] size_t operator[](std::string_view key) const { return doc->root_view_[key].size(); } |
402 | | |
403 | | [[nodiscard]] size_t operator[](size_t index) const { return doc->root_view_[index].size(); } |
404 | | }; |
405 | | |
406 | | size_accessor size{this}; |
407 | | }; |
408 | | |
409 | | // ============================================================================ |
410 | | // lazy_beve_iterator - Forward iterator with lazy scanning |
411 | | // ============================================================================ |
412 | | |
413 | | template <auto Opts> |
414 | | class lazy_beve_iterator |
415 | | { |
416 | | private: |
417 | | const lazy_beve_document<Opts>* doc_{}; |
418 | | const char* beve_end_{}; |
419 | | const char* current_pos_{}; // Current position in iteration |
420 | | size_t remaining_count_{}; // Elements remaining |
421 | | bool is_object_{}; |
422 | | bool is_typed_array_{}; |
423 | | bool has_string_keys_{true}; // For objects: string keys (default) vs number keys |
424 | | uint8_t key_byte_count_{}; // For number-keyed objects: bytes per key |
425 | | uint8_t element_size_{}; // For typed arrays |
426 | | bool at_end_{true}; |
427 | | lazy_beve_view<Opts> current_view_{}; |
428 | | |
429 | | public: |
430 | | using iterator_category = std::forward_iterator_tag; |
431 | | using value_type = lazy_beve_view<Opts>; |
432 | | using difference_type = std::ptrdiff_t; |
433 | | using pointer = void; |
434 | | using reference = lazy_beve_view<Opts>&; |
435 | | |
436 | | lazy_beve_iterator() = default; |
437 | | |
438 | | lazy_beve_iterator(const lazy_beve_document<Opts>* doc, const char* container_start, const char* end, |
439 | | bool is_object, bool is_typed_array); |
440 | | |
441 | | reference operator*() { return current_view_; } |
442 | | const lazy_beve_view<Opts>& operator*() const { return current_view_; } |
443 | | |
444 | | lazy_beve_iterator& operator++(); |
445 | | |
446 | | lazy_beve_iterator operator++(int) |
447 | | { |
448 | | auto tmp = *this; |
449 | | ++*this; |
450 | | return tmp; |
451 | | } |
452 | | |
453 | | bool operator==(const lazy_beve_iterator& other) const { return at_end_ == other.at_end_; } |
454 | | bool operator!=(const lazy_beve_iterator& other) const { return !(*this == other); } |
455 | | |
456 | | private: |
457 | | void advance_to_current_element(); |
458 | | }; |
459 | | |
460 | | // ============================================================================ |
461 | | // indexed_lazy_beve_view - Pre-built index for O(1) access |
462 | | // ============================================================================ |
463 | | |
464 | | template <auto Opts> |
465 | | struct indexed_lazy_beve_view |
466 | | { |
467 | | private: |
468 | | const lazy_beve_document<Opts>* doc_{}; |
469 | | const char* beve_end_{}; |
470 | | std::vector<const char*> value_starts_; |
471 | | std::vector<std::string_view> keys_; |
472 | | bool is_object_{}; |
473 | | bool is_typed_array_{}; // True if indexing a typed array |
474 | | uint8_t element_tag_{}; // Synthetic element tag for typed arrays |
475 | | |
476 | | friend class indexed_lazy_beve_iterator<Opts>; |
477 | | |
478 | | public: |
479 | | indexed_lazy_beve_view() = default; |
480 | | |
481 | | [[nodiscard]] size_t size() const noexcept { return value_starts_.size(); } |
482 | | [[nodiscard]] bool empty() const noexcept { return value_starts_.empty(); } |
483 | | [[nodiscard]] bool is_object() const noexcept { return is_object_; } |
484 | | [[nodiscard]] bool is_array() const noexcept { return !is_object_; } |
485 | | [[nodiscard]] bool is_typed_array() const noexcept { return is_typed_array_; } |
486 | | |
487 | | [[nodiscard]] lazy_beve_view<Opts> operator[](size_t index) const |
488 | | { |
489 | | if (index >= value_starts_.size()) { |
490 | | return lazy_beve_view<Opts>::make_error(error_code::exceeded_static_array_size); |
491 | | } |
492 | | std::string_view key = is_object_ ? keys_[index] : std::string_view{}; |
493 | | return lazy_beve_view<Opts>{doc_, value_starts_[index], key, element_tag_}; |
494 | | } |
495 | | |
496 | | [[nodiscard]] lazy_beve_view<Opts> operator[](std::string_view key) const |
497 | | { |
498 | | if (!is_object_) { |
499 | | return lazy_beve_view<Opts>::make_error(error_code::get_wrong_type); |
500 | | } |
501 | | for (size_t i = 0; i < keys_.size(); ++i) { |
502 | | if (keys_[i] == key) { |
503 | | return lazy_beve_view<Opts>{doc_, value_starts_[i], keys_[i]}; |
504 | | } |
505 | | } |
506 | | return lazy_beve_view<Opts>::make_error(error_code::key_not_found); |
507 | | } |
508 | | |
509 | | [[nodiscard]] bool contains(std::string_view key) const |
510 | | { |
511 | | if (!is_object_) return false; |
512 | | for (const auto& k : keys_) { |
513 | | if (k == key) return true; |
514 | | } |
515 | | return false; |
516 | | } |
517 | | |
518 | | [[nodiscard]] indexed_lazy_beve_iterator<Opts> begin() const; |
519 | | [[nodiscard]] indexed_lazy_beve_iterator<Opts> end() const; |
520 | | |
521 | | private: |
522 | | friend struct lazy_beve_view<Opts>; |
523 | | |
524 | | indexed_lazy_beve_view(const lazy_beve_document<Opts>* doc, const char* beve_end, bool is_object) |
525 | | : doc_(doc), beve_end_(beve_end), is_object_(is_object) |
526 | | {} |
527 | | |
528 | | void reserve(size_t n) |
529 | | { |
530 | | value_starts_.reserve(n); |
531 | | if (is_object_) { |
532 | | keys_.reserve(n); |
533 | | } |
534 | | } |
535 | | |
536 | | void add_element(const char* value_start, std::string_view key = {}) |
537 | | { |
538 | | value_starts_.push_back(value_start); |
539 | | if (is_object_) { |
540 | | keys_.push_back(key); |
541 | | } |
542 | | } |
543 | | }; |
544 | | |
545 | | // ============================================================================ |
546 | | // indexed_lazy_beve_iterator - O(1) advancement |
547 | | // ============================================================================ |
548 | | |
549 | | template <auto Opts> |
550 | | class indexed_lazy_beve_iterator |
551 | | { |
552 | | private: |
553 | | const indexed_lazy_beve_view<Opts>* parent_{}; |
554 | | size_t index_{}; |
555 | | mutable lazy_beve_view<Opts> current_view_{}; |
556 | | |
557 | | public: |
558 | | using iterator_category = std::random_access_iterator_tag; |
559 | | using value_type = lazy_beve_view<Opts>; |
560 | | using difference_type = std::ptrdiff_t; |
561 | | using pointer = void; |
562 | | using reference = lazy_beve_view<Opts>&; |
563 | | |
564 | | indexed_lazy_beve_iterator() = default; |
565 | | indexed_lazy_beve_iterator(const indexed_lazy_beve_view<Opts>* parent, size_t index) |
566 | | : parent_(parent), index_(index) |
567 | | {} |
568 | | |
569 | | reference operator*() const |
570 | | { |
571 | | std::string_view key = parent_->is_object_ ? parent_->keys_[index_] : std::string_view{}; |
572 | | current_view_ = |
573 | | lazy_beve_view<Opts>{parent_->doc_, parent_->value_starts_[index_], key, parent_->element_tag_}; |
574 | | return current_view_; |
575 | | } |
576 | | |
577 | | lazy_beve_view<Opts>* operator->() const |
578 | | { |
579 | | operator*(); |
580 | | return ¤t_view_; |
581 | | } |
582 | | |
583 | | indexed_lazy_beve_iterator& operator++() |
584 | | { |
585 | | ++index_; |
586 | | return *this; |
587 | | } |
588 | | |
589 | | indexed_lazy_beve_iterator operator++(int) |
590 | | { |
591 | | auto tmp = *this; |
592 | | ++index_; |
593 | | return tmp; |
594 | | } |
595 | | |
596 | | indexed_lazy_beve_iterator& operator--() |
597 | | { |
598 | | --index_; |
599 | | return *this; |
600 | | } |
601 | | |
602 | | indexed_lazy_beve_iterator operator--(int) |
603 | | { |
604 | | auto tmp = *this; |
605 | | --index_; |
606 | | return tmp; |
607 | | } |
608 | | |
609 | | indexed_lazy_beve_iterator& operator+=(difference_type n) |
610 | | { |
611 | | index_ = static_cast<size_t>(static_cast<difference_type>(index_) + n); |
612 | | return *this; |
613 | | } |
614 | | |
615 | | indexed_lazy_beve_iterator& operator-=(difference_type n) |
616 | | { |
617 | | index_ = static_cast<size_t>(static_cast<difference_type>(index_) - n); |
618 | | return *this; |
619 | | } |
620 | | |
621 | | indexed_lazy_beve_iterator operator+(difference_type n) const |
622 | | { |
623 | | return {parent_, static_cast<size_t>(static_cast<difference_type>(index_) + n)}; |
624 | | } |
625 | | |
626 | | indexed_lazy_beve_iterator operator-(difference_type n) const |
627 | | { |
628 | | return {parent_, static_cast<size_t>(static_cast<difference_type>(index_) - n)}; |
629 | | } |
630 | | |
631 | | difference_type operator-(const indexed_lazy_beve_iterator& other) const |
632 | | { |
633 | | return static_cast<difference_type>(index_) - static_cast<difference_type>(other.index_); |
634 | | } |
635 | | |
636 | | reference operator[](difference_type n) const |
637 | | { |
638 | | std::string_view key = |
639 | | parent_->is_object_ ? parent_->keys_[index_ + static_cast<size_t>(n)] : std::string_view{}; |
640 | | current_view_ = |
641 | | lazy_beve_view<Opts>{parent_->doc_, parent_->value_starts_[index_ + static_cast<size_t>(n)], key}; |
642 | | return current_view_; |
643 | | } |
644 | | |
645 | | bool operator==(const indexed_lazy_beve_iterator& other) const { return index_ == other.index_; } |
646 | | bool operator!=(const indexed_lazy_beve_iterator& other) const { return index_ != other.index_; } |
647 | | bool operator<(const indexed_lazy_beve_iterator& other) const { return index_ < other.index_; } |
648 | | bool operator<=(const indexed_lazy_beve_iterator& other) const { return index_ <= other.index_; } |
649 | | bool operator>(const indexed_lazy_beve_iterator& other) const { return index_ > other.index_; } |
650 | | bool operator>=(const indexed_lazy_beve_iterator& other) const { return index_ >= other.index_; } |
651 | | |
652 | | friend indexed_lazy_beve_iterator operator+(difference_type n, const indexed_lazy_beve_iterator& it) |
653 | | { |
654 | | return it + n; |
655 | | } |
656 | | }; |
657 | | |
658 | | // ============================================================================ |
659 | | // Implementation: lazy_beve_view methods |
660 | | // ============================================================================ |
661 | | |
662 | | template <auto Opts> |
663 | | inline const char* lazy_beve_view<Opts>::beve_end() const noexcept |
664 | | { |
665 | | return doc_ ? doc_->beve_ + doc_->len_ : nullptr; |
666 | | } |
667 | | |
668 | | template <auto Opts> |
669 | | inline bool lazy_beve_view<Opts>::in_bounds() const noexcept |
670 | | { |
671 | | const char* const end = beve_end(); |
672 | | return data_ && end && data_ < end; |
673 | | } |
674 | | |
675 | | template <auto Opts> |
676 | | inline lazy_beve_view<Opts> lazy_beve_view<Opts>::operator[](size_t index) const |
677 | | { |
678 | | if (has_error()) return *this; |
679 | | if (!is_array()) return make_error(error_code::get_wrong_type); |
680 | | |
681 | | const char* end = beve_end(); |
682 | | const char* p = data_; |
683 | | const uint8_t t = uint8_t(*p); |
684 | | const uint8_t type_bits = t & 0b00000'111; |
685 | | ++p; |
686 | | |
687 | | const auto count = detail::read_compressed_int(p, end); |
688 | | if (index >= count) { |
689 | | return make_error(error_code::exceeded_static_array_size); |
690 | | } |
691 | | |
692 | | if (type_bits == tag::generic_array) { |
693 | | // Generic array - each element has its own tag |
694 | | for (size_t i = 0; i < index; ++i) { |
695 | | if (p >= end) [[unlikely]] { |
696 | | return make_error(error_code::unexpected_end); |
697 | | } |
698 | | p = detail::skip_value_beve_lazy<Opts>(p, end); |
699 | | } |
700 | | if (p >= end) [[unlikely]] { |
701 | | // The count promised this element but the buffer ended before it. |
702 | | return make_error(error_code::unexpected_end); |
703 | | } |
704 | | return {doc_, p}; |
705 | | } |
706 | | else { |
707 | | // Typed array - homogeneous elements |
708 | | const uint8_t element_type = (t & 0b000'11'000) >> 3; |
709 | | |
710 | | if (element_type == 3) { |
711 | | // Bool or string array |
712 | | const bool is_string = (t & 0b00'1'00'000) >> 5; |
713 | | if (is_string) { |
714 | | // String array - variable length elements |
715 | | for (size_t i = 0; i < index; ++i) { |
716 | | if (p >= end) [[unlikely]] { |
717 | | return make_error(error_code::unexpected_end); |
718 | | } |
719 | | const auto len = detail::read_compressed_int(p, end); |
720 | | if (static_cast<size_t>(end - p) < len) [[unlikely]] { |
721 | | return make_error(error_code::unexpected_end); |
722 | | } |
723 | | p += len; |
724 | | } |
725 | | if (p >= end) [[unlikely]] { |
726 | | return make_error(error_code::unexpected_end); |
727 | | } |
728 | | // Return view with synthetic string tag |
729 | | return {doc_, p, {}, tag::string}; |
730 | | } |
731 | | else { |
732 | | // Boolean array - packed bits, can't easily index |
733 | | // For now, return error for direct indexing of bool arrays |
734 | | return make_error(error_code::get_wrong_type); |
735 | | } |
736 | | } |
737 | | else { |
738 | | // Numeric typed array - fixed size elements |
739 | | const size_t elem_size = byte_count_lookup[t >> 5]; |
740 | | // The stride comes off the wire, so index * elem_size can reach past the buffer. |
741 | | // Divide the bytes remaining instead of multiplying the offset: the product overflows |
742 | | // a 32-bit size_t and would wrap the cursor back inside the buffer. |
743 | | if (static_cast<size_t>(end - p) / elem_size <= index) [[unlikely]] { |
744 | | return make_error(error_code::unexpected_end); |
745 | | } |
746 | | p += index * elem_size; |
747 | | // Return view with synthetic numeric tag |
748 | | const uint8_t synthetic_tag = tag::number | (t & 0b11111000); |
749 | | return {doc_, p, {}, synthetic_tag}; |
750 | | } |
751 | | } |
752 | | } |
753 | | |
754 | | template <auto Opts> |
755 | | inline lazy_beve_view<Opts> lazy_beve_view<Opts>::operator[](std::string_view key) const |
756 | | { |
757 | | if (has_error()) return *this; |
758 | | if (!is_object()) return make_error(error_code::get_wrong_type); |
759 | | |
760 | | const char* end = beve_end(); |
761 | | const char* p = data_; |
762 | | const uint8_t t = uint8_t(*p); |
763 | | ++p; |
764 | | |
765 | | // Check for string-keyed object (key_type bits should be 0 for string keys) |
766 | | const uint8_t key_type = t & 0b000'11'000; |
767 | | if (key_type != 0) { |
768 | | // Number-keyed object - string lookup not supported |
769 | | return make_error(error_code::get_wrong_type); |
770 | | } |
771 | | |
772 | | const auto n_keys = detail::read_compressed_int(p, end); |
773 | | |
774 | | // Determine search start position for progressive scanning |
775 | | const char* search_start = p; |
776 | | size_t start_index = 0; |
777 | | |
778 | | if (parse_pos_ && parse_pos_ > data_) { |
779 | | // Skip past the last found value |
780 | | const char* skip_from = parse_pos_; |
781 | | skip_from = detail::skip_value_beve_lazy<Opts>(skip_from, end); |
782 | | search_start = skip_from; |
783 | | |
784 | | // Count how many keys we've passed |
785 | | const char* counter = p; |
786 | | while (counter < search_start && start_index < n_keys) { |
787 | | const auto key_len = detail::read_compressed_int(counter, end); |
788 | | if (static_cast<size_t>(end - counter) < key_len) [[unlikely]] { |
789 | | break; |
790 | | } |
791 | | counter += key_len; |
792 | | counter = detail::skip_value_beve_lazy<Opts>(counter, end); |
793 | | ++start_index; |
794 | | } |
795 | | } |
796 | | |
797 | | // Forward pass: search from current position |
798 | | const char* iter = search_start; |
799 | | for (size_t i = start_index; i < n_keys; ++i) { |
800 | | if (iter >= end) [[unlikely]] { |
801 | | // The key count is wire data and outruns the buffer; at the end there is no key to |
802 | | // read and the scan cannot advance, so it has to stop here rather than spin out the |
803 | | // declared count. |
804 | | return make_error(error_code::unexpected_end); |
805 | | } |
806 | | const auto key_len = detail::read_compressed_int(iter, end); |
807 | | if (static_cast<size_t>(end - iter) < key_len) [[unlikely]] { |
808 | | return make_error(error_code::unexpected_end); |
809 | | } |
810 | | std::string_view current_key{iter, key_len}; |
811 | | iter += key_len; |
812 | | |
813 | | if (current_key == key) { |
814 | | if (iter >= end) [[unlikely]] { |
815 | | // The key ran to the end of the buffer, so its value is not in the message. |
816 | | // Report before storing parse_pos_ so a truncated match leaves no scan state. |
817 | | return make_error(error_code::unexpected_end); |
818 | | } |
819 | | parse_pos_ = iter; |
820 | | return {doc_, iter}; |
821 | | } |
822 | | |
823 | | iter = detail::skip_value_beve_lazy<Opts>(iter, end); |
824 | | } |
825 | | |
826 | | // Wrap-around pass: search from beginning to where we started |
827 | | if (start_index > 0) { |
828 | | iter = p; |
829 | | for (size_t i = 0; i < start_index; ++i) { |
830 | | if (iter >= end) [[unlikely]] { |
831 | | return make_error(error_code::unexpected_end); |
832 | | } |
833 | | const auto key_len = detail::read_compressed_int(iter, end); |
834 | | if (static_cast<size_t>(end - iter) < key_len) [[unlikely]] { |
835 | | return make_error(error_code::unexpected_end); |
836 | | } |
837 | | std::string_view current_key{iter, key_len}; |
838 | | iter += key_len; |
839 | | |
840 | | if (current_key == key) { |
841 | | if (iter >= end) [[unlikely]] { |
842 | | return make_error(error_code::unexpected_end); |
843 | | } |
844 | | parse_pos_ = iter; |
845 | | return {doc_, iter}; |
846 | | } |
847 | | |
848 | | iter = detail::skip_value_beve_lazy<Opts>(iter, end); |
849 | | } |
850 | | } |
851 | | |
852 | | return make_error(error_code::key_not_found); |
853 | | } |
854 | | |
855 | | template <auto Opts> |
856 | | inline bool lazy_beve_view<Opts>::contains(std::string_view key) const |
857 | | { |
858 | | auto result = (*this)[key]; |
859 | | return !result.has_error(); |
860 | | } |
861 | | |
862 | | template <auto Opts> |
863 | | inline size_t lazy_beve_view<Opts>::size() const |
864 | | { |
865 | | if (has_error() || !in_bounds()) return 0; |
866 | | |
867 | | // Handle synthetic tags (typed array elements) |
868 | | if (synthetic_tag_ != 0) { |
869 | | const uint8_t base_type = synthetic_tag_ & 0b00000'111; |
870 | | if (base_type == tag::string) { |
871 | | // String element in typed string array - length prefixed |
872 | | auto [len, bytes] = detail::peek_compressed_int(data_, beve_end()); |
873 | | return len; |
874 | | } |
875 | | return 0; |
876 | | } |
877 | | |
878 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
879 | | |
880 | | if (t == tag::string) { |
881 | | const char* p = data_ + 1; // Skip tag |
882 | | auto [len, bytes] = detail::peek_compressed_int(p, beve_end()); |
883 | | return len; |
884 | | } |
885 | | |
886 | | if (t == tag::object || t == tag::typed_array || t == tag::generic_array) { |
887 | | const char* p = data_ + 1; |
888 | | return detail::read_compressed_int(p, beve_end()); |
889 | | } |
890 | | |
891 | | return 0; |
892 | | } |
893 | | |
894 | | template <auto Opts> |
895 | | inline bool lazy_beve_view<Opts>::empty() const noexcept |
896 | | { |
897 | | if (has_error() || !in_bounds()) return true; |
898 | | if (is_null()) return true; |
899 | | if (!is_array() && !is_object()) return false; |
900 | | |
901 | | const char* p = data_ + 1; |
902 | | const auto count = detail::read_compressed_int(p, beve_end()); |
903 | | return count == 0; |
904 | | } |
905 | | |
906 | | // ============================================================================ |
907 | | // lazy_beve_iterator implementation |
908 | | // ============================================================================ |
909 | | |
910 | | template <auto Opts> |
911 | | inline lazy_beve_iterator<Opts>::lazy_beve_iterator(const lazy_beve_document<Opts>* doc, const char* container_start, |
912 | | const char* end, bool is_object, bool is_typed_array) |
913 | | : doc_(doc), beve_end_(end), is_object_(is_object), is_typed_array_(is_typed_array), at_end_(false) |
914 | | { |
915 | | const char* p = container_start; |
916 | | const uint8_t t = uint8_t(*p); |
917 | | ++p; |
918 | | |
919 | | remaining_count_ = detail::read_compressed_int(p, end); |
920 | | |
921 | | if (remaining_count_ == 0) { |
922 | | at_end_ = true; |
923 | | return; |
924 | | } |
925 | | |
926 | | if (is_object_) { |
927 | | // Check key type from bits 3-4 of tag |
928 | | const uint8_t key_type_bits = t & 0b000'11'000; |
929 | | has_string_keys_ = (key_type_bits == 0); |
930 | | if (!has_string_keys_) { |
931 | | key_byte_count_ = static_cast<uint8_t>(byte_count_lookup[t >> 5]); |
932 | | } |
933 | | } |
934 | | |
935 | | if (is_typed_array_) { |
936 | | // For typed arrays, calculate element size |
937 | | const uint8_t element_type = (t & 0b000'11'000) >> 3; |
938 | | if (element_type < 3) { |
939 | | element_size_ = static_cast<uint8_t>(byte_count_lookup[t >> 5]); |
940 | | } |
941 | | else { |
942 | | // Bool or string array - variable handling needed |
943 | | element_size_ = 0; |
944 | | } |
945 | | } |
946 | | |
947 | | current_pos_ = p; |
948 | | advance_to_current_element(); |
949 | | } |
950 | | |
951 | | template <auto Opts> |
952 | | inline void lazy_beve_iterator<Opts>::advance_to_current_element() |
953 | | { |
954 | | std::string_view key{}; |
955 | | |
956 | | if (is_object_) { |
957 | | if (has_string_keys_) { |
958 | | // String key: length prefix + string data |
959 | | const auto key_len = detail::read_compressed_int(current_pos_, beve_end_); |
960 | | if (static_cast<size_t>(beve_end_ - current_pos_) < key_len) [[unlikely]] { |
961 | | // Declared key length runs past the buffer; stop rather than |
962 | | // handing back a view over out-of-bounds memory. |
963 | | at_end_ = true; |
964 | | current_view_ = lazy_beve_view<Opts>::make_error(error_code::unexpected_end); |
965 | | return; |
966 | | } |
967 | | key = std::string_view{current_pos_, key_len}; |
968 | | current_pos_ += key_len; |
969 | | } |
970 | | else { |
971 | | // Number key: just raw bytes (no key view for number keys) |
972 | | if (static_cast<size_t>(beve_end_ - current_pos_) < key_byte_count_) [[unlikely]] { |
973 | | at_end_ = true; |
974 | | current_view_ = lazy_beve_view<Opts>::make_error(error_code::unexpected_end); |
975 | | return; |
976 | | } |
977 | | current_pos_ += key_byte_count_; |
978 | | } |
979 | | } |
980 | | |
981 | | if (current_pos_ >= beve_end_) [[unlikely]] { |
982 | | // The count promised another element but the buffer ended first; stop rather than |
983 | | // handing back a view whose first byte is not in the message. |
984 | | at_end_ = true; |
985 | | current_view_ = lazy_beve_view<Opts>::make_error(error_code::unexpected_end); |
986 | | return; |
987 | | } |
988 | | |
989 | | current_view_ = lazy_beve_view<Opts>{doc_, current_pos_, key}; |
990 | | } |
991 | | |
992 | | template <auto Opts> |
993 | | inline lazy_beve_iterator<Opts>& lazy_beve_iterator<Opts>::operator++() |
994 | | { |
995 | | if (at_end_) return *this; |
996 | | |
997 | | --remaining_count_; |
998 | | if (remaining_count_ == 0) { |
999 | | at_end_ = true; |
1000 | | return *this; |
1001 | | } |
1002 | | |
1003 | | // Skip current value |
1004 | | if (is_typed_array_ && element_size_ > 0) { |
1005 | | if (static_cast<size_t>(beve_end_ - current_pos_) < element_size_) [[unlikely]] { |
1006 | | at_end_ = true; |
1007 | | current_view_ = lazy_beve_view<Opts>::make_error(error_code::unexpected_end); |
1008 | | return *this; |
1009 | | } |
1010 | | current_pos_ += element_size_; |
1011 | | } |
1012 | | else { |
1013 | | current_pos_ = detail::skip_value_beve_lazy<Opts>(current_pos_, beve_end_); |
1014 | | } |
1015 | | |
1016 | | advance_to_current_element(); |
1017 | | return *this; |
1018 | | } |
1019 | | |
1020 | | template <auto Opts> |
1021 | | inline lazy_beve_iterator<Opts> lazy_beve_view<Opts>::begin() const |
1022 | | { |
1023 | | if (has_error() || !in_bounds()) return end(); |
1024 | | if (!is_array() && !is_object()) return end(); |
1025 | | |
1026 | | const uint8_t t = uint8_t(*data_) & 0b00000'111; |
1027 | | const bool is_typed = (t == tag::typed_array); |
1028 | | return lazy_beve_iterator<Opts>{doc_, data_, beve_end(), is_object(), is_typed}; |
1029 | | } |
1030 | | |
1031 | | template <auto Opts> |
1032 | | inline lazy_beve_iterator<Opts> lazy_beve_view<Opts>::end() const |
1033 | | { |
1034 | | return lazy_beve_iterator<Opts>{}; |
1035 | | } |
1036 | | |
1037 | | // ============================================================================ |
1038 | | // indexed_lazy_beve_view implementation |
1039 | | // ============================================================================ |
1040 | | |
1041 | | template <auto Opts> |
1042 | | inline indexed_lazy_beve_iterator<Opts> indexed_lazy_beve_view<Opts>::begin() const |
1043 | | { |
1044 | | return indexed_lazy_beve_iterator<Opts>{this, 0}; |
1045 | | } |
1046 | | |
1047 | | template <auto Opts> |
1048 | | inline indexed_lazy_beve_iterator<Opts> indexed_lazy_beve_view<Opts>::end() const |
1049 | | { |
1050 | | return indexed_lazy_beve_iterator<Opts>{this, value_starts_.size()}; |
1051 | | } |
1052 | | |
1053 | | template <auto Opts> |
1054 | | inline indexed_lazy_beve_view<Opts> lazy_beve_view<Opts>::index() const |
1055 | | { |
1056 | | if (has_error() || !in_bounds() || (!is_array() && !is_object())) { |
1057 | | return indexed_lazy_beve_view<Opts>{}; |
1058 | | } |
1059 | | |
1060 | | const char* end = beve_end(); |
1061 | | indexed_lazy_beve_view<Opts> result{doc_, end, is_object()}; |
1062 | | |
1063 | | const char* p = data_; |
1064 | | const uint8_t t = uint8_t(*p); |
1065 | | const uint8_t type_bits = t & 0b00000'111; |
1066 | | ++p; |
1067 | | |
1068 | | const auto count = detail::read_compressed_int(p, end); |
1069 | | if (count == 0) return result; |
1070 | | |
1071 | | // Every element occupies at least one byte, so the bytes remaining cap how many there can |
1072 | | // be. The count is unvalidated wire data and would otherwise reserve for billions. |
1073 | | result.reserve(count < static_cast<size_t>(end - p) ? count : static_cast<size_t>(end - p)); |
1074 | | |
1075 | | if (type_bits == tag::object) { |
1076 | | // Check key type from bits 3-4 of tag |
1077 | | const uint8_t key_type_bits = t & 0b000'11'000; |
1078 | | const bool has_string_keys = (key_type_bits == 0); |
1079 | | |
1080 | | if (has_string_keys) { |
1081 | | // Object with string keys |
1082 | | for (size_t i = 0; i < count; ++i) { |
1083 | | const auto key_len = detail::read_compressed_int(p, end); |
1084 | | if (static_cast<size_t>(end - p) < key_len) [[unlikely]] { |
1085 | | // Truncated key; stop indexing instead of storing an |
1086 | | // out-of-bounds key view. |
1087 | | break; |
1088 | | } |
1089 | | std::string_view key{p, key_len}; |
1090 | | p += key_len; |
1091 | | |
1092 | | if (p >= end) [[unlikely]] { |
1093 | | // The key ran to the end of the buffer, so its value is not there. |
1094 | | break; |
1095 | | } |
1096 | | result.add_element(p, key); |
1097 | | p = detail::skip_value_beve_lazy<Opts>(p, end); |
1098 | | } |
1099 | | } |
1100 | | else { |
1101 | | // Object with number keys |
1102 | | const size_t key_size = byte_count_lookup[t >> 5]; |
1103 | | for (size_t i = 0; i < count; ++i) { |
1104 | | if (static_cast<size_t>(end - p) <= key_size) [[unlikely]] { |
1105 | | // Not enough bytes for this key and the value that follows it. |
1106 | | break; |
1107 | | } |
1108 | | p += key_size; // Skip the number key |
1109 | | result.add_element(p); // No key stored for number keys |
1110 | | p = detail::skip_value_beve_lazy<Opts>(p, end); |
1111 | | } |
1112 | | } |
1113 | | } |
1114 | | else if (type_bits == tag::generic_array) { |
1115 | | // Generic array |
1116 | | for (size_t i = 0; i < count; ++i) { |
1117 | | if (p >= end) [[unlikely]] { |
1118 | | break; |
1119 | | } |
1120 | | result.add_element(p); |
1121 | | p = detail::skip_value_beve_lazy<Opts>(p, end); |
1122 | | } |
1123 | | } |
1124 | | else if (type_bits == tag::typed_array) { |
1125 | | // Typed array |
1126 | | const uint8_t element_type = (t & 0b000'11'000) >> 3; |
1127 | | result.is_typed_array_ = true; |
1128 | | |
1129 | | if (element_type < 3) { |
1130 | | // Numeric typed array - fixed size elements |
1131 | | // Synthesize element tag: number base type + same type/width bits from array tag |
1132 | | result.element_tag_ = tag::number | (t & 0b11111000); |
1133 | | const size_t elem_size = byte_count_lookup[t >> 5]; |
1134 | | for (size_t i = 0; i < count; ++i) { |
1135 | | if (static_cast<size_t>(end - p) < elem_size) [[unlikely]] { |
1136 | | break; |
1137 | | } |
1138 | | result.add_element(p); |
1139 | | p += elem_size; |
1140 | | } |
1141 | | } |
1142 | | else { |
1143 | | // Bool or string array |
1144 | | const bool is_string_arr = (t & 0b00'1'00'000) >> 5; |
1145 | | if (is_string_arr) { |
1146 | | // String array - elements are length-prefixed strings without tag |
1147 | | result.element_tag_ = tag::string; |
1148 | | for (size_t i = 0; i < count; ++i) { |
1149 | | if (p >= end) [[unlikely]] { |
1150 | | break; |
1151 | | } |
1152 | | result.add_element(p); |
1153 | | const auto len = detail::read_compressed_int(p, end); |
1154 | | if (static_cast<size_t>(end - p) < len) [[unlikely]] { |
1155 | | break; |
1156 | | } |
1157 | | p += len; |
1158 | | } |
1159 | | } |
1160 | | else { |
1161 | | // Boolean array - packed bits, index each bit position conceptually |
1162 | | // For simplicity, we don't index individual bools |
1163 | | // Users should use size() and iteration instead |
1164 | | } |
1165 | | } |
1166 | | } |
1167 | | |
1168 | | return result; |
1169 | | } |
1170 | | |
1171 | | // ============================================================================ |
1172 | | // lazy_beve_view::get<T>() implementation |
1173 | | // ============================================================================ |
1174 | | |
1175 | | template <auto Opts> |
1176 | | template <class T> |
1177 | | [[nodiscard]] inline expected<T, error_ctx> lazy_beve_view<Opts>::get() const |
1178 | | { |
1179 | | if (has_error()) { |
1180 | | return unexpected(error_ctx{0, error_}); |
1181 | | } |
1182 | | if (!in_bounds()) { |
1183 | | // Both forms below start at data_: the tag byte of a normal value, the first byte of a |
1184 | | // typed array element. Neither exists unless the cursor is inside the message. |
1185 | | return unexpected(error_ctx{0, error_code::unexpected_end}); |
1186 | | } |
1187 | | |
1188 | | const char* end = beve_end(); |
1189 | | |
1190 | | // For typed array elements, synthetic_tag_ is non-zero and data_ points directly to value |
1191 | | // For normal values, tag is read from data_ and value starts at data_+1 |
1192 | | const bool has_synthetic_tag = (synthetic_tag_ != 0); |
1193 | | const uint8_t tag = has_synthetic_tag ? synthetic_tag_ : uint8_t(*data_); |
1194 | | const char* value_ptr = has_synthetic_tag ? data_ : data_ + 1; |
1195 | | |
1196 | | if constexpr (std::is_same_v<T, bool>) { |
1197 | | if (has_synthetic_tag) { |
1198 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); // Bool arrays not supported this way |
1199 | | } |
1200 | | if (!is_boolean()) { |
1201 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); |
1202 | | } |
1203 | | // Boolean value is in bit 4 of the tag |
1204 | | return (uint8_t(*data_) >> 4) & 1; |
1205 | | } |
1206 | | else if constexpr (std::is_same_v<T, std::nullptr_t>) { |
1207 | | if (has_synthetic_tag || !is_null()) { |
1208 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); |
1209 | | } |
1210 | | return nullptr; |
1211 | | } |
1212 | | else if constexpr (std::is_same_v<T, std::string>) { |
1213 | | const uint8_t base_type = tag & 0b00000'111; |
1214 | | if (base_type != tag::string) { |
1215 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); |
1216 | | } |
1217 | | // For typed string arrays, value_ptr points to length-prefixed string (no tag) |
1218 | | // For normal strings, we already skipped the tag |
1219 | | const char* p = value_ptr; |
1220 | | const auto len = detail::read_compressed_int(p, end); |
1221 | | if (static_cast<size_t>(end - p) < len) { |
1222 | | return unexpected(error_ctx{0, error_code::unexpected_end}); |
1223 | | } |
1224 | | return std::string{p, len}; |
1225 | | } |
1226 | | else if constexpr (std::is_same_v<T, std::string_view>) { |
1227 | | const uint8_t base_type = tag & 0b00000'111; |
1228 | | if (base_type != tag::string) { |
1229 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); |
1230 | | } |
1231 | | const char* p = value_ptr; |
1232 | | const auto len = detail::read_compressed_int(p, end); |
1233 | | if (static_cast<size_t>(end - p) < len) { |
1234 | | return unexpected(error_ctx{0, error_code::unexpected_end}); |
1235 | | } |
1236 | | return std::string_view{p, len}; |
1237 | | } |
1238 | | else if constexpr (std::is_arithmetic_v<T> && !std::is_same_v<T, bool>) { |
1239 | | const uint8_t base_type = tag & 0b00000'111; |
1240 | | if (base_type != tag::number) { |
1241 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); |
1242 | | } |
1243 | | |
1244 | | if (has_synthetic_tag) { |
1245 | | // Typed array element - read directly from value_ptr |
1246 | | return read_numeric_from_tag<T>(tag, value_ptr, end); |
1247 | | } |
1248 | | else { |
1249 | | // Normal value - use standard BEVE parsing |
1250 | | T value{}; |
1251 | | auto it = data_; |
1252 | | context ctx{}; |
1253 | | parse<BEVE>::op<Opts>(value, ctx, it, end); |
1254 | | if (bool(ctx.error)) { |
1255 | | return unexpected(error_ctx{0, ctx.error}); |
1256 | | } |
1257 | | return value; |
1258 | | } |
1259 | | } |
1260 | | else { |
1261 | | static_assert(false_v<T>, "Unsupported type for lazy_beve_view::get<T>()"); |
1262 | | } |
1263 | | } |
1264 | | |
1265 | | // Helper to read numeric value directly given tag info (for typed array elements) |
1266 | | template <auto Opts> |
1267 | | template <class T> |
1268 | | [[nodiscard]] inline expected<T, error_ctx> lazy_beve_view<Opts>::read_numeric_from_tag(uint8_t tag, |
1269 | | const char* value_ptr, |
1270 | | const char* end) const |
1271 | | { |
1272 | | const uint8_t type = (tag & 0b000'11'000) >> 3; // 0=float, 1=signed, 2=unsigned |
1273 | | const size_t byte_count = byte_count_lookup[tag >> 5]; |
1274 | | |
1275 | | if (static_cast<size_t>(end - value_ptr) < byte_count) { |
1276 | | return unexpected(error_ctx{0, error_code::unexpected_end}); |
1277 | | } |
1278 | | |
1279 | | // Typed array elements are little-endian on the wire |
1280 | | const auto load = [value_ptr]<class V>() { |
1281 | | V v; |
1282 | | std::memcpy(&v, value_ptr, sizeof(V)); |
1283 | | if constexpr (std::endian::native == std::endian::big) { |
1284 | | byteswap_le(v); |
1285 | | } |
1286 | | return v; |
1287 | | }; |
1288 | | |
1289 | | if (type == 0) { |
1290 | | // Floating point. from<BEVE, num_t> refuses a float to integral conversion, and a |
1291 | | // static_cast of an out-of-range double to an integer is undefined behavior, so an |
1292 | | // integral get<T>() on a float element reports the error the numeric reader would. |
1293 | | if constexpr (std::integral<T>) { |
1294 | | return unexpected(error_ctx{0, error_code::syntax_error}); |
1295 | | } |
1296 | | else { |
1297 | | if (byte_count == 4) { |
1298 | | return static_cast<T>(load.template operator()<float>()); |
1299 | | } |
1300 | | else if (byte_count == 8) { |
1301 | | return static_cast<T>(load.template operator()<double>()); |
1302 | | } |
1303 | | } |
1304 | | } |
1305 | | else if (type == 1) { |
1306 | | // Signed integer |
1307 | | switch (byte_count) { |
1308 | | case 1: |
1309 | | return static_cast<T>(load.template operator()<int8_t>()); |
1310 | | case 2: |
1311 | | return static_cast<T>(load.template operator()<int16_t>()); |
1312 | | case 4: |
1313 | | return static_cast<T>(load.template operator()<int32_t>()); |
1314 | | case 8: |
1315 | | return static_cast<T>(load.template operator()<int64_t>()); |
1316 | | } |
1317 | | } |
1318 | | else if (type == 2) { |
1319 | | // Unsigned integer |
1320 | | switch (byte_count) { |
1321 | | case 1: |
1322 | | return static_cast<T>(load.template operator()<uint8_t>()); |
1323 | | case 2: |
1324 | | return static_cast<T>(load.template operator()<uint16_t>()); |
1325 | | case 4: |
1326 | | return static_cast<T>(load.template operator()<uint32_t>()); |
1327 | | case 8: |
1328 | | return static_cast<T>(load.template operator()<uint64_t>()); |
1329 | | } |
1330 | | } |
1331 | | |
1332 | | return unexpected(error_ctx{0, error_code::get_wrong_type}); |
1333 | | } |
1334 | | |
1335 | | // ============================================================================ |
1336 | | // BEVE writer for lazy_beve_view |
1337 | | // ============================================================================ |
1338 | | |
1339 | | template <auto Opts> |
1340 | | struct to<BEVE, lazy_beve_view<Opts>> |
1341 | | { |
1342 | | template <auto WriteOpts, class B> |
1343 | | GLZ_ALWAYS_INLINE static void op(const lazy_beve_view<Opts>& view, is_context auto&& ctx, B&& b, auto& ix) |
1344 | | { |
1345 | | if (view.has_error()) { |
1346 | | ctx.error = view.error(); |
1347 | | return; |
1348 | | } |
1349 | | if (!view.data()) { |
1350 | | // Write null |
1351 | | dump_type(ctx, uint8_t{tag::null}, b, ix); |
1352 | | return; |
1353 | | } |
1354 | | |
1355 | | auto it = view.data(); |
1356 | | context parse_ctx{}; |
1357 | | skip_value<BEVE>::op<Opts>(parse_ctx, it, view.beve_end()); |
1358 | | if (bool(parse_ctx.error)) [[unlikely]] { |
1359 | | ctx.error = parse_ctx.error; |
1360 | | return; |
1361 | | } |
1362 | | |
1363 | | const size_t n = static_cast<size_t>(it - view.data()); |
1364 | | if constexpr (resizable<B>) { |
1365 | | if (ix + n > b.size()) [[unlikely]] { |
1366 | | grow_buffer(b, ix + n); |
1367 | | } |
1368 | | } |
1369 | | else { |
1370 | | if (ix + n > b.size()) [[unlikely]] { |
1371 | | ctx.error = error_code::buffer_overflow; |
1372 | | return; |
1373 | | } |
1374 | | } |
1375 | | std::memcpy(&b[ix], view.data(), n); |
1376 | | ix += n; |
1377 | | } |
1378 | | }; |
1379 | | |
1380 | | // ============================================================================ |
1381 | | // lazy_beve - Main entry point |
1382 | | // ============================================================================ |
1383 | | |
1384 | | /** |
1385 | | * @brief Create a lazy BEVE document - minimal upfront processing. |
1386 | | * |
1387 | | * @tparam Opts Options for parsing |
1388 | | * @param buffer The BEVE buffer (must remain valid for document lifetime) |
1389 | | * @return lazy_beve_document on success, error_ctx on failure |
1390 | | */ |
1391 | | template <auto Opts = opts{}, class Buffer> |
1392 | | [[nodiscard]] inline expected<lazy_beve_document<Opts>, error_ctx> lazy_beve(Buffer&& buffer) |
1393 | | { |
1394 | | lazy_beve_document<Opts> doc; |
1395 | | doc.beve_ = reinterpret_cast<const char*>(buffer.data()); |
1396 | | doc.len_ = buffer.size(); |
1397 | | |
1398 | | if (buffer.size() == 0) { |
1399 | | return unexpected(error_ctx{0, error_code::unexpected_end}); |
1400 | | } |
1401 | | |
1402 | | // Validate first byte is a valid BEVE tag |
1403 | | const uint8_t first_byte = static_cast<uint8_t>(doc.beve_[0]); |
1404 | | const uint8_t type_bits = first_byte & 0b00000'111; |
1405 | | |
1406 | | // Valid types: null(0), number(1), string(2), object(3), typed_array(4), generic_array(5), extensions(6) |
1407 | | // Also boolean has tag::boolean pattern |
1408 | | const bool is_boolean = (first_byte & 0b0000'1111) == tag::boolean; |
1409 | | const bool is_valid_type = is_boolean || type_bits <= 6; |
1410 | | |
1411 | | if (!is_valid_type) { |
1412 | | return unexpected(error_ctx{0, error_code::syntax_error}); |
1413 | | } |
1414 | | |
1415 | | doc.root_data_ = doc.beve_; |
1416 | | doc.init_root_view(); |
1417 | | return doc; |
1418 | | } |
1419 | | |
1420 | | // ============================================================================ |
1421 | | // read_beve overload for lazy_beve_view |
1422 | | // ============================================================================ |
1423 | | |
1424 | | template <class T, auto Opts> |
1425 | | [[nodiscard]] inline error_ctx read_beve(T& value, const lazy_beve_view<Opts>& view) |
1426 | | { |
1427 | | return view.template read_into<T>(value); |
1428 | | } |
1429 | | |
1430 | | template <class T, auto Opts> |
1431 | | [[nodiscard]] inline error_ctx read_beve(T& value, lazy_beve_view<Opts>&& view) |
1432 | | { |
1433 | | return view.template read_into<T>(value); |
1434 | | } |
1435 | | |
1436 | | } // namespace glz |