Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 634d803

Browse files
committed
Increment depth before starting a scope
1 parent ad46154 commit 634d803

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

src/generic/stage2/structural_parser.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ struct structural_parser : structural_iterator {
2727
current_string_buf_loc{parser.doc->string_buf.get()} {
2828
}
2929

30-
WARN_UNUSED really_inline error_code start_scope(bool parent_is_array) {
30+
WARN_UNUSED really_inline error_code start_scope(bool is_array) {
31+
depth++;
32+
if (depth >= parser.max_depth()) { log_error("Exceeded max depth!"); return DEPTH_ERROR; }
3133
parser.containing_scope[depth].tape_index = next_tape_index();
3234
parser.containing_scope[depth].count = 0;
3335
tape.skip(); // We don't actually *write* the start element until the end.
34-
parser.is_array[depth] = parent_is_array;
35-
depth++;
36-
if (depth >= parser.max_depth()) { log_error("Exceeded max depth!"); return DEPTH_ERROR; }
36+
parser.is_array[depth] = is_array;
3737
return SUCCESS;
3838
}
3939

@@ -43,24 +43,23 @@ struct structural_parser : structural_iterator {
4343
parser.containing_scope[depth].count = 0;
4444
tape.skip(); // We don't actually *write* the start element until the end.
4545
parser.is_array[depth] = false;
46-
depth++;
4746
if (depth >= parser.max_depth()) { log_error("Exceeded max depth!"); return DEPTH_ERROR; }
4847
return SUCCESS;
4948
}
5049

51-
WARN_UNUSED really_inline error_code start_object(bool parent_is_array) {
50+
WARN_UNUSED really_inline error_code start_object() {
5251
log_start_value("object");
53-
return start_scope(parent_is_array);
52+
return start_scope(false);
5453
}
5554

56-
WARN_UNUSED really_inline error_code start_array(bool parent_is_array) {
55+
WARN_UNUSED really_inline error_code start_array() {
5756
log_start_value("array");
58-
return start_scope(parent_is_array);
57+
return start_scope(true);
5958
}
6059

6160
// this function is responsible for annotating the start of the scope
6261
really_inline void end_scope(internal::tape_type start, internal::tape_type end) noexcept {
63-
depth--;
62+
// SIMDJSON_ASSUME(depth > 0);
6463
// Write the ending tape element, pointing at the start location
6564
const uint32_t start_tape_index = parser.containing_scope[depth].tape_index;
6665
tape.append(start_tape_index, end);
@@ -70,6 +69,7 @@ struct structural_parser : structural_iterator {
7069
const uint32_t count = parser.containing_scope[depth].count;
7170
const uint32_t cntsat = count > 0xFFFFFF ? 0xFFFFFF : count;
7271
tape_writer::write(parser.doc->tape[start_tape_index], next_tape_index() | (uint64_t(cntsat) << 32), start);
72+
depth--;
7373
}
7474

7575
really_inline uint32_t next_tape_index() {
@@ -86,7 +86,6 @@ struct structural_parser : structural_iterator {
8686
}
8787
really_inline void end_document() {
8888
log_end_value("document");
89-
depth--;
9089
constexpr uint32_t start_tape_index = 0;
9190
tape.append(start_tape_index, internal::tape_type::ROOT);
9291
tape_writer::write(parser.doc->tape[start_tape_index], next_tape_index(), internal::tape_type::ROOT);
@@ -117,11 +116,8 @@ struct structural_parser : structural_iterator {
117116
}
118117

119118
// increment_count increments the count of keys in an object or values in an array.
120-
// Note that if you are at the level of the values or elements, the count
121-
// must be increment in the preceding depth (depth-1) where the array or
122-
// the object resides.
123119
really_inline void increment_count() {
124-
parser.containing_scope[depth - 1].count++; // we have a key value pair in the object at parser.depth - 1
120+
parser.containing_scope[depth].count++; // we have a key value pair in the object at parser.depth - 1
125121
}
126122

127123
really_inline uint8_t *on_start_string() noexcept {
@@ -286,12 +282,12 @@ WARN_UNUSED static really_inline error_code parse_structurals(dom_parser_impleme
286282
switch (parser.current_char()) {
287283
case '{': {
288284
if (parser.empty_object()) { goto finish; }
289-
SIMDJSON_TRY( parser.start_object(false) );
285+
SIMDJSON_TRY( parser.start_object() );
290286
goto object_begin;
291287
}
292288
case '[': {
293289
if (parser.empty_array()) { goto finish; }
294-
SIMDJSON_TRY( parser.start_array(false) );
290+
SIMDJSON_TRY( parser.start_array() );
295291
// Make sure the outer array is closed before continuing; otherwise, there are ways we could get
296292
// into memory corruption. See https://github.com/simdjson/simdjson/issues/906
297293
if (!STREAMING) {
@@ -331,12 +327,12 @@ WARN_UNUSED static really_inline error_code parse_structurals(dom_parser_impleme
331327
switch (parser.advance_char()) {
332328
case '{': {
333329
if (parser.empty_object()) { break; };
334-
SIMDJSON_TRY( parser.start_object(false) );
330+
SIMDJSON_TRY( parser.start_object() );
335331
goto object_begin;
336332
}
337333
case '[': {
338334
if (parser.empty_array()) { break; };
339-
SIMDJSON_TRY( parser.start_array(false) );
335+
SIMDJSON_TRY( parser.start_array() );
340336
goto array_begin;
341337
}
342338
case '"': SIMDJSON_TRY( parser.parse_string() ); break;
@@ -368,7 +364,7 @@ WARN_UNUSED static really_inline error_code parse_structurals(dom_parser_impleme
368364
}
369365

370366
scope_end:
371-
if (parser.depth == 1) { goto finish; }
367+
if (parser.depth == 0) { goto finish; }
372368
if (parser.parser.is_array[parser.depth]) { goto array_continue; }
373369
goto object_continue;
374370

@@ -382,12 +378,12 @@ WARN_UNUSED static really_inline error_code parse_structurals(dom_parser_impleme
382378
switch (parser.advance_char()) {
383379
case '{': {
384380
if (parser.empty_object()) { break; };
385-
SIMDJSON_TRY( parser.start_object(true) );
381+
SIMDJSON_TRY( parser.start_object() );
386382
goto object_begin;
387383
}
388384
case '[': {
389385
if (parser.empty_array()) { break; };
390-
SIMDJSON_TRY( parser.start_array(true) );
386+
SIMDJSON_TRY( parser.start_array() );
391387
goto array_begin;
392388
}
393389
case '"': SIMDJSON_TRY( parser.parse_string() ); break;

0 commit comments

Comments
 (0)