From 06349583297b7a31daaca462dcdbb93b1db63120 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Sun, 21 Feb 2021 11:42:27 -0800 Subject: [PATCH] Don't emit out of order iteration error for empty array --- include/simdjson/generic/ondemand/array-inl.h | 2 +- include/simdjson/generic/ondemand/object-inl.h | 2 +- include/simdjson/generic/ondemand/value_iterator-inl.h | 6 +++++- include/simdjson/generic/ondemand/value_iterator.h | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/include/simdjson/generic/ondemand/array-inl.h b/include/simdjson/generic/ondemand/array-inl.h index ca4ea43e..1018e9ac 100644 --- a/include/simdjson/generic/ondemand/array-inl.h +++ b/include/simdjson/generic/ondemand/array-inl.h @@ -57,7 +57,7 @@ simdjson_really_inline array array::started(value_iterator &iter) noexcept { simdjson_really_inline simdjson_result array::begin() noexcept { #ifdef SIMDJSON_DEVELOPMENT_CHECKS - if (!iter.is_at_container_start()) { return OUT_OF_ORDER_ITERATION; } + if (!iter.is_at_iterator_start()) { return OUT_OF_ORDER_ITERATION; } #endif return array_iterator(iter); } diff --git a/include/simdjson/generic/ondemand/object-inl.h b/include/simdjson/generic/ondemand/object-inl.h index 6b973694..b1d8620c 100644 --- a/include/simdjson/generic/ondemand/object-inl.h +++ b/include/simdjson/generic/ondemand/object-inl.h @@ -53,7 +53,7 @@ simdjson_really_inline object::object(const value_iterator &_iter) noexcept simdjson_really_inline simdjson_result object::begin() noexcept { #ifdef SIMDJSON_DEVELOPMENT_CHECKS - if (!iter.is_at_container_start()) { return OUT_OF_ORDER_ITERATION; } + if (!iter.is_at_iterator_start()) { return OUT_OF_ORDER_ITERATION; } #endif return object_iterator(iter); } diff --git a/include/simdjson/generic/ondemand/value_iterator-inl.h b/include/simdjson/generic/ondemand/value_iterator-inl.h index b6dc3972..af2eb539 100644 --- a/include/simdjson/generic/ondemand/value_iterator-inl.h +++ b/include/simdjson/generic/ondemand/value_iterator-inl.h @@ -462,7 +462,7 @@ simdjson_really_inline error_code value_iterator::advance_container_start(const // If we're not at the position anymore, we don't want to advance the cursor. if (!is_at_start()) { #ifdef SIMDJSON_DEVELOPMENT_CHECKS - if (!is_at_container_start()) { return OUT_OF_ORDER_ITERATION; } + if (!is_at_iterator_start()) { return OUT_OF_ORDER_ITERATION; } #endif json = peek_start(); return SUCCESS; @@ -503,6 +503,10 @@ simdjson_really_inline bool value_iterator::is_at_start() const noexcept { simdjson_really_inline bool value_iterator::is_at_container_start() const noexcept { return _json_iter->token.index == _start_position + 1; } +simdjson_really_inline bool value_iterator::is_at_iterator_start() const noexcept { + auto delta = _json_iter->token.index - _start_position; + return delta == 1 || delta == 2; +} simdjson_really_inline void value_iterator::assert_at_start() const noexcept { SIMDJSON_ASSUME( _json_iter->token.index == _start_position ); diff --git a/include/simdjson/generic/ondemand/value_iterator.h b/include/simdjson/generic/ondemand/value_iterator.h index 72656378..0e49a65e 100644 --- a/include/simdjson/generic/ondemand/value_iterator.h +++ b/include/simdjson/generic/ondemand/value_iterator.h @@ -285,6 +285,7 @@ protected: simdjson_really_inline bool is_at_start() const noexcept; simdjson_really_inline bool is_at_container_start() const noexcept; + simdjson_really_inline bool is_at_iterator_start() const noexcept; simdjson_really_inline void assert_at_start() const noexcept; simdjson_really_inline void assert_at_container_start() const noexcept; simdjson_really_inline void assert_at_root() const noexcept;