1namespace simdjson {
2namespace SIMDJSON_IMPLEMENTATION {
3namespace ondemand {
4
5/**
6 * Iterates through JSON tokens (`{` `}` `[` `]` `,` `:` `"<string>"` `123` `true` `false` `null`)
7 * detected by stage 1.
8 *
9 * @private This is not intended for external use.
10 */
11class token_iterator {
12public:
13 /**
14 * Create a new invalid token_iterator.
15 *
16 * Exists so you can declare a variable and later assign to it before use.
17 */
18 simdjson_inline token_iterator() noexcept = default;
19 simdjson_inline token_iterator(token_iterator &&other) noexcept = default;
20 simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default;
21 simdjson_inline token_iterator(const token_iterator &other) noexcept = default;
22 simdjson_inline token_iterator &operator=(const token_iterator &other) noexcept = default;
23
24 /**
25 * Advance to the next token (returning the current one).
26 */
27 simdjson_inline const uint8_t *return_current_and_advance() noexcept;
28 /**
29 * Reports the current offset in bytes from the start of the underlying buffer.
30 */
31 simdjson_inline uint32_t current_offset() const noexcept;
32 /**
33 * Get the JSON text for a given token (relative).
34 *
35 * This is not null-terminated; it is a view into the JSON.
36 *
37 * @param delta The relative position of the token to retrieve. e.g. 0 = current token,
38 * 1 = next token, -1 = prev token.
39 *
40 * TODO consider a string_view, assuming the length will get stripped out by the optimizer when
41 * it isn't used ...
42 */
43 simdjson_inline const uint8_t *peek(int32_t delta=0) const noexcept;
44 /**
45 * Get the maximum length of the JSON text for a given token.
46 *
47 * The length will include any whitespace at the end of the token.
48 *
49 * @param delta The relative position of the token to retrieve. e.g. 0 = current token,
50 * 1 = next token, -1 = prev token.
51 */
52 simdjson_inline uint32_t peek_length(int32_t delta=0) const noexcept;
53
54 /**
55 * Get the JSON text for a given token.
56 *
57 * This is not null-terminated; it is a view into the JSON.
58 *
59 * @param position The position of the token.
60 *
61 */
62 simdjson_inline const uint8_t *peek(token_position position) const noexcept;
63 /**
64 * Get the maximum length of the JSON text for a given token.
65 *
66 * The length will include any whitespace at the end of the token.
67 *
68 * @param position The position of the token.
69 */
70 simdjson_inline uint32_t peek_length(token_position position) const noexcept;
71
72 /**
73 * Return the current index.
74 */
75 simdjson_inline token_position position() const noexcept;
76 /**
77 * Reset to a previously saved index.
78 */
79 simdjson_inline void set_position(token_position target_position) noexcept;
80
81 // NOTE: we don't support a full C++ iterator interface, because we expect people to make
82 // different calls to advance the iterator based on *their own* state.
83
84 simdjson_inline bool operator==(const token_iterator &other) const noexcept;
85 simdjson_inline bool operator!=(const token_iterator &other) const noexcept;
86 simdjson_inline bool operator>(const token_iterator &other) const noexcept;
87 simdjson_inline bool operator>=(const token_iterator &other) const noexcept;
88 simdjson_inline bool operator<(const token_iterator &other) const noexcept;
89 simdjson_inline bool operator<=(const token_iterator &other) const noexcept;
90
91protected:
92 simdjson_inline token_iterator(const uint8_t *buf, token_position position) noexcept;
93
94 /**
95 * Get the index of the JSON text for a given token (relative).
96 *
97 * This is not null-terminated; it is a view into the JSON.
98 *
99 * @param delta The relative position of the token to retrieve. e.g. 0 = current token,
100 * 1 = next token, -1 = prev token.
101 */
102 simdjson_inline uint32_t peek_index(int32_t delta=0) const noexcept;
103 /**
104 * Get the index of the JSON text for a given token.
105 *
106 * This is not null-terminated; it is a view into the JSON.
107 *
108 * @param position The position of the token.
109 *
110 */
111 simdjson_inline uint32_t peek_index(token_position position) const noexcept;
112
113 const uint8_t *buf{};
114 token_position _position{};
115
116 friend class json_iterator;
117 friend class value_iterator;
118 friend class object;
119 friend simdjson_inline void logger::log_line(const json_iterator &iter, const char *title_prefix, const char *title, std::string_view detail, int delta, int depth_delta) noexcept;
120 friend simdjson_inline void logger::log_line(const json_iterator &iter, token_position index, depth_t depth, const char *title_prefix, const char *title, std::string_view detail) noexcept;
121};
122
123} // namespace ondemand
124} // namespace SIMDJSON_IMPLEMENTATION
125} // namespace simdjson
126
127namespace simdjson {
128
129template<>
130struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::token_iterator> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::token_iterator> {
131public:
132 simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::token_iterator &&value) noexcept; ///< @private
133 simdjson_inline simdjson_result(error_code error) noexcept; ///< @private
134 simdjson_inline simdjson_result() noexcept = default;
135 simdjson_inline ~simdjson_result() noexcept = default; ///< @private
136};
137
138} // namespace simdjson
139