1 #ifndef SIMDJSON_INLINE_DOCUMENT_STREAM_H
2 #define SIMDJSON_INLINE_DOCUMENT_STREAM_H
4 #include "simdjson/document_stream.h"
10 namespace simdjson::internal {
35 inline size_t find_last_json_buf_idx(
const uint8_t *buf,
size_t size,
const dom::parser &parser) {
37 if (parser.n_structural_indexes == 0)
39 auto last_i = parser.n_structural_indexes - 1;
40 if (parser.structural_indexes[last_i] == size) {
43 last_i = parser.n_structural_indexes - 2;
47 for (
auto i = last_i; i > 0; i--) {
48 auto idxb = parser.structural_indexes[i];
66 auto idxa = parser.structural_indexes[i - 1];
74 if (!arr_cnt && !obj_cnt) {
83 static inline bool is_ascii(
char c) {
84 return ((
unsigned char)c) <= 127;
89 static inline size_t trimmed_length_safe_utf8(
const char * c,
size_t len) {
90 while ((len > 0) and (not is_ascii(c[len - 1]))) {
98 namespace simdjson::dom {
100 really_inline document_stream::document_stream(
101 dom::parser &_parser,
106 ) noexcept : parser{_parser}, _buf{buf}, _len{len}, _batch_size(batch_size), error{_error} {
107 if (!error) { error = json_parse(); }
110 inline document_stream::~document_stream() noexcept {
111 #ifdef SIMDJSON_THREADS_ENABLED
112 if (stage_1_thread.joinable()) {
113 stage_1_thread.join();
126 really_inline document_stream::iterator::iterator(
document_stream& _stream,
bool is_end) noexcept
127 : stream{_stream}, finished{is_end} {
131 error_code err = stream.error == SUCCESS_AND_HAS_MORE ? SUCCESS : stream.error;
132 if (err) {
return err; }
133 return stream.parser.doc.
root();
137 if (stream.error == SUCCESS_AND_HAS_MORE) {
138 stream.error = stream.json_parse();
146 return finished != other.finished;
149 #ifdef SIMDJSON_THREADS_ENABLED
153 inline error_code document_stream::json_parse() noexcept {
154 error =
parser.ensure_capacity(_batch_size);
155 if (error) {
return error; }
156 error = parser_thread.ensure_capacity(_batch_size);
157 if (error) {
return error; }
159 if (unlikely(load_next_batch)) {
161 if (!stage_1_thread.joinable()) {
162 _batch_size = (std::min)(_batch_size, remaining());
163 _batch_size = internal::trimmed_length_safe_utf8((
const char *)buf(), _batch_size);
164 if (_batch_size == 0) {
165 return simdjson::UTF8_ERROR;
167 auto stage1_is_ok = error_code(simdjson::active_implementation->stage1(buf(), _batch_size, parser,
true));
168 if (stage1_is_ok != simdjson::SUCCESS) {
171 size_t last_index = internal::find_last_json_buf_idx(buf(), _batch_size, parser);
172 if (last_index == 0) {
173 if (parser.n_structural_indexes == 0) {
174 return simdjson::EMPTY;
177 parser.n_structural_indexes = last_index + 1;
182 stage_1_thread.join();
183 if (stage1_is_ok_thread != simdjson::SUCCESS) {
184 return stage1_is_ok_thread;
186 std::swap(parser.structural_indexes, parser_thread.structural_indexes);
187 parser.n_structural_indexes = parser_thread.n_structural_indexes;
188 advance(last_json_buffer_loc);
189 n_bytes_parsed += last_json_buffer_loc;
192 if (remaining() - _batch_size > 0) {
193 last_json_buffer_loc =
194 parser.structural_indexes[internal::find_last_json_buf_idx(buf(), _batch_size, parser)];
195 _batch_size = (std::min)(_batch_size, remaining() - last_json_buffer_loc);
196 if (_batch_size > 0) {
197 _batch_size = internal::trimmed_length_safe_utf8(
198 (
const char *)(buf() + last_json_buffer_loc), _batch_size);
199 if (_batch_size == 0) {
200 return simdjson::UTF8_ERROR;
203 const uint8_t *
const b = buf() + last_json_buffer_loc;
204 const size_t bs = _batch_size;
208 stage_1_thread = std::thread([
this, b, bs] {
209 this->stage1_is_ok_thread = error_code(simdjson::active_implementation->stage1(b, bs, this->parser_thread,
true));
214 load_next_batch =
false;
216 error_code res = simdjson::active_implementation->stage2(buf(), remaining(), parser, next_json);
217 if (res == simdjson::SUCCESS_AND_HAS_MORE) {
219 current_buffer_loc = parser.structural_indexes[next_json];
220 load_next_batch = (current_buffer_loc == last_json_buffer_loc);
221 }
else if (res == simdjson::SUCCESS) {
223 if (remaining() > _batch_size) {
224 current_buffer_loc = parser.structural_indexes[next_json - 1];
225 load_next_batch =
true;
226 res = simdjson::SUCCESS_AND_HAS_MORE;
232 #else // SIMDJSON_THREADS_ENABLED
235 inline error_code document_stream::json_parse() noexcept {
236 error = parser.ensure_capacity(_batch_size);
237 if (error) {
return error; }
239 if (unlikely(load_next_batch)) {
240 advance(current_buffer_loc);
241 n_bytes_parsed += current_buffer_loc;
242 _batch_size = (std::min)(_batch_size, remaining());
243 _batch_size = internal::trimmed_length_safe_utf8((
const char *)buf(), _batch_size);
244 auto stage1_is_ok = (error_code)simdjson::active_implementation->stage1(buf(), _batch_size, parser,
true);
245 if (stage1_is_ok != simdjson::SUCCESS) {
248 size_t last_index = internal::find_last_json_buf_idx(buf(), _batch_size, parser);
249 if (last_index == 0) {
250 if (parser.n_structural_indexes == 0) {
254 parser.n_structural_indexes = last_index + 1;
256 load_next_batch =
false;
258 error_code res = simdjson::active_implementation->stage2(buf(), remaining(), parser, next_json);
259 if (likely(res == simdjson::SUCCESS_AND_HAS_MORE)) {
261 current_buffer_loc = parser.structural_indexes[next_json];
262 }
else if (res == simdjson::SUCCESS) {
264 if (remaining() > _batch_size) {
265 current_buffer_loc = parser.structural_indexes[next_json - 1];
267 load_next_batch =
true;
268 res = simdjson::SUCCESS_AND_HAS_MORE;
273 #endif // SIMDJSON_THREADS_ENABLED
276 #endif // SIMDJSON_INLINE_DOCUMENT_STREAM_H