From 79c7e61645ad946689d3696ad1d39642f22e477a Mon Sep 17 00:00:00 2001 From: "Node.js GitHub Bot" Date: Sun, 22 Nov 2026 00:35:07 +0000 Subject: [PATCH] deps: update simdjson to 5.2.0 --- deps/simdjson/simdjson.cpp | 1437 ++++-- deps/simdjson/simdjson.h | 9188 +++++++++++++++++++++++++++--------- 2 files changed, 7824 insertions(+), 2801 deletions(-) diff --git a/deps/simdjson/simdjson.cpp b/deps/simdjson/simdjson.cpp index 6db84f295573..537a7ab6a9a5 100644 --- a/deps/simdjson/simdjson.cpp +++ b/deps/simdjson/simdjson.cpp @@ -1,4 +1,4 @@ -/* auto-generated on 2026-10-31 19:14:12 -0400. version 5.1.1 Do not edit! */ +/* auto-generated on 2026-11-14 18:54:03 -0500. version 5.2.0 Do not edit! */ /* including simdjson.cpp: */ /* begin file simdjson.cpp */ #define SIMDJSON_SRC_SIMDJSON_CPP @@ -185,6 +185,24 @@ #endif #endif // !defined(SIMDJSON_SUPPORTS_CHAR8_T) +// std::string::resize_and_overwrite (C++23) is available. +#if !defined(SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE) +#if defined(__cpp_lib_string_resize_and_overwrite) && __cpp_lib_string_resize_and_overwrite >= 202110L +#define SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE 1 +#else +#define SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE 0 +#endif +#endif // !defined(SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE) + +// std::string can be used in constant expressions (C++20, e.g., libstdc++ 12 or better). +#if !defined(SIMDJSON_SUPPORTS_CONSTEXPR_STRING) +#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#define SIMDJSON_SUPPORTS_CONSTEXPR_STRING 1 +#else +#define SIMDJSON_SUPPORTS_CONSTEXPR_STRING 0 +#endif +#endif // !defined(SIMDJSON_SUPPORTS_CONSTEXPR_STRING) + // The C++23 fixed-width floating-point types std::float32_t and std::float64_t // () are available. They are optional even in C++23: a compiler that // provides them predefines __STDCPP_FLOAT32_T__ and __STDCPP_FLOAT64_T__. @@ -217,11 +235,11 @@ #if !defined(SIMDJSON_CONSTEVAL) -#if defined(__cpp_consteval) && __cpp_consteval >= 201811L && defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#if defined(__cpp_consteval) && __cpp_consteval >= 201811L && SIMDJSON_SUPPORTS_CONSTEXPR_STRING #define SIMDJSON_CONSTEVAL 1 #else #define SIMDJSON_CONSTEVAL 0 -#endif // defined(__cpp_consteval) && __cpp_consteval >= 201811L && defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#endif // defined(__cpp_consteval) && __cpp_consteval >= 201811L && SIMDJSON_SUPPORTS_CONSTEXPR_STRING #endif // !defined(SIMDJSON_CONSTEVAL) // SIMDJSON_CONSTEXPR_STRING is 'constexpr' when the standard library supports @@ -229,11 +247,11 @@ // lets functions that build a std::string be constant expressions when possible // while still compiling against older standard libraries. #if !defined(SIMDJSON_CONSTEXPR_STRING) -#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#if SIMDJSON_SUPPORTS_CONSTEXPR_STRING #define SIMDJSON_CONSTEXPR_STRING constexpr #else #define SIMDJSON_CONSTEXPR_STRING -#endif // defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#endif // SIMDJSON_SUPPORTS_CONSTEXPR_STRING #endif // !defined(SIMDJSON_CONSTEXPR_STRING) #endif // SIMDJSON_COMPILER_CHECK_H /* end file simdjson/compiler_check.h */ @@ -309,7 +327,25 @@ using std::size_t; #define SIMDJSON_IS_LSX 1 // Adjust for runtime dispatching support. -#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__NVCOMPILER) +// +// LASX runtime dispatching is deliberately disabled by default, so that the +// LSX kernel is used even when the processor supports LASX. On a Loongson +// 3A6000, our LASX kernel parses about 5% slower overall than our LSX +// kernel (stage 1 about 14% slower), with both GCC 15 and LLVM. The 256-bit +// registers do not help: stage 1 is limited by moving bitmasks from vector +// registers to general-purpose registers, and with LASX each bitmask needs +// a cross-lane permutation (3 cycles) and a slower vector-to-GPR move +// (xvpickve2gr is 2 cycles slower than vpickve2gr). +// +// Define SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH to 0 to enable LASX runtime +// dispatching (GCC 15+ or LLVM). Compiling with -mlasx selects the LASX +// kernel regardless of this macro. See doc/implementation-selection.md. +#ifndef SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH + #define SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH 1 +#endif +#if SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH + #define SIMDJSON_IMPLEMENTATION_LASX 0 +#elif defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__NVCOMPILER) #if __GNUC__ > 15 || (__GNUC__ == 15 && __GNUC_MINOR__ >= 0) // We are ok, we will support runtime dispatch for LASX. #else @@ -318,14 +354,15 @@ using std::size_t; // Loongson users should update to GCC 15 or better. #define SIMDJSON_IMPLEMENTATION_LASX 0 #endif +#elif defined(__clang__) + // LLVM supports runtime dispatch for LASX through the target attribute. + // See simdjson/lasx/intrinsics.h. #else - // We are not using GCC, so we assume that we can support runtime dispatch for LASX. + // We are not using GCC or LLVM, so we do not support runtime dispatch for LASX. // https://godbolt.org/z/jcMnrjYhs #define SIMDJSON_IMPLEMENTATION_LASX 0 #endif - - #endif #elif defined(__PPC64__) || defined(_M_PPC64) #define SIMDJSON_IS_PPC64 1 @@ -3139,10 +3176,25 @@ template concept string_view_like = std::is_convertible_v && !std::is_convertible_v; +template +concept optional_type = requires(std::remove_cvref_t obj) { + typename std::remove_cvref_t::value_type; + { obj.value() } -> std::same_as::value_type&>; + requires requires(typename std::remove_cvref_t::value_type &&val) { + obj.emplace(std::move(val)); + { + obj.value_or(val) + } -> std::convertible_to::value_type>; + }; + { static_cast(obj) } -> std::same_as; // convertible to bool + { obj.reset() } noexcept -> std::same_as; +}; + template concept constructible_from_string_view = std::is_constructible_v && !std::is_same_v - && std::is_default_constructible_v; + && std::is_default_constructible_v + && !optional_type; #if SIMDJSON_SUPPORTS_CHAR8_T /** @@ -3154,7 +3206,8 @@ template concept constructible_from_u8string_view = std::is_constructible_v && !std::is_same_v && !std::is_constructible_v - && std::is_default_constructible_v; + && std::is_default_constructible_v + && !optional_type; #endif // SIMDJSON_SUPPORTS_CHAR8_T template @@ -3224,22 +3277,6 @@ concept smart_pointer = requires(std::remove_cvref_t ptr) { { *ptr } -> std::same_as::element_type &>; }; -template -concept optional_type = requires(std::remove_cvref_t obj) { - typename std::remove_cvref_t::value_type; - { obj.value() } -> std::same_as::value_type&>; - requires requires(typename std::remove_cvref_t::value_type &&val) { - obj.emplace(std::move(val)); - { - obj.value_or(val) - } -> std::convertible_to::value_type>; - }; - { static_cast(obj) } -> std::same_as; // convertible to bool - { obj.reset() } noexcept -> std::same_as; -}; - - - // Types we serialize as JSON strings (not as containers) template concept string_like = @@ -13013,7 +13050,7 @@ class document { * * The parser will allocate capacity as needed. */ - document() noexcept = default; + simdjson_inline document() noexcept {} ~document() noexcept = default; /** @@ -14543,6 +14580,9 @@ namespace { simdjson_inline simd8() : simd8(vdupq_n_u8(0)) {} // Splat constructor simdjson_inline simd8(bool _value) : simd8(splat(_value)) {} + // base_u8's operator~ xors with 0xFFu, which simd8 only accepts by + // narrowing it to bool (MSVC C4305). Every other kernel overrides it too. + simdjson_inline simd8 operator~() const { return *this ^ true; } // We return uint32_t instead of uint16_t because that seems to be more efficient for most // purposes (cutting it down to uint16_t costs performance in some compilers). @@ -14642,9 +14682,15 @@ namespace { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return ~any_bits_set(bits); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } simdjson_inline simd8 any_bits_set(simd8 bits) const { return vtstq_u8(*this, bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return vshrq_n_u8(*this, N); } template @@ -15408,8 +15454,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -15419,11 +15470,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -15474,7 +15528,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -16687,6 +16741,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -16779,6 +16836,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -17061,6 +17121,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -17157,6 +17220,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -17842,6 +17908,9 @@ namespace { simdjson_inline simd8() : simd8(vdupq_n_u8(0)) {} // Splat constructor simdjson_inline simd8(bool _value) : simd8(splat(_value)) {} + // base_u8's operator~ xors with 0xFFu, which simd8 only accepts by + // narrowing it to bool (MSVC C4305). Every other kernel overrides it too. + simdjson_inline simd8 operator~() const { return *this ^ true; } // We return uint32_t instead of uint16_t because that seems to be more efficient for most // purposes (cutting it down to uint16_t costs performance in some compilers). @@ -17941,9 +18010,15 @@ namespace { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return ~any_bits_set(bits); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } simdjson_inline simd8 any_bits_set(simd8 bits) const { return vtstq_u8(*this, bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return vshrq_n_u8(*this, N); } template @@ -19706,6 +19781,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for arm64 */ +/* including generic/stage1/utf8_validator.h for arm64: #include */ +/* begin file generic/stage1/utf8_validator.h for arm64 */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace arm64 { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace arm64 +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for arm64 */ /* including generic/stage1/json_structural_indexer.h for arm64: #include */ /* begin file generic/stage1/json_structural_indexer.h for arm64 */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -19719,6 +19842,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -20008,7 +20132,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -20161,54 +20295,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for arm64 */ -/* including generic/stage1/utf8_validator.h for arm64: #include */ -/* begin file generic/stage1/utf8_validator.h for arm64 */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace arm64 { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace arm64 -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for arm64 */ /* end file generic/stage1/amalgamated.h for arm64 */ /* including generic/stage2/amalgamated.h for arm64: #include */ /* begin file generic/stage2/amalgamated.h for arm64 */ @@ -22973,8 +23059,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -22984,11 +23075,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -23039,7 +23133,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -24252,6 +24346,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -24344,6 +24441,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -24626,6 +24726,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -24722,6 +24825,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -27119,6 +27225,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for haswell */ +/* including generic/stage1/utf8_validator.h for haswell: #include */ +/* begin file generic/stage1/utf8_validator.h for haswell */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace haswell { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace haswell +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for haswell */ /* including generic/stage1/json_structural_indexer.h for haswell: #include */ /* begin file generic/stage1/json_structural_indexer.h for haswell */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -27132,6 +27286,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -27421,7 +27576,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -27574,54 +27739,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for haswell */ -/* including generic/stage1/utf8_validator.h for haswell: #include */ -/* begin file generic/stage1/utf8_validator.h for haswell */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace haswell { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace haswell -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for haswell */ /* end file generic/stage1/amalgamated.h for haswell */ /* including generic/stage2/amalgamated.h for haswell: #include */ /* begin file generic/stage2/amalgamated.h for haswell */ @@ -30362,8 +30479,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -30373,11 +30495,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -30428,7 +30553,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -31641,6 +31766,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -31733,6 +31861,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -32015,6 +32146,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -32111,6 +32245,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -34503,6 +34640,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for icelake */ +/* including generic/stage1/utf8_validator.h for icelake: #include */ +/* begin file generic/stage1/utf8_validator.h for icelake */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace icelake { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace icelake +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for icelake */ /* including generic/stage1/json_structural_indexer.h for icelake: #include */ /* begin file generic/stage1/json_structural_indexer.h for icelake */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -34516,6 +34701,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -34805,7 +34991,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -34958,54 +35154,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for icelake */ -/* including generic/stage1/utf8_validator.h for icelake: #include */ -/* begin file generic/stage1/utf8_validator.h for icelake */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace icelake { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace icelake -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for icelake */ /* end file generic/stage1/amalgamated.h for icelake */ /* including generic/stage2/amalgamated.h for icelake: #include */ /* begin file generic/stage2/amalgamated.h for icelake */ @@ -37303,7 +37451,7 @@ template <> struct simd8 : base8_numeric { } simdjson_inline simd8 operator<(const simd8 other) const { - return this->gt_bits(other).any_bits_set(); + return this->lt_bits(other).any_bits_set(); } // Bit-specific operations @@ -37332,6 +37480,9 @@ template <> struct simd8 : base8_numeric { simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return !bits_not_set_anywhere(bits); } + simdjson_inline bool is_ascii() const { + return this->bits_not_set_anywhere(uint8_t(0x80)); + } template simdjson_inline simd8 shr() const { return simd8( (__m128i)vec_sr(this->value, (__m128i)vec_splat_u8(N))); @@ -37907,8 +38058,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -37918,11 +38074,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -37973,7 +38132,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -39186,6 +39345,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -39278,6 +39440,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -39560,6 +39725,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -39656,6 +39824,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -40561,7 +40732,7 @@ template <> struct simd8 : base8_numeric { } simdjson_inline simd8 operator<(const simd8 other) const { - return this->gt_bits(other).any_bits_set(); + return this->lt_bits(other).any_bits_set(); } // Bit-specific operations @@ -40590,6 +40761,9 @@ template <> struct simd8 : base8_numeric { simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return !bits_not_set_anywhere(bits); } + simdjson_inline bool is_ascii() const { + return this->bits_not_set_anywhere(uint8_t(0x80)); + } template simdjson_inline simd8 shr() const { return simd8( (__m128i)vec_sr(this->value, (__m128i)vec_splat_u8(N))); @@ -42164,6 +42338,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for ppc64 */ +/* including generic/stage1/utf8_validator.h for ppc64: #include */ +/* begin file generic/stage1/utf8_validator.h for ppc64 */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace ppc64 { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace ppc64 +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for ppc64 */ /* including generic/stage1/json_structural_indexer.h for ppc64: #include */ /* begin file generic/stage1/json_structural_indexer.h for ppc64 */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -42177,6 +42399,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -42466,7 +42689,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -42619,54 +42852,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for ppc64 */ -/* including generic/stage1/utf8_validator.h for ppc64: #include */ -/* begin file generic/stage1/utf8_validator.h for ppc64 */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace ppc64 { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace ppc64 -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for ppc64 */ /* end file generic/stage1/amalgamated.h for ppc64 */ /* including generic/stage2/amalgamated.h for ppc64: #include */ /* begin file generic/stage2/amalgamated.h for ppc64 */ @@ -44829,7 +45014,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -45248,7 +45433,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -45806,8 +45991,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -45817,11 +46007,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -45872,7 +46065,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -47085,6 +47278,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -47177,6 +47373,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -47459,6 +47658,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -47555,6 +47757,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -48393,7 +48598,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -48812,7 +49017,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -50369,6 +50574,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for westmere */ +/* including generic/stage1/utf8_validator.h for westmere: #include */ +/* begin file generic/stage1/utf8_validator.h for westmere */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace westmere { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace westmere +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for westmere */ /* including generic/stage1/json_structural_indexer.h for westmere: #include */ /* begin file generic/stage1/json_structural_indexer.h for westmere */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -50382,6 +50635,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -50671,7 +50925,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -50824,54 +51088,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for westmere */ -/* including generic/stage1/utf8_validator.h for westmere: #include */ -/* begin file generic/stage1/utf8_validator.h for westmere */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace westmere { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace westmere -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for westmere */ /* end file generic/stage1/amalgamated.h for westmere */ /* including generic/stage2/amalgamated.h for westmere: #include */ /* begin file generic/stage2/amalgamated.h for westmere */ @@ -52570,7 +52786,18 @@ template struct simd8x64; /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ #include +#if defined(__clang__) && !defined(__loongarch_asx) +// LLVM's lasxintrin.h only declares the LASX types and functions when +// __loongarch_asx is defined, that is, when the code is compiled with -mlasx. +// For runtime dispatching, we include it inside a target("lasx") region +// (see simdjson/lasx/begin.h) where the LASX builtins are available, so we +// define __loongarch_asx while including the header. +#define __loongarch_asx 1 #include +#undef __loongarch_asx +#else +#include +#endif static_assert(sizeof(__m256i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch ASX"); @@ -52789,10 +53016,7 @@ namespace simd { __m256i mask = __lasx_xvmskltz_b(*this); return (__lasx_xvpickve2gr_w(mask, 4) << 16) | (__lasx_xvpickve2gr_w(mask, 0)); } - simdjson_inline bool any() const { - __m256i v = __lasx_xvmsknz_b(*this); - return (0 == __lasx_xvpickve2gr_w(v, 0)) && (0 == __lasx_xvpickve2gr_w(v, 4)); - } + simdjson_inline bool any() const { return !__lasx_xbz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -53558,8 +53782,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -53569,11 +53798,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -53624,7 +53856,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -54837,6 +55069,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -54929,6 +55164,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -55211,6 +55449,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -55307,6 +55548,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -55650,7 +55894,18 @@ template struct simd8x64; /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ #include +#if defined(__clang__) && !defined(__loongarch_asx) +// LLVM's lasxintrin.h only declares the LASX types and functions when +// __loongarch_asx is defined, that is, when the code is compiled with -mlasx. +// For runtime dispatching, we include it inside a target("lasx") region +// (see simdjson/lasx/begin.h) where the LASX builtins are available, so we +// define __loongarch_asx while including the header. +#define __loongarch_asx 1 #include +#undef __loongarch_asx +#else +#include +#endif static_assert(sizeof(__m256i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch ASX"); @@ -55869,10 +56124,7 @@ namespace simd { __m256i mask = __lasx_xvmskltz_b(*this); return (__lasx_xvpickve2gr_w(mask, 4) << 16) | (__lasx_xvpickve2gr_w(mask, 0)); } - simdjson_inline bool any() const { - __m256i v = __lasx_xvmsknz_b(*this); - return (0 == __lasx_xvpickve2gr_w(v, 0)) && (0 == __lasx_xvpickve2gr_w(v, 4)); - } + simdjson_inline bool any() const { return !__lasx_xbz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -57637,6 +57889,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for lasx */ +/* including generic/stage1/utf8_validator.h for lasx: #include */ +/* begin file generic/stage1/utf8_validator.h for lasx */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace lasx { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace lasx +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for lasx */ /* including generic/stage1/json_structural_indexer.h for lasx: #include */ /* begin file generic/stage1/json_structural_indexer.h for lasx */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -57650,6 +57950,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -57939,7 +58240,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -58092,54 +58403,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for lasx */ -/* including generic/stage1/utf8_validator.h for lasx: #include */ -/* begin file generic/stage1/utf8_validator.h for lasx */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace lasx { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace lasx -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for lasx */ /* end file generic/stage1/amalgamated.h for lasx */ /* including generic/stage2/amalgamated.h for lasx: #include */ /* begin file generic/stage2/amalgamated.h for lasx */ @@ -60015,7 +60278,7 @@ namespace simd { simdjson_inline simd8(bool _value) : base8(splat(_value)) {} simdjson_inline int to_bitmask() const { return __lsx_vpickve2gr_w(__lsx_vmskltz_b(*this), 0); } - simdjson_inline bool any() const { return 0 == __lsx_vpickve2gr_hu(__lsx_vmsknz_b(*this), 0); } + simdjson_inline bool any() const { return !__lsx_bz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -60771,8 +61034,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -60782,11 +61050,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -60837,7 +61108,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -62050,6 +62321,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -62142,6 +62416,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -62424,6 +62701,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -62520,6 +62800,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -63062,7 +63345,7 @@ namespace simd { simdjson_inline simd8(bool _value) : base8(splat(_value)) {} simdjson_inline int to_bitmask() const { return __lsx_vpickve2gr_w(__lsx_vmskltz_b(*this), 0); } - simdjson_inline bool any() const { return 0 == __lsx_vpickve2gr_hu(__lsx_vmsknz_b(*this), 0); } + simdjson_inline bool any() const { return !__lsx_bz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -64817,6 +65100,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for lsx */ +/* including generic/stage1/utf8_validator.h for lsx: #include */ +/* begin file generic/stage1/utf8_validator.h for lsx */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace lsx { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace lsx +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for lsx */ /* including generic/stage1/json_structural_indexer.h for lsx: #include */ /* begin file generic/stage1/json_structural_indexer.h for lsx */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -64830,6 +65161,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -65119,7 +65451,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -65272,54 +65614,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for lsx */ -/* including generic/stage1/utf8_validator.h for lsx: #include */ -/* begin file generic/stage1/utf8_validator.h for lsx */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace lsx { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace lsx -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for lsx */ /* end file generic/stage1/amalgamated.h for lsx */ /* including generic/stage2/amalgamated.h for lsx: #include */ /* begin file generic/stage2/amalgamated.h for lsx */ @@ -67166,7 +67460,9 @@ namespace simd { simdjson_inline simd8 operator|(const simd8 other) const { return __riscv_vmor(*this, other, VL8); } simdjson_inline simd8 operator&(const simd8 other) const { return __riscv_vmand(*this, other, VL8); } simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vmxor(*this, other, VL8); } - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(other, *this, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(*this, other, VL8); } + simdjson_inline simd8 operator==(const simd8 other) const { return __riscv_vmxnor(*this, other, VL8); } + simdjson_inline bool any() const { return __riscv_vfirst(value, VL8) >= 0; } simdjson_inline simd8 operator~() const { return __riscv_vmnot(*this, VL8); } simdjson_inline simd8& operator|=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast | other; return *this_cast; } simdjson_inline simd8& operator&=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast & other; return *this_cast; } @@ -67218,9 +67514,9 @@ namespace simd { simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vxor_vv_u8m1( value, other, VL8); } simdjson_inline simd8 operator~() const { return __riscv_vnot_v_u8m1(value, VL8); } #if __riscv_zvbb - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(other, value, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(value, other, VL8); } #else - simdjson_inline simd8 bit_andnot(const simd8 other) const { return other & ~*this; } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return *this & ~other; } #endif simdjson_inline simd8& operator|=(const simd8 other) { value = *this | other; return *this; } simdjson_inline simd8& operator&=(const simd8 other) { value = *this & other; return *this; } @@ -67248,6 +67544,8 @@ namespace simd { simdjson_inline simd8& operator-=(const simd8 other) { value = *this - other; return *this; } // Order-specific operations + simdjson_inline simd8 max_val(const simd8 other) const { return __riscv_vmaxu(value, other, VL8); } + simdjson_inline simd8 min_val(const simd8 other) const { return __riscv_vminu(value, other, VL8); } simdjson_inline simd8 operator<=(const simd8 other) const { return __riscv_vmsleu(value, other, VL8); } simdjson_inline simd8 operator>=(const simd8 other) const { return __riscv_vmsgeu(value, other, VL8); } simdjson_inline simd8 operator<(const simd8 other) const { return __riscv_vmsltu(value, other, VL8); } @@ -67259,10 +67557,17 @@ namespace simd { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return (*this & bits).bits_not_set(); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } + simdjson_inline simd8 any_bits_set(simd8 bits) const { return ~this->bits_not_set(bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return __riscv_vfirst(__riscv_vmsne(value, 0, VL8), VL8) >= 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return __riscv_vsrl(value, N, VL8); } template @@ -67978,8 +68283,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -67989,11 +68299,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -68044,7 +68357,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -69257,6 +69570,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -69349,6 +69665,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -69631,6 +69950,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -69727,6 +70049,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -70244,7 +70569,9 @@ namespace simd { simdjson_inline simd8 operator|(const simd8 other) const { return __riscv_vmor(*this, other, VL8); } simdjson_inline simd8 operator&(const simd8 other) const { return __riscv_vmand(*this, other, VL8); } simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vmxor(*this, other, VL8); } - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(other, *this, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(*this, other, VL8); } + simdjson_inline simd8 operator==(const simd8 other) const { return __riscv_vmxnor(*this, other, VL8); } + simdjson_inline bool any() const { return __riscv_vfirst(value, VL8) >= 0; } simdjson_inline simd8 operator~() const { return __riscv_vmnot(*this, VL8); } simdjson_inline simd8& operator|=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast | other; return *this_cast; } simdjson_inline simd8& operator&=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast & other; return *this_cast; } @@ -70296,9 +70623,9 @@ namespace simd { simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vxor_vv_u8m1( value, other, VL8); } simdjson_inline simd8 operator~() const { return __riscv_vnot_v_u8m1(value, VL8); } #if __riscv_zvbb - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(other, value, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(value, other, VL8); } #else - simdjson_inline simd8 bit_andnot(const simd8 other) const { return other & ~*this; } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return *this & ~other; } #endif simdjson_inline simd8& operator|=(const simd8 other) { value = *this | other; return *this; } simdjson_inline simd8& operator&=(const simd8 other) { value = *this & other; return *this; } @@ -70326,6 +70653,8 @@ namespace simd { simdjson_inline simd8& operator-=(const simd8 other) { value = *this - other; return *this; } // Order-specific operations + simdjson_inline simd8 max_val(const simd8 other) const { return __riscv_vmaxu(value, other, VL8); } + simdjson_inline simd8 min_val(const simd8 other) const { return __riscv_vminu(value, other, VL8); } simdjson_inline simd8 operator<=(const simd8 other) const { return __riscv_vmsleu(value, other, VL8); } simdjson_inline simd8 operator>=(const simd8 other) const { return __riscv_vmsgeu(value, other, VL8); } simdjson_inline simd8 operator<(const simd8 other) const { return __riscv_vmsltu(value, other, VL8); } @@ -70337,10 +70666,17 @@ namespace simd { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return (*this & bits).bits_not_set(); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } + simdjson_inline simd8 any_bits_set(simd8 bits) const { return ~this->bits_not_set(bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return __riscv_vfirst(__riscv_vmsne(value, 0, VL8), VL8) >= 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return __riscv_vsrl(value, N, VL8); } template @@ -70723,7 +71059,9 @@ namespace simd { simdjson_inline simd8 operator|(const simd8 other) const { return __riscv_vmor(*this, other, VL8); } simdjson_inline simd8 operator&(const simd8 other) const { return __riscv_vmand(*this, other, VL8); } simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vmxor(*this, other, VL8); } - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(other, *this, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(*this, other, VL8); } + simdjson_inline simd8 operator==(const simd8 other) const { return __riscv_vmxnor(*this, other, VL8); } + simdjson_inline bool any() const { return __riscv_vfirst(value, VL8) >= 0; } simdjson_inline simd8 operator~() const { return __riscv_vmnot(*this, VL8); } simdjson_inline simd8& operator|=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast | other; return *this_cast; } simdjson_inline simd8& operator&=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast & other; return *this_cast; } @@ -70775,9 +71113,9 @@ namespace simd { simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vxor_vv_u8m1( value, other, VL8); } simdjson_inline simd8 operator~() const { return __riscv_vnot_v_u8m1(value, VL8); } #if __riscv_zvbb - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(other, value, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(value, other, VL8); } #else - simdjson_inline simd8 bit_andnot(const simd8 other) const { return other & ~*this; } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return *this & ~other; } #endif simdjson_inline simd8& operator|=(const simd8 other) { value = *this | other; return *this; } simdjson_inline simd8& operator&=(const simd8 other) { value = *this & other; return *this; } @@ -70805,6 +71143,8 @@ namespace simd { simdjson_inline simd8& operator-=(const simd8 other) { value = *this - other; return *this; } // Order-specific operations + simdjson_inline simd8 max_val(const simd8 other) const { return __riscv_vmaxu(value, other, VL8); } + simdjson_inline simd8 min_val(const simd8 other) const { return __riscv_vminu(value, other, VL8); } simdjson_inline simd8 operator<=(const simd8 other) const { return __riscv_vmsleu(value, other, VL8); } simdjson_inline simd8 operator>=(const simd8 other) const { return __riscv_vmsgeu(value, other, VL8); } simdjson_inline simd8 operator<(const simd8 other) const { return __riscv_vmsltu(value, other, VL8); } @@ -70816,10 +71156,17 @@ namespace simd { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return (*this & bits).bits_not_set(); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } + simdjson_inline simd8 any_bits_set(simd8 bits) const { return ~this->bits_not_set(bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return __riscv_vfirst(__riscv_vmsne(value, 0, VL8), VL8) >= 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return __riscv_vsrl(value, N, VL8); } template @@ -72428,6 +72775,54 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_MINIFIER_H /* end file generic/stage1/json_minifier.h for rvv_vls */ +/* including generic/stage1/utf8_validator.h for rvv_vls: #include */ +/* begin file generic/stage1/utf8_validator.h for rvv_vls */ +#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H + +/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ +/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ + +namespace simdjson { +namespace rvv_vls { +namespace { +namespace stage1 { + +/** + * Validates that the string is actual UTF-8. + */ +template +bool generic_validate_utf8(const uint8_t * input, size_t length) { + checker c{}; + buf_block_reader<64> reader(input, length); + while (reader.has_full_block()) { + simd::simd8x64 in(reader.full_block()); + c.check_next_input(in); + reader.advance(); + } + uint8_t block[64]{}; + reader.get_remainder(block); + simd::simd8x64 in(block); + c.check_next_input(in); + reader.advance(); + c.check_eof(); + return c.errors() == error_code::SUCCESS; +} + +bool generic_validate_utf8(const char * input, size_t length) { + return generic_validate_utf8(reinterpret_cast(input),length); +} + +} // namespace stage1 +} // unnamed namespace +} // namespace rvv_vls +} // namespace simdjson + +#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H +/* end file generic/stage1/utf8_validator.h for rvv_vls */ /* including generic/stage1/json_structural_indexer.h for rvv_vls: #include */ /* begin file generic/stage1/json_structural_indexer.h for rvv_vls */ #ifndef SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H @@ -72441,6 +72836,7 @@ error_code json_minifier::minify(const uint8_t *buf, size_t len, uint8_t *dst, s /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #include */ +/* amalgamation skipped (editor-only): #include */ /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ // This file contains the common code every implementation uses in stage1 @@ -72730,7 +73126,17 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa // Bail out only once the count and sentinels above are set: // document_stream::truncated_bytes() reads them even on error. - if (simdjson_unlikely(should_we_exit)) { return error; } + if (simdjson_unlikely(should_we_exit)) { + // The error is UNCLOSED_STRING, which iterate_allow_incomplete_json() + // accepts: it must not hide an invalid input. A character cut by the end + // of the input belongs to the unclosed string, as when streaming. + if (unescaped_chars_error) { return UNESCAPED_CHARS; } + if (checker.errors() != SUCCESS && + !generic_validate_utf8(parser.buf, trim_partial_utf8(parser.buf, len))) { + return UTF8_ERROR; + } + return error; + } if (unescaped_chars_error) { return UNESCAPED_CHARS; } // a valid JSON file cannot have zero structural indexes - we should have found something @@ -72883,54 +73289,6 @@ simdjson_inline error_code json_structural_indexer::finish(dom_parser_implementa #endif // SIMDJSON_SRC_GENERIC_STAGE1_JSON_STRUCTURAL_INDEXER_H /* end file generic/stage1/json_structural_indexer.h for rvv_vls */ -/* including generic/stage1/utf8_validator.h for rvv_vls: #include */ -/* begin file generic/stage1/utf8_validator.h for rvv_vls */ -#ifndef SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H - -/* amalgamation skipped (editor-only): #ifndef SIMDJSON_CONDITIONAL_INCLUDE */ -/* amalgamation skipped (editor-only): #define SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #include */ -/* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ - -namespace simdjson { -namespace rvv_vls { -namespace { -namespace stage1 { - -/** - * Validates that the string is actual UTF-8. - */ -template -bool generic_validate_utf8(const uint8_t * input, size_t length) { - checker c{}; - buf_block_reader<64> reader(input, length); - while (reader.has_full_block()) { - simd::simd8x64 in(reader.full_block()); - c.check_next_input(in); - reader.advance(); - } - uint8_t block[64]{}; - reader.get_remainder(block); - simd::simd8x64 in(block); - c.check_next_input(in); - reader.advance(); - c.check_eof(); - return c.errors() == error_code::SUCCESS; -} - -bool generic_validate_utf8(const char * input, size_t length) { - return generic_validate_utf8(reinterpret_cast(input),length); -} - -} // namespace stage1 -} // unnamed namespace -} // namespace rvv_vls -} // namespace simdjson - -#endif // SIMDJSON_SRC_GENERIC_STAGE1_UTF8_VALIDATOR_H -/* end file generic/stage1/utf8_validator.h for rvv_vls */ /* end file generic/stage1/amalgamated.h for rvv_vls */ /* including generic/stage2/amalgamated.h for rvv_vls: #include */ /* begin file generic/stage2/amalgamated.h for rvv_vls */ @@ -75173,8 +75531,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -75184,11 +75547,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -75239,7 +75605,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -76452,6 +76818,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -76544,6 +76913,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -76826,6 +77198,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -76922,6 +77297,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -79325,6 +79703,17 @@ simdjson_inline bool is_continuation(uint8_t c) { return (c & 0xc0) == 0x80; } +// The input ends inside the n-byte character at idx: the bytes after it, as +// far as they go, are continuation bytes. Such a character belongs to a string +// that does not close: it is not an error, as with streaming. +simdjson_inline bool is_cut_by_end(size_t n) { + for (size_t i = size_t(idx) + 1; i < size_t(idx) + n; i++) { + if (i >= len) { return true; } + if (!is_continuation(buf[i])) { return false; } + } + return false; +} + simdjson_inline void validate_utf8_character() { // Continuation if (simdjson_unlikely((buf[idx] & 0x40) == 0)) { @@ -79338,7 +79727,7 @@ simdjson_inline void validate_utf8_character() { if ((buf[idx] & 0x20) == 0) { // missing continuation if (simdjson_unlikely(idx+1 >= len || !is_continuation(buf[idx+1]))) { - if (idx+1 >= len && is_streaming(partial)) { idx = len; return; } + if (idx+1 >= len && (is_streaming(partial) || is_cut_by_end(2))) { idx = len; return; } error = UTF8_ERROR; idx++; return; @@ -79353,7 +79742,7 @@ simdjson_inline void validate_utf8_character() { if ((buf[idx] & 0x10) == 0) { // missing continuation if (simdjson_unlikely(idx+2 >= len || !is_continuation(buf[idx+1]) || !is_continuation(buf[idx+2]))) { - if (idx+2 >= len && is_streaming(partial)) { idx = len; return; } + if (idx+2 >= len && (is_streaming(partial) || is_cut_by_end(3))) { idx = len; return; } error = UTF8_ERROR; idx++; return; @@ -79369,7 +79758,7 @@ simdjson_inline void validate_utf8_character() { // 4-byte // missing continuation if (simdjson_unlikely(idx+3 >= len || !is_continuation(buf[idx+1]) || !is_continuation(buf[idx+2]) || !is_continuation(buf[idx+3]))) { - if (idx+3 >= len && is_streaming(partial)) { idx = len; return; } + if (idx+3 >= len && (is_streaming(partial) || is_cut_by_end(4))) { idx = len; return; } error = UTF8_ERROR; idx++; return; @@ -79485,7 +79874,7 @@ simdjson_warn_unused simdjson_inline error_code scan() { // String if (buf[idx] == '"') { add_structural(); - unclosed_string |= validate_string(); + if (validate_string()) { unclosed_string = true; break; } // Operator } else if (char_is_operator(buf[idx])) { add_structural(); @@ -79632,7 +80021,11 @@ simdjson_warn_unused simdjson_inline error_code scan() { parser.structural_indexes[parser.n_structural_indexes + 1] = parser.structural_indexes[parser.n_structural_indexes]; parser.structural_indexes[parser.n_structural_indexes] = uint32_t(len); if (simdjson_unlikely(parser.n_structural_indexes == 0)) { return EMPTY; } - } else if(unclosed_string) { error = UNCLOSED_STRING; } + } else if(unclosed_string && error == SUCCESS) { + // Keep an earlier UTF8_ERROR or UNESCAPED_CHARS: iterate_allow_incomplete_json() + // accepts UNCLOSED_STRING. + error = UNCLOSED_STRING; + } return error; } diff --git a/deps/simdjson/simdjson.h b/deps/simdjson/simdjson.h index 04dbe98eb574..49f66b687dcf 100644 --- a/deps/simdjson/simdjson.h +++ b/deps/simdjson/simdjson.h @@ -1,4 +1,4 @@ -/* auto-generated on 2026-10-31 19:14:12 -0400. version 5.1.1 Do not edit! */ +/* auto-generated on 2026-11-14 18:54:03 -0500. version 5.2.0 Do not edit! */ /* including simdjson.h: */ /* begin file simdjson.h */ #ifndef SIMDJSON_H @@ -205,6 +205,24 @@ #endif #endif // !defined(SIMDJSON_SUPPORTS_CHAR8_T) +// std::string::resize_and_overwrite (C++23) is available. +#if !defined(SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE) +#if defined(__cpp_lib_string_resize_and_overwrite) && __cpp_lib_string_resize_and_overwrite >= 202110L +#define SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE 1 +#else +#define SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE 0 +#endif +#endif // !defined(SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE) + +// std::string can be used in constant expressions (C++20, e.g., libstdc++ 12 or better). +#if !defined(SIMDJSON_SUPPORTS_CONSTEXPR_STRING) +#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#define SIMDJSON_SUPPORTS_CONSTEXPR_STRING 1 +#else +#define SIMDJSON_SUPPORTS_CONSTEXPR_STRING 0 +#endif +#endif // !defined(SIMDJSON_SUPPORTS_CONSTEXPR_STRING) + // The C++23 fixed-width floating-point types std::float32_t and std::float64_t // () are available. They are optional even in C++23: a compiler that // provides them predefines __STDCPP_FLOAT32_T__ and __STDCPP_FLOAT64_T__. @@ -237,11 +255,11 @@ #if !defined(SIMDJSON_CONSTEVAL) -#if defined(__cpp_consteval) && __cpp_consteval >= 201811L && defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#if defined(__cpp_consteval) && __cpp_consteval >= 201811L && SIMDJSON_SUPPORTS_CONSTEXPR_STRING #define SIMDJSON_CONSTEVAL 1 #else #define SIMDJSON_CONSTEVAL 0 -#endif // defined(__cpp_consteval) && __cpp_consteval >= 201811L && defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#endif // defined(__cpp_consteval) && __cpp_consteval >= 201811L && SIMDJSON_SUPPORTS_CONSTEXPR_STRING #endif // !defined(SIMDJSON_CONSTEVAL) // SIMDJSON_CONSTEXPR_STRING is 'constexpr' when the standard library supports @@ -249,11 +267,11 @@ // lets functions that build a std::string be constant expressions when possible // while still compiling against older standard libraries. #if !defined(SIMDJSON_CONSTEXPR_STRING) -#if defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#if SIMDJSON_SUPPORTS_CONSTEXPR_STRING #define SIMDJSON_CONSTEXPR_STRING constexpr #else #define SIMDJSON_CONSTEXPR_STRING -#endif // defined(__cpp_lib_constexpr_string) && __cpp_lib_constexpr_string >= 201907L +#endif // SIMDJSON_SUPPORTS_CONSTEXPR_STRING #endif // !defined(SIMDJSON_CONSTEXPR_STRING) #endif // SIMDJSON_COMPILER_CHECK_H /* end file simdjson/compiler_check.h */ @@ -329,7 +347,25 @@ using std::size_t; #define SIMDJSON_IS_LSX 1 // Adjust for runtime dispatching support. -#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__NVCOMPILER) +// +// LASX runtime dispatching is deliberately disabled by default, so that the +// LSX kernel is used even when the processor supports LASX. On a Loongson +// 3A6000, our LASX kernel parses about 5% slower overall than our LSX +// kernel (stage 1 about 14% slower), with both GCC 15 and LLVM. The 256-bit +// registers do not help: stage 1 is limited by moving bitmasks from vector +// registers to general-purpose registers, and with LASX each bitmask needs +// a cross-lane permutation (3 cycles) and a slower vector-to-GPR move +// (xvpickve2gr is 2 cycles slower than vpickve2gr). +// +// Define SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH to 0 to enable LASX runtime +// dispatching (GCC 15+ or LLVM). Compiling with -mlasx selects the LASX +// kernel regardless of this macro. See doc/implementation-selection.md. +#ifndef SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH + #define SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH 1 +#endif +#if SIMDJSON_DISABLE_LASX_RUNTIME_DISPATCH + #define SIMDJSON_IMPLEMENTATION_LASX 0 +#elif defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__NVCOMPILER) #if __GNUC__ > 15 || (__GNUC__ == 15 && __GNUC_MINOR__ >= 0) // We are ok, we will support runtime dispatch for LASX. #else @@ -338,14 +374,15 @@ using std::size_t; // Loongson users should update to GCC 15 or better. #define SIMDJSON_IMPLEMENTATION_LASX 0 #endif +#elif defined(__clang__) + // LLVM supports runtime dispatch for LASX through the target attribute. + // See simdjson/lasx/intrinsics.h. #else - // We are not using GCC, so we assume that we can support runtime dispatch for LASX. + // We are not using GCC or LLVM, so we do not support runtime dispatch for LASX. // https://godbolt.org/z/jcMnrjYhs #define SIMDJSON_IMPLEMENTATION_LASX 0 #endif - - #endif #elif defined(__PPC64__) || defined(_M_PPC64) #define SIMDJSON_IS_PPC64 1 @@ -2715,7 +2752,7 @@ namespace std { #define SIMDJSON_SIMDJSON_VERSION_H /** The version of simdjson being used (major.minor.revision) */ -#define SIMDJSON_VERSION "5.1.1" +#define SIMDJSON_VERSION "5.2.0" namespace simdjson { enum { @@ -2726,11 +2763,11 @@ enum { /** * The minor version (major.MINOR.revision) of simdjson being used. */ - SIMDJSON_VERSION_MINOR = 1, + SIMDJSON_VERSION_MINOR = 2, /** * The revision (major.minor.REVISION) of simdjson being used. */ - SIMDJSON_VERSION_REVISION = 1 + SIMDJSON_VERSION_REVISION = 0 }; } // namespace simdjson @@ -3202,10 +3239,25 @@ template concept string_view_like = std::is_convertible_v && !std::is_convertible_v; +template +concept optional_type = requires(std::remove_cvref_t obj) { + typename std::remove_cvref_t::value_type; + { obj.value() } -> std::same_as::value_type&>; + requires requires(typename std::remove_cvref_t::value_type &&val) { + obj.emplace(std::move(val)); + { + obj.value_or(val) + } -> std::convertible_to::value_type>; + }; + { static_cast(obj) } -> std::same_as; // convertible to bool + { obj.reset() } noexcept -> std::same_as; +}; + template concept constructible_from_string_view = std::is_constructible_v && !std::is_same_v - && std::is_default_constructible_v; + && std::is_default_constructible_v + && !optional_type; #if SIMDJSON_SUPPORTS_CHAR8_T /** @@ -3217,7 +3269,8 @@ template concept constructible_from_u8string_view = std::is_constructible_v && !std::is_same_v && !std::is_constructible_v - && std::is_default_constructible_v; + && std::is_default_constructible_v + && !optional_type; #endif // SIMDJSON_SUPPORTS_CHAR8_T template @@ -3287,22 +3340,6 @@ concept smart_pointer = requires(std::remove_cvref_t ptr) { { *ptr } -> std::same_as::element_type &>; }; -template -concept optional_type = requires(std::remove_cvref_t obj) { - typename std::remove_cvref_t::value_type; - { obj.value() } -> std::same_as::value_type&>; - requires requires(typename std::remove_cvref_t::value_type &&val) { - obj.emplace(std::move(val)); - { - obj.value_or(val) - } -> std::convertible_to::value_type>; - }; - { static_cast(obj) } -> std::same_as; // convertible to bool - { obj.reset() } noexcept -> std::same_as; -}; - - - // Types we serialize as JSON strings (not as containers) template concept string_like = @@ -5052,6 +5089,11 @@ inline padded_string_view pad_with_reserve(std::string& s) noexcept; * is longer than block_size; skip it and continue. With block_size larger than * the longest document, no slice is ever empty. * + * Cost: a slice searches its own block for its start, and then from the end of + * its block to the next delimiter. An empty slice returns after searching its + * block alone. Cutting the whole input therefore scans each byte about twice, + * whatever the document lengths. + * * @param data The padded input. * @param delimiter The byte that separates documents. * @param block_size The nominal slice size, before snapping. @@ -5266,14 +5308,30 @@ inline padded_string_view slice_at(padded_string_view data, char delimiter, const size_t raw_begin = index * block_size; if (raw_begin >= data.size()) { return {}; } - auto snap = [&](size_t want) -> size_t { - if (want >= data.size()) { return data.size(); } - const void *p = std::memchr(data.data() + want, delimiter, data.size() - want); - return p ? size_t(static_cast(p) - data.data()) + 1 : data.size(); + auto find = [&](size_t from, size_t to) -> const char * { + return static_cast( + std::memchr(data.data() + from, delimiter, to - from)); }; - const size_t begin = (raw_begin == 0) ? 0 : snap(raw_begin); - const size_t end = snap(raw_begin + block_size); + // The slice begins just past the first delimiter in its own block. If the + // block holds no delimiter, it lies inside a document that an earlier slice + // owns, and the slice is empty. Searching past the block would give the same + // answer, but would make every block inside a long document scan to that + // document's end: quadratic time in the document's length. + size_t begin = 0; + if (raw_begin != 0) { + const size_t block_end = (data.size() - raw_begin > block_size) + ? raw_begin + block_size : data.size(); + const char *p = find(raw_begin, block_end); + if (p == nullptr) { return {}; } + begin = size_t(p - data.data()) + 1; + } + // Only the slice that owns a document scans to its end. + size_t end = data.size(); + if (data.size() - raw_begin > block_size) { + const char *p = find(raw_begin + block_size, data.size()); + if (p != nullptr) { end = size_t(p - data.data()) + 1; } + } if (begin >= end) { return {}; } return padded_string_view(data.data() + begin, end - begin, data.capacity() - begin); @@ -6037,7 +6095,7 @@ class array { inline bool operator>=(const iterator& other) const noexcept; inline bool operator>(const iterator& other) const noexcept; - iterator() noexcept = default; + simdjson_inline iterator() noexcept {} iterator(const iterator&) noexcept = default; iterator& operator=(const iterator&) noexcept = default; private: @@ -6065,7 +6123,7 @@ class array { inline bool operator==(const reverse_iterator& other) const noexcept; inline bool operator!=(const reverse_iterator& other) const noexcept; - reverse_iterator() noexcept = default; + simdjson_inline reverse_iterator() noexcept {} reverse_iterator(const reverse_iterator&) noexcept = default; reverse_iterator& operator=(const reverse_iterator&) noexcept = default; private: @@ -6283,7 +6341,7 @@ class document { * * The parser will allocate capacity as needed. */ - document() noexcept = default; + simdjson_inline document() noexcept {} ~document() noexcept = default; /** @@ -7135,7 +7193,7 @@ namespace dom { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -7858,7 +7916,7 @@ class element { * - NO_SUCH_FIELD if the field does not exist in the object * - INCORRECT_TYPE if this is not an object */ - inline simdjson_result operator[](std::string_view key) const noexcept; + simdjson_inline simdjson_result operator[](std::string_view key) const noexcept; /** * Get the value associated with the given key. @@ -7873,7 +7931,7 @@ class element { * - NO_SUCH_FIELD if the field does not exist in the object * - INCORRECT_TYPE if this is not an object */ - inline simdjson_result operator[](const char *key) const noexcept; + simdjson_inline simdjson_result operator[](const char *key) const noexcept; simdjson_result operator[](int) const noexcept = delete; @@ -7963,7 +8021,7 @@ class element { * @return The value associated with this field, or: * - NO_SUCH_FIELD if the field does not exist in the object */ - inline simdjson_result at_key(std::string_view key) const noexcept; + simdjson_inline simdjson_result at_key(std::string_view key) const noexcept; /** * Get the value associated with the given key in a case-insensitive manner. @@ -8153,7 +8211,7 @@ class object { * Returns true if the key in this key/value pair is equal * to the provided string_view. */ - inline bool key_equals(std::string_view o) const noexcept; + simdjson_inline bool key_equals(std::string_view o) const noexcept; /** * Returns true if the key in this key/value pair is equal * to the provided string_view in a case-insensitive manner. @@ -8169,7 +8227,7 @@ class object { */ inline element value() const noexcept; - iterator() noexcept = default; + simdjson_inline iterator() noexcept {} iterator(const iterator&) noexcept = default; iterator& operator=(const iterator&) noexcept = default; private: @@ -8213,7 +8271,7 @@ class object { * - NO_SUCH_FIELD if the field does not exist in the object * - INCORRECT_TYPE if this is not an object */ - inline simdjson_result operator[](std::string_view key) const noexcept; + simdjson_inline simdjson_result operator[](std::string_view key) const noexcept; /** * Get the value associated with the given key. @@ -8230,7 +8288,7 @@ class object { * - NO_SUCH_FIELD if the field does not exist in the object * - INCORRECT_TYPE if this is not an object */ - inline simdjson_result operator[](const char *key) const noexcept; + simdjson_inline simdjson_result operator[](const char *key) const noexcept; simdjson_result operator[](int) const noexcept = delete; /** @@ -8297,7 +8355,7 @@ class object { * @return The value associated with this field, or: * - NO_SUCH_FIELD if the field does not exist in the object */ - inline simdjson_result at_key(std::string_view key) const noexcept; + simdjson_inline simdjson_result at_key(std::string_view key) const noexcept; /** * Gets the values associated with keys of an object @@ -8361,13 +8419,13 @@ struct simdjson_result : public internal::simdjson_result_base operator[](std::string_view key) const noexcept; - inline simdjson_result operator[](const char *key) const noexcept; + simdjson_inline simdjson_result operator[](std::string_view key) const noexcept; + simdjson_inline simdjson_result operator[](const char *key) const noexcept; simdjson_result operator[](int) const noexcept = delete; inline simdjson_result at_pointer(std::string_view json_pointer) const noexcept; inline simdjson_result> at_path_with_wildcard(std::string_view json_path_new) const noexcept; inline simdjson_result at_path(std::string_view json_path) const noexcept; - inline simdjson_result at_key(std::string_view key) const noexcept; + simdjson_inline simdjson_result at_key(std::string_view key) const noexcept; inline std::vector& get_values(std::vector& out) const noexcept; inline simdjson_result at_key_case_insensitive(std::string_view key) const noexcept; @@ -8474,7 +8532,7 @@ template class base_formatter { // implementation details (subject to change) /** Backing buffer **/ struct vector_with_small_buffer { - vector_with_small_buffer() = default; + vector_with_small_buffer() {} ~vector_with_small_buffer() { free_buffer(); } vector_with_small_buffer(const vector_with_small_buffer &) = delete; @@ -8559,6 +8617,8 @@ template class base_formatter { */ class mini_formatter : public base_formatter { public: + /** User-provided so that value-initialization does not zero the buffer. **/ + mini_formatter() {} simdjson_inline void print_newline(); simdjson_inline void print_indents(size_t depth); @@ -8593,7 +8653,7 @@ class pretty_formatter : public base_formatter { template class string_builder { public: /** Construct an initially empty builder, would print the empty string **/ - string_builder() = default; + string_builder() {} /** Construct a builder whose formatter uses the given indentation step **/ explicit string_builder(int indent_step) : format(indent_step) {} @@ -9043,6 +9103,7 @@ simdjson_inline bool is_matching_failure_error(error_code error) noexcept { * @return A string containing the equivalent JSON Pointer. */ inline std::string json_path_to_pointer_conversion(std::string_view json_path) { + if (json_path == "$") { return {}; } size_t i = 0; // if JSONPath starts with $, skip it // json_path.starts_with('$') requires C++20. @@ -9065,7 +9126,16 @@ inline std::string json_path_to_pointer_conversion(std::string_view json_path) { } else if (json_path[i] == '[') { result += '/'; ++i; // Move past the '[' - while (i < json_path.length() && json_path[i] != ']') { + // A bracket-quoted key (['key'] or ["key"]) is the key without its quotes, + // as in get_next_key_and_json_path(). It runs up to the matching quote, + // which the ']' must follow. + char quote = '\0'; + if (i < json_path.length() && (json_path[i] == '\'' || json_path[i] == '"')) { + quote = json_path[i]; + ++i; + } + const char key_end = quote ? quote : ']'; + while (i < json_path.length() && json_path[i] != key_end) { if (json_path[i] == '~') { result += "~0"; } else if (json_path[i] == '/') { @@ -9075,6 +9145,12 @@ inline std::string json_path_to_pointer_conversion(std::string_view json_path) { } ++i; } + if (quote) { + if (i == json_path.length()) { + return "-1"; // missing closing quote + } + ++i; // Move past the closing quote + } if (i == json_path.length() || json_path[i] != ']') { return "-1"; // Using sentinel value that will be handled as an error by the caller. } @@ -9614,7 +9690,8 @@ inline array::iterator array::begin() const noexcept { } inline array::iterator array::end() const noexcept { SIMDJSON_DEVELOPMENT_ASSERT(tape.usable()); // https://github.com/simdjson/simdjson/issues/1914 - return internal::tape_ref(tape.doc, tape.after_element() - 1); + SIMDJSON_DEVELOPMENT_ASSERT(tape.tape_ref_type() == internal::tape_type::START_ARRAY); + return internal::tape_ref(tape.doc, tape.matching_brace_index() - 1); } inline size_t array::size() const noexcept { SIMDJSON_DEVELOPMENT_ASSERT(tape.usable()); // https://github.com/simdjson/simdjson/issues/1914 @@ -9650,14 +9727,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = array(tape).at(array_index); + element child; // If there is an error, it ends here - if(child.error()) { - return child; - } + SIMDJSON_TRY(array(tape).at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -9818,11 +9893,11 @@ simdjson_inline simdjson_result::simdjson_result(dom::object value) simdjson_inline simdjson_result::simdjson_result(error_code error) noexcept : internal::simdjson_result_base(error) {} -inline simdjson_result simdjson_result::operator[](std::string_view key) const noexcept { +simdjson_inline simdjson_result simdjson_result::operator[](std::string_view key) const noexcept { if (error()) { return error(); } return first[key]; } -inline simdjson_result simdjson_result::operator[](const char *key) const noexcept { +simdjson_inline simdjson_result simdjson_result::operator[](const char *key) const noexcept { if (error()) { return error(); } return first[key]; } @@ -9842,7 +9917,7 @@ inline simdjson_result> simdjson_result:: } return first.at_path_with_wildcard(json_path); } -inline simdjson_result simdjson_result::at_key(std::string_view key) const noexcept { +simdjson_inline simdjson_result simdjson_result::at_key(std::string_view key) const noexcept { if (error()) { return error(); } return first.at_key(key); } @@ -9885,17 +9960,18 @@ inline object::iterator object::begin() const noexcept { } inline object::iterator object::end() const noexcept { SIMDJSON_DEVELOPMENT_ASSERT(tape.usable()); // https://github.com/simdjson/simdjson/issues/1914 - return internal::tape_ref(tape.doc, tape.after_element() - 1); + SIMDJSON_DEVELOPMENT_ASSERT(tape.tape_ref_type() == internal::tape_type::START_OBJECT); + return internal::tape_ref(tape.doc, tape.matching_brace_index() - 1); } inline size_t object::size() const noexcept { SIMDJSON_DEVELOPMENT_ASSERT(tape.usable()); // https://github.com/simdjson/simdjson/issues/1914 return tape.scope_count(); } -inline simdjson_result object::operator[](std::string_view key) const noexcept { +simdjson_inline simdjson_result object::operator[](std::string_view key) const noexcept { return at_key(key); } -inline simdjson_result object::operator[](const char *key) const noexcept { +simdjson_inline simdjson_result object::operator[](const char *key) const noexcept { return at_key(key); } inline simdjson_result object::at_pointer(std::string_view json_pointer) const noexcept { @@ -9909,7 +9985,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + element child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -9929,16 +10006,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = at_key(unescaped); + error = at_key(unescaped).get(child); } else { - child = at_key(key); + error = at_key(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -9973,7 +10050,7 @@ inline simdjson_result> object::at_path_with_wildcard(std:: return element(*this).at_path_with_wildcard(json_path); } -inline simdjson_result object::at_key(std::string_view key) const noexcept { +simdjson_inline simdjson_result object::at_key(std::string_view key) const noexcept { iterator end_field = end(); for (iterator field = begin(); field != end_field; ++field) { if (field.key_equals(key)) { @@ -10072,7 +10149,7 @@ inline element object::iterator::value() const noexcept { * on the long run. */ -inline bool object::iterator::key_equals(std::string_view o) const noexcept { +simdjson_inline bool object::iterator::key_equals(std::string_view o) const noexcept { // We use the fact that the key length can be computed quickly // without access to the string buffer. const uint32_t len = key_length(); @@ -10600,10 +10677,10 @@ inline array::iterator element::end() const noexcept(false) { #endif // SIMDJSON_EXCEPTIONS -inline simdjson_result element::operator[](std::string_view key) const noexcept { +simdjson_inline simdjson_result element::operator[](std::string_view key) const noexcept { return at_key(key); } -inline simdjson_result element::operator[](const char *key) const noexcept { +simdjson_inline simdjson_result element::operator[](const char *key) const noexcept { return at_key(key); } @@ -10647,6 +10724,7 @@ inline simdjson_result element::at_pointer(std::string_view json_pointe inline simdjson_result> element::at_path_with_wildcard(std::string_view json_path) const noexcept { SIMDJSON_DEVELOPMENT_ASSERT(tape.usable()); // https://github.com/simdjson/simdjson/issues/1914 + if (json_path == "$") { return std::vector{*this}; } const internal::wildcard_json_path path = internal::parse_wildcard_json_path(json_path); // A path that cannot be parsed matches nothing, whatever the document: say @@ -10680,7 +10758,7 @@ inline simdjson_result element::at(std::string_view json_pointer) const inline simdjson_result element::at(size_t index) const noexcept { return get().at(index); } -inline simdjson_result element::at_key(std::string_view key) const noexcept { +simdjson_inline simdjson_result element::at_key(std::string_view key) const noexcept { return get().at_key(key); } inline simdjson_result element::at_key_case_insensitive(std::string_view key) const noexcept { @@ -11041,6 +11119,7 @@ inline error_code parser::allocate(size_t capacity, size_t max_depth) noexcept { err = implementation->allocate(capacity, max_depth); } else { err = simdjson::get_active_implementation()->create_dom_parser_implementation(capacity, max_depth, implementation); + if (err) { implementation.reset(); } } if (err) { return err; } return SUCCESS; @@ -12145,6 +12224,9 @@ find_next_json_quotable_character_scalar(const std::string_view view, template inline size_t write_string_escaped_with(const std::string_view input, char *out) { size_t mysize = input.size(); + if (mysize == 0) { + return 0; + } // Use SIMD position finder directly - it returns mysize if no escape needed size_t location = Finder::find(input, 0); @@ -16838,6 +16920,9 @@ namespace { simdjson_inline simd8() : simd8(vdupq_n_u8(0)) {} // Splat constructor simdjson_inline simd8(bool _value) : simd8(splat(_value)) {} + // base_u8's operator~ xors with 0xFFu, which simd8 only accepts by + // narrowing it to bool (MSVC C4305). Every other kernel overrides it too. + simdjson_inline simd8 operator~() const { return *this ^ true; } // We return uint32_t instead of uint16_t because that seems to be more efficient for most // purposes (cutting it down to uint16_t costs performance in some compilers). @@ -16937,9 +17022,15 @@ namespace { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return ~any_bits_set(bits); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } simdjson_inline simd8 any_bits_set(simd8 bits) const { return vtstq_u8(*this, bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return vshrq_n_u8(*this, N); } template @@ -17703,8 +17794,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -17714,11 +17810,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -17769,7 +17868,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -18982,6 +19081,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -19074,6 +19176,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -19356,6 +19461,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -19452,6 +19560,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -20342,8 +20453,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -20353,11 +20469,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -20408,7 +20527,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -21621,6 +21740,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -21713,6 +21835,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -21995,6 +22120,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -22091,6 +22219,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -23451,8 +23582,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -23462,11 +23598,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -23517,7 +23656,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -24730,6 +24869,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -24822,6 +24964,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -25104,6 +25249,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -25200,6 +25348,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -26556,8 +26707,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -26567,11 +26723,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -26622,7 +26781,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -27835,6 +27994,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -27927,6 +28089,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -28209,6 +28374,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -28305,6 +28473,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -29175,7 +29346,7 @@ template <> struct simd8 : base8_numeric { } simdjson_inline simd8 operator<(const simd8 other) const { - return this->gt_bits(other).any_bits_set(); + return this->lt_bits(other).any_bits_set(); } // Bit-specific operations @@ -29204,6 +29375,9 @@ template <> struct simd8 : base8_numeric { simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return !bits_not_set_anywhere(bits); } + simdjson_inline bool is_ascii() const { + return this->bits_not_set_anywhere(uint8_t(0x80)); + } template simdjson_inline simd8 shr() const { return simd8( (__m128i)vec_sr(this->value, (__m128i)vec_splat_u8(N))); @@ -29779,8 +29953,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -29790,11 +29969,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -29845,7 +30027,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -31058,6 +31240,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -31150,6 +31335,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -31432,6 +31620,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -31528,6 +31719,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -32333,7 +32527,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -32752,7 +32946,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -33310,8 +33504,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -33321,11 +33520,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -33376,7 +33578,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -34589,6 +34791,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -34681,6 +34886,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -34963,6 +35171,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -35059,6 +35270,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -35369,7 +35583,18 @@ template struct simd8x64; /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ #include +#if defined(__clang__) && !defined(__loongarch_asx) +// LLVM's lasxintrin.h only declares the LASX types and functions when +// __loongarch_asx is defined, that is, when the code is compiled with -mlasx. +// For runtime dispatching, we include it inside a target("lasx") region +// (see simdjson/lasx/begin.h) where the LASX builtins are available, so we +// define __loongarch_asx while including the header. +#define __loongarch_asx 1 #include +#undef __loongarch_asx +#else +#include +#endif static_assert(sizeof(__m256i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch ASX"); @@ -35588,10 +35813,7 @@ namespace simd { __m256i mask = __lasx_xvmskltz_b(*this); return (__lasx_xvpickve2gr_w(mask, 4) << 16) | (__lasx_xvpickve2gr_w(mask, 0)); } - simdjson_inline bool any() const { - __m256i v = __lasx_xvmsknz_b(*this); - return (0 == __lasx_xvpickve2gr_w(v, 0)) && (0 == __lasx_xvpickve2gr_w(v, 4)); - } + simdjson_inline bool any() const { return !__lasx_xbz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -36357,8 +36579,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -36368,11 +36595,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -36423,7 +36653,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -37636,6 +37866,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -37728,6 +37961,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -38010,6 +38246,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -38106,6 +38345,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -38626,7 +38868,7 @@ namespace simd { simdjson_inline simd8(bool _value) : base8(splat(_value)) {} simdjson_inline int to_bitmask() const { return __lsx_vpickve2gr_w(__lsx_vmskltz_b(*this), 0); } - simdjson_inline bool any() const { return 0 == __lsx_vpickve2gr_hu(__lsx_vmsknz_b(*this), 0); } + simdjson_inline bool any() const { return !__lsx_bz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -39382,8 +39624,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -39393,11 +39640,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -39448,7 +39698,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -40661,6 +40911,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -40753,6 +41006,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -41035,6 +41291,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -41131,6 +41390,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -41618,7 +41880,9 @@ namespace simd { simdjson_inline simd8 operator|(const simd8 other) const { return __riscv_vmor(*this, other, VL8); } simdjson_inline simd8 operator&(const simd8 other) const { return __riscv_vmand(*this, other, VL8); } simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vmxor(*this, other, VL8); } - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(other, *this, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(*this, other, VL8); } + simdjson_inline simd8 operator==(const simd8 other) const { return __riscv_vmxnor(*this, other, VL8); } + simdjson_inline bool any() const { return __riscv_vfirst(value, VL8) >= 0; } simdjson_inline simd8 operator~() const { return __riscv_vmnot(*this, VL8); } simdjson_inline simd8& operator|=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast | other; return *this_cast; } simdjson_inline simd8& operator&=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast & other; return *this_cast; } @@ -41670,9 +41934,9 @@ namespace simd { simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vxor_vv_u8m1( value, other, VL8); } simdjson_inline simd8 operator~() const { return __riscv_vnot_v_u8m1(value, VL8); } #if __riscv_zvbb - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(other, value, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(value, other, VL8); } #else - simdjson_inline simd8 bit_andnot(const simd8 other) const { return other & ~*this; } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return *this & ~other; } #endif simdjson_inline simd8& operator|=(const simd8 other) { value = *this | other; return *this; } simdjson_inline simd8& operator&=(const simd8 other) { value = *this & other; return *this; } @@ -41700,6 +41964,8 @@ namespace simd { simdjson_inline simd8& operator-=(const simd8 other) { value = *this - other; return *this; } // Order-specific operations + simdjson_inline simd8 max_val(const simd8 other) const { return __riscv_vmaxu(value, other, VL8); } + simdjson_inline simd8 min_val(const simd8 other) const { return __riscv_vminu(value, other, VL8); } simdjson_inline simd8 operator<=(const simd8 other) const { return __riscv_vmsleu(value, other, VL8); } simdjson_inline simd8 operator>=(const simd8 other) const { return __riscv_vmsgeu(value, other, VL8); } simdjson_inline simd8 operator<(const simd8 other) const { return __riscv_vmsltu(value, other, VL8); } @@ -41711,10 +41977,17 @@ namespace simd { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return (*this & bits).bits_not_set(); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } + simdjson_inline simd8 any_bits_set(simd8 bits) const { return ~this->bits_not_set(bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return __riscv_vfirst(__riscv_vmsne(value, 0, VL8), VL8) >= 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return __riscv_vsrl(value, N, VL8); } template @@ -42430,8 +42703,13 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s } size_t max_structures = rounded_capacity + 9; if(max_structures > SIZE_MAX / sizeof(uint32_t)) { return CAPACITY; } - structural_indexes.reset( new (std::nothrow) uint32_t[max_structures] ); - if (!structural_indexes) { _capacity = 0; return MEMALLOC; } + // Allocate before releasing the current buffer so that, on failure, the + // implementation keeps its previous buffer and capacity. Otherwise a parser + // that still believes it has room skips reallocation and stage 1 rejects + // every document with CAPACITY. + std::unique_ptr new_structural_indexes(new (std::nothrow) uint32_t[max_structures]); + if (!new_structural_indexes) { return MEMALLOC; } + structural_indexes = std::move(new_structural_indexes); structural_indexes[0] = 0; n_structural_indexes = 0; @@ -42441,11 +42719,14 @@ inline simdjson_warn_unused error_code dom_parser_implementation::set_capacity(s inline simdjson_warn_unused error_code dom_parser_implementation::set_max_depth(size_t max_depth) noexcept { if(max_depth == 0 || max_depth > SIMDJSON_MAX_DEPTH || max_depth > SIZE_MAX / sizeof(open_container)) { return CAPACITY; } - // Stage 2 stacks - open_containers.reset(new (std::nothrow) open_container[max_depth]); - is_array.reset(new (std::nothrow) bool[max_depth]); - if (!is_array || !open_containers) { _max_depth = 0; return MEMALLOC; } - + // Stage 2 stacks. Allocate both before touching the parser so that, on + // failure, it keeps its previous stacks and depth: a parser whose depth does + // not match its stacks would crash in stage 2. + std::unique_ptr new_open_containers(new (std::nothrow) open_container[max_depth]); + std::unique_ptr new_is_array(new (std::nothrow) bool[max_depth]); + if (!new_is_array || !new_open_containers) { return MEMALLOC; } + open_containers = std::move(new_open_containers); + is_array = std::move(new_is_array); _max_depth = max_depth; return SUCCESS; } @@ -42496,7 +42777,7 @@ struct implementation_simdjson_result_base { /** * Create a new empty result with error = UNINITIALIZED. */ - simdjson_inline implementation_simdjson_result_base() noexcept = default; + simdjson_inline implementation_simdjson_result_base() noexcept {} /** * Create a new error result. @@ -43709,6 +43990,9 @@ simdjson_unused simdjson_inline simdjson_result parse_unsigned(const u } // Parse any number from 0 to 18,446,744,073,709,551,615 simdjson_unused simdjson_inline simdjson_result parse_unsigned_in_string(const uint8_t * const src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } const uint8_t *p = src + 1; // // Parse the integer part. @@ -43801,6 +44085,9 @@ simdjson_unused simdjson_inline simdjson_result parse_integer(const uin // Parse any number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 simdjson_unused simdjson_inline simdjson_result parse_integer_in_string(const uint8_t *src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -44083,6 +44370,9 @@ simdjson_unused simdjson_inline simdjson_result get_number_type(con } simdjson_unused simdjson_inline simdjson_result parse_double_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -44179,6 +44469,9 @@ simdjson_unused simdjson_inline simdjson_result parse_double_in_string(c // Parse a JSON number held inside a JSON string into a binary32 (float) value. // See parse_float for why we do not simply round parse_double_in_string. simdjson_unused simdjson_inline simdjson_result parse_float_in_string(const uint8_t * src) noexcept { + // The number must be inside a string: anything else (a bare number, an + // array, an object...) is not the requested type. + if (*src != '"') { return INCORRECT_TYPE; } // // Check for minus sign // @@ -45235,6 +45528,9 @@ namespace { simdjson_inline simd8() : simd8(vdupq_n_u8(0)) {} // Splat constructor simdjson_inline simd8(bool _value) : simd8(splat(_value)) {} + // base_u8's operator~ xors with 0xFFu, which simd8 only accepts by + // narrowing it to bool (MSVC C4305). Every other kernel overrides it too. + simdjson_inline simd8 operator~() const { return *this ^ true; } // We return uint32_t instead of uint16_t because that seems to be more efficient for most // purposes (cutting it down to uint16_t costs performance in some compilers). @@ -45334,9 +45630,15 @@ namespace { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return ~any_bits_set(bits); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } simdjson_inline simd8 any_bits_set(simd8 bits) const { return vtstq_u8(*this, bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return vshrq_n_u8(*this, N); } template @@ -45927,7 +46229,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -47447,14 +47765,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -47553,10 +47871,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -48220,7 +48545,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -49740,14 +50081,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -49846,10 +50187,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -50983,7 +51331,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -52503,14 +52867,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -52609,10 +52973,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -53742,7 +54113,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -55262,14 +55649,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -55368,10 +55755,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -56188,7 +56582,7 @@ template <> struct simd8 : base8_numeric { } simdjson_inline simd8 operator<(const simd8 other) const { - return this->gt_bits(other).any_bits_set(); + return this->lt_bits(other).any_bits_set(); } // Bit-specific operations @@ -56217,6 +56611,9 @@ template <> struct simd8 : base8_numeric { simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return !bits_not_set_anywhere(bits); } + simdjson_inline bool is_ascii() const { + return this->bits_not_set_anywhere(uint8_t(0x80)); + } template simdjson_inline simd8 shr() const { return simd8( (__m128i)vec_sr(this->value, (__m128i)vec_splat_u8(N))); @@ -56619,7 +57016,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -58139,14 +58552,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -58245,10 +58658,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -59000,7 +59420,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -59419,7 +59839,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -59804,7 +60224,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -61324,14 +61760,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -61430,10 +61866,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -61896,7 +62339,7 @@ namespace simd { simdjson_inline simd8(bool _value) : base8(splat(_value)) {} simdjson_inline int to_bitmask() const { return __lsx_vpickve2gr_w(__lsx_vmskltz_b(*this), 0); } - simdjson_inline bool any() const { return 0 == __lsx_vpickve2gr_hu(__lsx_vmsknz_b(*this), 0); } + simdjson_inline bool any() const { return !__lsx_bz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -62479,7 +62922,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -63999,14 +64458,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -64105,10 +64564,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -64362,7 +64828,18 @@ template struct simd8x64; /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ #include +#if defined(__clang__) && !defined(__loongarch_asx) +// LLVM's lasxintrin.h only declares the LASX types and functions when +// __loongarch_asx is defined, that is, when the code is compiled with -mlasx. +// For runtime dispatching, we include it inside a target("lasx") region +// (see simdjson/lasx/begin.h) where the LASX builtins are available, so we +// define __loongarch_asx while including the header. +#define __loongarch_asx 1 #include +#undef __loongarch_asx +#else +#include +#endif static_assert(sizeof(__m256i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch ASX"); @@ -64581,10 +65058,7 @@ namespace simd { __m256i mask = __lasx_xvmskltz_b(*this); return (__lasx_xvpickve2gr_w(mask, 4) << 16) | (__lasx_xvpickve2gr_w(mask, 0)); } - simdjson_inline bool any() const { - __m256i v = __lasx_xvmsknz_b(*this); - return (0 == __lasx_xvpickve2gr_w(v, 0)) && (0 == __lasx_xvpickve2gr_w(v, 4)); - } + simdjson_inline bool any() const { return !__lasx_xbz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -65177,7 +65651,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -66697,14 +67187,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -66803,10 +67293,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -67246,7 +67743,9 @@ namespace simd { simdjson_inline simd8 operator|(const simd8 other) const { return __riscv_vmor(*this, other, VL8); } simdjson_inline simd8 operator&(const simd8 other) const { return __riscv_vmand(*this, other, VL8); } simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vmxor(*this, other, VL8); } - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(other, *this, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(*this, other, VL8); } + simdjson_inline simd8 operator==(const simd8 other) const { return __riscv_vmxnor(*this, other, VL8); } + simdjson_inline bool any() const { return __riscv_vfirst(value, VL8) >= 0; } simdjson_inline simd8 operator~() const { return __riscv_vmnot(*this, VL8); } simdjson_inline simd8& operator|=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast | other; return *this_cast; } simdjson_inline simd8& operator&=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast & other; return *this_cast; } @@ -67298,9 +67797,9 @@ namespace simd { simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vxor_vv_u8m1( value, other, VL8); } simdjson_inline simd8 operator~() const { return __riscv_vnot_v_u8m1(value, VL8); } #if __riscv_zvbb - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(other, value, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(value, other, VL8); } #else - simdjson_inline simd8 bit_andnot(const simd8 other) const { return other & ~*this; } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return *this & ~other; } #endif simdjson_inline simd8& operator|=(const simd8 other) { value = *this | other; return *this; } simdjson_inline simd8& operator&=(const simd8 other) { value = *this & other; return *this; } @@ -67328,6 +67827,8 @@ namespace simd { simdjson_inline simd8& operator-=(const simd8 other) { value = *this - other; return *this; } // Order-specific operations + simdjson_inline simd8 max_val(const simd8 other) const { return __riscv_vmaxu(value, other, VL8); } + simdjson_inline simd8 min_val(const simd8 other) const { return __riscv_vminu(value, other, VL8); } simdjson_inline simd8 operator<=(const simd8 other) const { return __riscv_vmsleu(value, other, VL8); } simdjson_inline simd8 operator>=(const simd8 other) const { return __riscv_vmsgeu(value, other, VL8); } simdjson_inline simd8 operator<(const simd8 other) const { return __riscv_vmsltu(value, other, VL8); } @@ -67339,10 +67840,17 @@ namespace simd { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return (*this & bits).bits_not_set(); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } + simdjson_inline simd8 any_bits_set(simd8 bits) const { return ~this->bits_not_set(bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return __riscv_vfirst(__riscv_vmsne(value, 0, VL8), VL8) >= 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return __riscv_vsrl(value, N, VL8); } template @@ -67885,7 +68393,23 @@ requires (!std::is_convertible::value && !concepts::optiona * The result may not be valid UTF-8 if some of your content was not valid UTF-8. * Use validate_unicode() to check the content if needed. */ - simdjson_inline operator std::string() const noexcept(false); + simdjson_inline operator std::string() const & noexcept(false); + + /** + * Moves the written JSON buffer out of the builder into an std::string, + * see move_to(). The builder is left empty and can be reused. + * Throws if memory allocation failed. + * + * Used for copy-initialization from an rvalue, e.g. + * `std::string s = std::move(builder);`, and for `return builder;` from a + * function returning std::string (implicit move, C++20 or as an extension). + * Direct-initialization, `std::string s(std::move(builder));`, may copy + * through operator std::string_view() instead, depending on the compiler. + * + * The result may not be valid UTF-8 if some of your content was not valid UTF-8. + * Use validate_unicode() to check the content if needed. + */ + inline operator std::string() && noexcept(false); /** * Creates an std::string_view from the written JSON buffer. @@ -69405,14 +69929,14 @@ simdjson_inline void string_builder::append_raw(const char *c) noexcept { simdjson_inline void string_builder::append_raw(std::string_view input) noexcept { - if (capacity_check(input.size())) { - std::memcpy(buffer_data() + position, input.data(), input.size()); - position += input.size(); - } + append_raw(input.data(), input.size()); } simdjson_inline void string_builder::append_raw(const char *str, size_t len) noexcept { + if (len == 0) { + return; + } if (capacity_check(len)) { std::memcpy(buffer_data() + position, str, len); position += len; @@ -69511,10 +70035,17 @@ simdjson_inline void string_builder::append(const R &range) noexcept { #endif #if SIMDJSON_EXCEPTIONS -simdjson_inline string_builder::operator std::string() const noexcept(false) { +simdjson_inline string_builder::operator std::string() const & noexcept(false) { return std::string(operator std::string_view()); } +inline string_builder::operator std::string() && noexcept(false) { + std::string out; + error_code error = move_to(out); + if (error) { throw simdjson_error(error); } + return out; +} + simdjson_inline string_builder::operator std::string_view() const noexcept(false) simdjson_lifetime_bound { return view(); @@ -70182,6 +70713,9 @@ namespace { simdjson_inline simd8() : simd8(vdupq_n_u8(0)) {} // Splat constructor simdjson_inline simd8(bool _value) : simd8(splat(_value)) {} + // base_u8's operator~ xors with 0xFFu, which simd8 only accepts by + // narrowing it to bool (MSVC C4305). Every other kernel overrides it too. + simdjson_inline simd8 operator~() const { return *this ^ true; } // We return uint32_t instead of uint16_t because that seems to be more efficient for most // purposes (cutting it down to uint16_t costs performance in some compilers). @@ -70281,9 +70815,15 @@ namespace { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return ~any_bits_set(bits); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } simdjson_inline simd8 any_bits_set(simd8 bits) const { return vtstq_u8(*this, bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return vmaxvq_u32(vreinterpretq_u32_u8(*this)) != 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return vshrq_n_u8(*this, N); } template @@ -70834,7 +71374,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -70842,6 +71390,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -71159,6 +71716,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -71303,7 +71866,7 @@ struct simdjson_result : public arm64::implemen public: simdjson_inline simdjson_result(arm64::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -72211,7 +72774,7 @@ struct simdjson_result : public arm64::implementation_si public: simdjson_inline simdjson_result(arm64::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -72484,7 +73047,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -72610,7 +73173,7 @@ struct simdjson_result : public arm64::implemen public: simdjson_inline simdjson_result(arm64::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -72687,7 +73250,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -72696,6 +73259,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -72712,6 +73291,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -72891,6 +73476,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -72963,7 +73554,7 @@ struct simdjson_result : public arm64::implement simdjson_inline simdjson_result(arm64::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -73126,7 +73717,7 @@ struct simdjson_result : public arm64::implementatio public: simdjson_inline simdjson_result(arm64::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -73178,7 +73769,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -73294,6 +73885,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -73333,6 +73938,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -73359,7 +73965,7 @@ struct simdjson_result : public arm64::impleme public: simdjson_inline simdjson_result(arm64::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -73821,6 +74427,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -73828,6 +74438,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -73848,7 +74462,7 @@ struct simdjson_result : public arm64::implementation_s public: simdjson_inline simdjson_result(arm64::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -73884,7 +74498,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -74131,7 +74745,7 @@ struct simdjson_result : public arm64::implementation_si public: simdjson_inline simdjson_result(arm64::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -74212,7 +74826,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -74270,6 +74884,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -74294,7 +74909,7 @@ struct simdjson_result : public arm64::implemen simdjson_inline simdjson_result(arm64::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -74344,7 +74959,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -75467,7 +76082,7 @@ struct simdjson_result : public arm64::implementation public: simdjson_inline simdjson_result(arm64::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -75597,7 +76212,7 @@ template<> struct simdjson_result : public arm64::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(arm64::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -75744,7 +76359,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -76094,7 +76709,7 @@ struct simdjson_result : public arm64::impleme public: simdjson_inline simdjson_result(arm64::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -76222,7 +76837,7 @@ struct simdjson_result : public arm64::implementation_si public: simdjson_inline simdjson_result(arm64::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -77766,7 +78381,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -77788,7 +78403,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -78236,7 +78851,7 @@ struct simdjson_result : public arm64::implementation_s public: simdjson_inline simdjson_result(arm64::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -78355,7 +78970,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -78396,6 +79011,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -78413,7 +79029,7 @@ struct simdjson_result : public arm64::impleme public: simdjson_inline simdjson_result(arm64::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -78472,7 +79088,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -78516,7 +79132,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -78544,7 +79160,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -78572,7 +79188,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -78834,6 +79450,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -78887,6 +79507,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -78904,7 +79534,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); arm64::ondemand::array arr; if constexpr (std::is_same_v, arm64::ondemand::array>) { arr = val; @@ -78984,35 +79614,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - arm64::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, arm64::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + arm64::ondemand::object obj; + if constexpr (std::is_same_v, arm64::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -79029,27 +79646,6 @@ error_code tag_invoke(deserialize_tag, arm64::ondemand::object &obj, T &out) noe return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, arm64::ondemand::value &val, T &out) noexcept(false) { - arm64::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, arm64::ondemand::document &doc, T &out) noexcept(false) { - arm64::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, arm64::ondemand::document_reference &doc, T &out) noexcept(false) { - arm64::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -79072,7 +79668,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -79095,10 +79691,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -79107,10 +79711,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -79132,6 +79748,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -79900,15 +80518,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -80088,12 +80703,12 @@ namespace arm64 { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -80106,7 +80721,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -80119,6 +80735,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -80128,7 +80745,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -80139,6 +80757,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -80166,8 +80785,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -80622,6 +81242,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -80641,6 +81262,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -81436,6 +82062,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -81463,7 +82097,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -81491,6 +82125,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -82821,6 +83461,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -82839,7 +83550,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -82853,7 +83565,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -83070,10 +83783,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -83305,14 +84018,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -83340,7 +84147,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -83350,6 +84157,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -83360,53 +84175,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -83432,6 +84248,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -83452,6 +84282,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -83609,6 +84445,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -84208,7 +85084,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -84366,7 +85242,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -84386,16 +85263,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -84755,7 +85632,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -84768,6 +85646,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -84777,7 +85656,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -84788,6 +85668,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -84802,6 +85683,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -84827,7 +85709,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -85087,25 +85969,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -85152,7 +86046,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -85422,7 +86349,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -85442,7 +86369,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -85474,6 +86401,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -85775,13 +86736,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -85841,10 +86811,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -85894,7 +86878,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -85903,8 +86887,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -85912,14 +86894,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -85927,9 +86902,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -86009,7 +86984,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -86036,8 +87011,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -86046,14 +87019,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -86061,9 +87027,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -86086,8 +87052,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -86096,14 +87060,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -86111,9 +87068,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -86158,6 +87115,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -86248,6 +87213,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -86271,15 +87241,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -86290,8 +87254,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -86440,15 +87403,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -86650,6 +87607,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -86709,6 +87683,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -86772,6 +87753,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -86888,7 +87875,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -88327,7 +89319,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -88335,6 +89335,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -88652,6 +89661,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -88796,7 +89811,7 @@ struct simdjson_result : public fallback::im public: simdjson_inline simdjson_result(fallback::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -89704,7 +90719,7 @@ struct simdjson_result : public fallback::implementat public: simdjson_inline simdjson_result(fallback::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -89977,7 +90992,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -90103,7 +91118,7 @@ struct simdjson_result : public fallback::im public: simdjson_inline simdjson_result(fallback::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -90180,7 +91195,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -90189,6 +91204,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -90205,6 +91236,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -90384,6 +91421,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -90456,7 +91499,7 @@ struct simdjson_result : public fallback::imp simdjson_inline simdjson_result(fallback::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -90619,7 +91662,7 @@ struct simdjson_result : public fallback::impleme public: simdjson_inline simdjson_result(fallback::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -90671,7 +91714,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -90787,6 +91830,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -90826,6 +91883,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -90852,7 +91910,7 @@ struct simdjson_result : public fallback::i public: simdjson_inline simdjson_result(fallback::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -91314,6 +92372,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -91321,6 +92383,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -91341,7 +92407,7 @@ struct simdjson_result : public fallback::implementa public: simdjson_inline simdjson_result(fallback::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -91377,7 +92443,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -91624,7 +92690,7 @@ struct simdjson_result : public fallback::implementat public: simdjson_inline simdjson_result(fallback::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -91705,7 +92771,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -91763,6 +92829,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -91787,7 +92854,7 @@ struct simdjson_result : public fallback::im simdjson_inline simdjson_result(fallback::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -91837,7 +92904,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -92960,7 +94027,7 @@ struct simdjson_result : public fallback::implemen public: simdjson_inline simdjson_result(fallback::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -93090,7 +94157,7 @@ template<> struct simdjson_result : public fallback::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(fallback::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -93237,7 +94304,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -93587,7 +94654,7 @@ struct simdjson_result : public fallback::i public: simdjson_inline simdjson_result(fallback::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -93715,7 +94782,7 @@ struct simdjson_result : public fallback::implementat public: simdjson_inline simdjson_result(fallback::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -95259,7 +96326,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -95281,7 +96348,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -95729,7 +96796,7 @@ struct simdjson_result : public fallback::implementa public: simdjson_inline simdjson_result(fallback::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -95848,7 +96915,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -95889,6 +96956,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -95906,7 +96974,7 @@ struct simdjson_result : public fallback::i public: simdjson_inline simdjson_result(fallback::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -95965,7 +97033,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -96009,7 +97077,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -96037,7 +97105,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -96065,7 +97133,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -96327,6 +97395,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -96380,6 +97452,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -96397,7 +97479,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); fallback::ondemand::array arr; if constexpr (std::is_same_v, fallback::ondemand::array>) { arr = val; @@ -96477,35 +97559,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - fallback::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, fallback::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + fallback::ondemand::object obj; + if constexpr (std::is_same_v, fallback::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -96522,27 +97591,6 @@ error_code tag_invoke(deserialize_tag, fallback::ondemand::object &obj, T &out) return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, fallback::ondemand::value &val, T &out) noexcept(false) { - fallback::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, fallback::ondemand::document &doc, T &out) noexcept(false) { - fallback::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, fallback::ondemand::document_reference &doc, T &out) noexcept(false) { - fallback::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -96565,7 +97613,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -96588,10 +97636,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -96600,10 +97656,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -96625,6 +97693,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -97393,15 +98463,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -97581,12 +98648,12 @@ namespace fallback { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -97599,7 +98666,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -97612,6 +98680,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -97621,7 +98690,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -97632,6 +98702,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -97659,8 +98730,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -98115,6 +99187,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -98134,6 +99207,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -98929,6 +100007,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -98956,7 +100042,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -98984,6 +100070,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -100314,6 +101406,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -100332,7 +101495,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -100346,7 +101510,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -100563,10 +101728,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -100798,14 +101963,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -100833,7 +102092,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -100843,6 +102102,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -100853,53 +102120,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -100925,6 +102193,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -100945,6 +102227,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -101102,6 +102390,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -101701,7 +103029,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -101859,7 +103187,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -101879,16 +103208,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -102248,7 +103577,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -102261,6 +103591,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -102270,7 +103601,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -102281,6 +103613,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -102295,6 +103628,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -102320,7 +103654,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -102580,25 +103914,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -102645,7 +103991,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -102915,7 +104294,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -102935,7 +104314,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -102967,6 +104346,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -103268,13 +104681,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -103334,10 +104756,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -103387,7 +104823,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -103396,8 +104832,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -103405,14 +104839,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -103420,9 +104847,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -103502,7 +104929,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -103529,8 +104956,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -103539,14 +104964,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -103554,9 +104972,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -103579,8 +104997,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -103589,14 +105005,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -103604,9 +105013,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -103651,6 +105060,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -103741,6 +105158,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -103764,15 +105186,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -103783,8 +105199,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -103933,15 +105348,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -104143,6 +105552,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -104202,6 +105628,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -104265,6 +105698,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -104381,7 +105820,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -106290,7 +107734,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -106298,6 +107750,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -106615,6 +108076,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -106759,7 +108226,7 @@ struct simdjson_result : public haswell::impl public: simdjson_inline simdjson_result(haswell::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -107667,7 +109134,7 @@ struct simdjson_result : public haswell::implementatio public: simdjson_inline simdjson_result(haswell::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -107940,7 +109407,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -108066,7 +109533,7 @@ struct simdjson_result : public haswell::impl public: simdjson_inline simdjson_result(haswell::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -108143,7 +109610,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -108152,6 +109619,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -108168,6 +109651,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -108347,6 +109836,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -108419,7 +109914,7 @@ struct simdjson_result : public haswell::imple simdjson_inline simdjson_result(haswell::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -108582,7 +110077,7 @@ struct simdjson_result : public haswell::implement public: simdjson_inline simdjson_result(haswell::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -108634,7 +110129,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -108750,6 +110245,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -108789,6 +110298,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -108815,7 +110325,7 @@ struct simdjson_result : public haswell::imp public: simdjson_inline simdjson_result(haswell::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -109277,6 +110787,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -109284,6 +110798,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -109304,7 +110822,7 @@ struct simdjson_result : public haswell::implementati public: simdjson_inline simdjson_result(haswell::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -109340,7 +110858,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -109587,7 +111105,7 @@ struct simdjson_result : public haswell::implementatio public: simdjson_inline simdjson_result(haswell::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -109668,7 +111186,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -109726,6 +111244,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -109750,7 +111269,7 @@ struct simdjson_result : public haswell::impl simdjson_inline simdjson_result(haswell::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -109800,7 +111319,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -110923,7 +112442,7 @@ struct simdjson_result : public haswell::implementa public: simdjson_inline simdjson_result(haswell::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -111053,7 +112572,7 @@ template<> struct simdjson_result : public haswell::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(haswell::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -111200,7 +112719,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -111550,7 +113069,7 @@ struct simdjson_result : public haswell::imp public: simdjson_inline simdjson_result(haswell::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -111678,7 +113197,7 @@ struct simdjson_result : public haswell::implementatio public: simdjson_inline simdjson_result(haswell::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -113222,7 +114741,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -113244,7 +114763,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -113692,7 +115211,7 @@ struct simdjson_result : public haswell::implementati public: simdjson_inline simdjson_result(haswell::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -113811,7 +115330,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -113852,6 +115371,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -113869,7 +115389,7 @@ struct simdjson_result : public haswell::imp public: simdjson_inline simdjson_result(haswell::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -113928,7 +115448,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -113972,7 +115492,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -114000,7 +115520,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -114028,7 +115548,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -114290,6 +115810,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -114343,6 +115867,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -114360,7 +115894,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); haswell::ondemand::array arr; if constexpr (std::is_same_v, haswell::ondemand::array>) { arr = val; @@ -114440,35 +115974,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - haswell::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, haswell::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + haswell::ondemand::object obj; + if constexpr (std::is_same_v, haswell::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -114485,27 +116006,6 @@ error_code tag_invoke(deserialize_tag, haswell::ondemand::object &obj, T &out) n return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, haswell::ondemand::value &val, T &out) noexcept(false) { - haswell::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, haswell::ondemand::document &doc, T &out) noexcept(false) { - haswell::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, haswell::ondemand::document_reference &doc, T &out) noexcept(false) { - haswell::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -114528,7 +116028,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -114551,10 +116051,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -114563,10 +116071,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -114588,6 +116108,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -115356,15 +116878,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -115544,12 +117063,12 @@ namespace haswell { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -115562,7 +117081,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -115575,6 +117095,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -115584,7 +117105,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -115595,6 +117117,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -115622,8 +117145,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -116078,6 +117602,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -116097,6 +117622,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -116892,6 +118422,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -116919,7 +118457,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -116947,6 +118485,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -118277,6 +119821,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -118295,7 +119910,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -118309,7 +119925,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -118526,10 +120143,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -118761,14 +120378,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -118796,7 +120507,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -118806,6 +120517,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -118816,53 +120535,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -118888,6 +120608,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -118908,6 +120642,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -119065,6 +120805,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -119664,7 +121444,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -119822,7 +121602,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -119842,16 +121623,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -120211,7 +121992,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -120224,6 +122006,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -120233,7 +122016,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -120244,6 +122028,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -120258,6 +122043,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -120283,7 +122069,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -120543,25 +122329,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -120608,7 +122406,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -120878,7 +122709,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -120898,7 +122729,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -120930,6 +122761,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -121231,13 +123096,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -121297,10 +123171,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -121350,7 +123238,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -121359,8 +123247,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -121368,14 +123254,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -121383,9 +123262,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -121465,7 +123344,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -121492,8 +123371,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -121502,14 +123379,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -121517,9 +123387,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -121542,8 +123412,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -121552,14 +123420,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -121567,9 +123428,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -121614,6 +123475,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -121704,6 +123573,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -121727,15 +123601,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -121746,8 +123614,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -121896,15 +123763,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -122106,6 +123967,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -122165,6 +124043,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -122228,6 +124113,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -122344,7 +124235,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -124249,7 +126145,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -124257,6 +126161,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -124574,6 +126487,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -124718,7 +126637,7 @@ struct simdjson_result : public icelake::impl public: simdjson_inline simdjson_result(icelake::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -125626,7 +127545,7 @@ struct simdjson_result : public icelake::implementatio public: simdjson_inline simdjson_result(icelake::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -125899,7 +127818,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -126025,7 +127944,7 @@ struct simdjson_result : public icelake::impl public: simdjson_inline simdjson_result(icelake::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -126102,7 +128021,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -126111,6 +128030,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -126127,6 +128062,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -126306,6 +128247,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -126378,7 +128325,7 @@ struct simdjson_result : public icelake::imple simdjson_inline simdjson_result(icelake::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -126541,7 +128488,7 @@ struct simdjson_result : public icelake::implement public: simdjson_inline simdjson_result(icelake::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -126593,7 +128540,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -126709,6 +128656,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -126748,6 +128709,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -126774,7 +128736,7 @@ struct simdjson_result : public icelake::imp public: simdjson_inline simdjson_result(icelake::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -127236,6 +129198,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -127243,6 +129209,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -127263,7 +129233,7 @@ struct simdjson_result : public icelake::implementati public: simdjson_inline simdjson_result(icelake::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -127299,7 +129269,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -127546,7 +129516,7 @@ struct simdjson_result : public icelake::implementatio public: simdjson_inline simdjson_result(icelake::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -127627,7 +129597,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -127685,6 +129655,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -127709,7 +129680,7 @@ struct simdjson_result : public icelake::impl simdjson_inline simdjson_result(icelake::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -127759,7 +129730,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -128882,7 +130853,7 @@ struct simdjson_result : public icelake::implementa public: simdjson_inline simdjson_result(icelake::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -129012,7 +130983,7 @@ template<> struct simdjson_result : public icelake::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(icelake::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -129159,7 +131130,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -129509,7 +131480,7 @@ struct simdjson_result : public icelake::imp public: simdjson_inline simdjson_result(icelake::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -129637,7 +131608,7 @@ struct simdjson_result : public icelake::implementatio public: simdjson_inline simdjson_result(icelake::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -131181,7 +133152,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -131203,7 +133174,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -131651,7 +133622,7 @@ struct simdjson_result : public icelake::implementati public: simdjson_inline simdjson_result(icelake::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -131770,7 +133741,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -131811,6 +133782,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -131828,7 +133800,7 @@ struct simdjson_result : public icelake::imp public: simdjson_inline simdjson_result(icelake::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -131887,7 +133859,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -131931,7 +133903,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -131959,7 +133931,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -131987,7 +133959,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -132249,6 +134221,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -132302,6 +134278,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -132319,7 +134305,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); icelake::ondemand::array arr; if constexpr (std::is_same_v, icelake::ondemand::array>) { arr = val; @@ -132399,35 +134385,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - icelake::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, icelake::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + icelake::ondemand::object obj; + if constexpr (std::is_same_v, icelake::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -132444,27 +134417,6 @@ error_code tag_invoke(deserialize_tag, icelake::ondemand::object &obj, T &out) n return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, icelake::ondemand::value &val, T &out) noexcept(false) { - icelake::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, icelake::ondemand::document &doc, T &out) noexcept(false) { - icelake::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, icelake::ondemand::document_reference &doc, T &out) noexcept(false) { - icelake::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -132487,7 +134439,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -132510,10 +134462,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -132522,10 +134482,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -132547,6 +134519,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -133315,15 +135289,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -133503,12 +135474,12 @@ namespace icelake { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -133521,7 +135492,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -133534,6 +135506,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -133543,7 +135516,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -133554,6 +135528,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -133581,8 +135556,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -134037,6 +136013,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -134056,6 +136033,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -134851,6 +136833,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -134878,7 +136868,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -134906,6 +136896,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -136236,6 +138232,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -136254,7 +138321,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -136268,7 +138336,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -136485,10 +138554,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -136720,14 +138789,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -136755,7 +138918,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -136765,6 +138928,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -136775,53 +138946,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -136847,6 +139019,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -136867,6 +139053,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -137024,6 +139216,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -137623,7 +139855,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -137781,7 +140013,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -137801,16 +140034,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -138170,7 +140403,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -138183,6 +140417,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -138192,7 +140427,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -138203,6 +140439,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -138217,6 +140454,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -138242,7 +140480,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -138502,25 +140740,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -138567,7 +140817,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -138837,7 +141120,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -138857,7 +141140,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -138889,6 +141172,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -139190,13 +141507,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -139256,10 +141582,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -139309,7 +141649,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -139318,8 +141658,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -139327,14 +141665,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -139342,9 +141673,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -139424,7 +141755,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -139451,8 +141782,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -139461,14 +141790,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -139476,9 +141798,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -139501,8 +141823,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -139511,14 +141831,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -139526,9 +141839,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -139573,6 +141886,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -139663,6 +141984,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -139686,15 +142012,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -139705,8 +142025,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -139855,15 +142174,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -140065,6 +142378,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -140124,6 +142454,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -140187,6 +142524,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -140303,7 +142646,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -141935,7 +144283,7 @@ template <> struct simd8 : base8_numeric { } simdjson_inline simd8 operator<(const simd8 other) const { - return this->gt_bits(other).any_bits_set(); + return this->lt_bits(other).any_bits_set(); } // Bit-specific operations @@ -141964,6 +144312,9 @@ template <> struct simd8 : base8_numeric { simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return !bits_not_set_anywhere(bits); } + simdjson_inline bool is_ascii() const { + return this->bits_not_set_anywhere(uint8_t(0x80)); + } template simdjson_inline simd8 shr() const { return simd8( (__m128i)vec_sr(this->value, (__m128i)vec_splat_u8(N))); @@ -142326,7 +144677,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -142334,6 +144693,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -142651,6 +145019,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -142795,7 +145169,7 @@ struct simdjson_result : public ppc64::implemen public: simdjson_inline simdjson_result(ppc64::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -143703,7 +146077,7 @@ struct simdjson_result : public ppc64::implementation_si public: simdjson_inline simdjson_result(ppc64::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -143976,7 +146350,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -144102,7 +146476,7 @@ struct simdjson_result : public ppc64::implemen public: simdjson_inline simdjson_result(ppc64::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -144179,7 +146553,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -144188,6 +146562,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -144204,6 +146594,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -144383,6 +146779,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -144455,7 +146857,7 @@ struct simdjson_result : public ppc64::implement simdjson_inline simdjson_result(ppc64::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -144618,7 +147020,7 @@ struct simdjson_result : public ppc64::implementatio public: simdjson_inline simdjson_result(ppc64::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -144670,7 +147072,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -144786,6 +147188,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -144825,6 +147241,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -144851,7 +147268,7 @@ struct simdjson_result : public ppc64::impleme public: simdjson_inline simdjson_result(ppc64::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -145313,6 +147730,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -145320,6 +147741,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -145340,7 +147765,7 @@ struct simdjson_result : public ppc64::implementation_s public: simdjson_inline simdjson_result(ppc64::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -145376,7 +147801,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -145623,7 +148048,7 @@ struct simdjson_result : public ppc64::implementation_si public: simdjson_inline simdjson_result(ppc64::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -145704,7 +148129,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -145762,6 +148187,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -145786,7 +148212,7 @@ struct simdjson_result : public ppc64::implemen simdjson_inline simdjson_result(ppc64::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -145836,7 +148262,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -146959,7 +149385,7 @@ struct simdjson_result : public ppc64::implementation public: simdjson_inline simdjson_result(ppc64::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -147089,7 +149515,7 @@ template<> struct simdjson_result : public ppc64::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(ppc64::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -147236,7 +149662,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -147586,7 +150012,7 @@ struct simdjson_result : public ppc64::impleme public: simdjson_inline simdjson_result(ppc64::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -147714,7 +150140,7 @@ struct simdjson_result : public ppc64::implementation_si public: simdjson_inline simdjson_result(ppc64::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -149258,7 +151684,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -149280,7 +151706,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -149728,7 +152154,7 @@ struct simdjson_result : public ppc64::implementation_s public: simdjson_inline simdjson_result(ppc64::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -149847,7 +152273,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -149888,6 +152314,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -149905,7 +152332,7 @@ struct simdjson_result : public ppc64::impleme public: simdjson_inline simdjson_result(ppc64::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -149964,7 +152391,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -150008,7 +152435,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -150036,7 +152463,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -150064,7 +152491,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -150326,6 +152753,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -150379,6 +152810,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -150396,7 +152837,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); ppc64::ondemand::array arr; if constexpr (std::is_same_v, ppc64::ondemand::array>) { arr = val; @@ -150476,35 +152917,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - ppc64::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, ppc64::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + ppc64::ondemand::object obj; + if constexpr (std::is_same_v, ppc64::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -150521,27 +152949,6 @@ error_code tag_invoke(deserialize_tag, ppc64::ondemand::object &obj, T &out) noe return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, ppc64::ondemand::value &val, T &out) noexcept(false) { - ppc64::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, ppc64::ondemand::document &doc, T &out) noexcept(false) { - ppc64::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, ppc64::ondemand::document_reference &doc, T &out) noexcept(false) { - ppc64::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -150564,7 +152971,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -150587,10 +152994,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -150599,10 +153014,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -150624,6 +153051,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -151392,15 +153821,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -151580,12 +154006,12 @@ namespace ppc64 { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -151598,7 +154024,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -151611,6 +154038,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -151620,7 +154048,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -151631,6 +154060,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -151658,8 +154088,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -152114,6 +154545,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -152133,6 +154565,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -152928,6 +155365,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -152955,7 +155400,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -152983,6 +155428,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -154313,6 +156764,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -154331,7 +156853,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -154345,7 +156868,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -154562,10 +157086,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -154797,14 +157321,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -154832,7 +157450,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -154842,6 +157460,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -154852,53 +157478,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -154924,6 +157551,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -154944,6 +157585,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -155101,6 +157748,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -155700,7 +158387,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -155858,7 +158545,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -155878,16 +158566,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -156247,7 +158935,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -156260,6 +158949,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -156269,7 +158959,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -156280,6 +158971,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -156294,6 +158986,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -156319,7 +159012,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -156579,25 +159272,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -156644,7 +159349,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -156914,7 +159652,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -156934,7 +159672,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -156966,6 +159704,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -157267,13 +160039,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -157333,10 +160114,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -157386,7 +160181,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -157395,8 +160190,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -157404,14 +160197,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -157419,9 +160205,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -157501,7 +160287,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -157528,8 +160314,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -157538,14 +160322,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -157553,9 +160330,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -157578,8 +160355,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -157588,14 +160363,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -157603,9 +160371,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -157650,6 +160418,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -157740,6 +160516,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -157763,15 +160544,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -157782,8 +160557,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -157932,15 +160706,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -158142,6 +160910,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -158201,6 +160986,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -158264,6 +161056,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -158380,7 +161178,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -159947,7 +162750,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -160366,7 +163169,7 @@ namespace simd { simdjson_inline simd8 operator<=(const simd8 other) const { return other.max_val(*this) == other; } simdjson_inline simd8 operator>=(const simd8 other) const { return other.min_val(*this) == other; } simdjson_inline simd8 operator>(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } - simdjson_inline simd8 operator<(const simd8 other) const { return this->gt_bits(other).any_bits_set(); } + simdjson_inline simd8 operator<(const simd8 other) const { return this->lt_bits(other).any_bits_set(); } // Bit-specific operations simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } @@ -160711,7 +163514,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -160719,6 +163530,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -161036,6 +163856,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -161180,7 +164006,7 @@ struct simdjson_result : public westmere::im public: simdjson_inline simdjson_result(westmere::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -162088,7 +164914,7 @@ struct simdjson_result : public westmere::implementat public: simdjson_inline simdjson_result(westmere::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -162361,7 +165187,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -162487,7 +165313,7 @@ struct simdjson_result : public westmere::im public: simdjson_inline simdjson_result(westmere::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -162564,7 +165390,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -162573,6 +165399,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -162589,6 +165431,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -162768,6 +165616,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -162840,7 +165694,7 @@ struct simdjson_result : public westmere::imp simdjson_inline simdjson_result(westmere::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -163003,7 +165857,7 @@ struct simdjson_result : public westmere::impleme public: simdjson_inline simdjson_result(westmere::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -163055,7 +165909,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -163171,6 +166025,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -163210,6 +166078,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -163236,7 +166105,7 @@ struct simdjson_result : public westmere::i public: simdjson_inline simdjson_result(westmere::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -163698,6 +166567,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -163705,6 +166578,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -163725,7 +166602,7 @@ struct simdjson_result : public westmere::implementa public: simdjson_inline simdjson_result(westmere::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -163761,7 +166638,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -164008,7 +166885,7 @@ struct simdjson_result : public westmere::implementat public: simdjson_inline simdjson_result(westmere::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -164089,7 +166966,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -164147,6 +167024,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -164171,7 +167049,7 @@ struct simdjson_result : public westmere::im simdjson_inline simdjson_result(westmere::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -164221,7 +167099,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -165344,7 +168222,7 @@ struct simdjson_result : public westmere::implemen public: simdjson_inline simdjson_result(westmere::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -165474,7 +168352,7 @@ template<> struct simdjson_result : public westmere::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(westmere::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -165621,7 +168499,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -165971,7 +168849,7 @@ struct simdjson_result : public westmere::i public: simdjson_inline simdjson_result(westmere::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -166099,7 +168977,7 @@ struct simdjson_result : public westmere::implementat public: simdjson_inline simdjson_result(westmere::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -167643,7 +170521,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -167665,7 +170543,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -168113,7 +170991,7 @@ struct simdjson_result : public westmere::implementa public: simdjson_inline simdjson_result(westmere::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -168232,7 +171110,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -168273,6 +171151,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -168290,7 +171169,7 @@ struct simdjson_result : public westmere::i public: simdjson_inline simdjson_result(westmere::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -168349,7 +171228,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -168393,7 +171272,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -168421,7 +171300,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -168449,7 +171328,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -168711,6 +171590,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -168764,6 +171647,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -168781,7 +171674,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); westmere::ondemand::array arr; if constexpr (std::is_same_v, westmere::ondemand::array>) { arr = val; @@ -168861,35 +171754,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - westmere::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, westmere::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + westmere::ondemand::object obj; + if constexpr (std::is_same_v, westmere::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -168906,27 +171786,6 @@ error_code tag_invoke(deserialize_tag, westmere::ondemand::object &obj, T &out) return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, westmere::ondemand::value &val, T &out) noexcept(false) { - westmere::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, westmere::ondemand::document &doc, T &out) noexcept(false) { - westmere::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, westmere::ondemand::document_reference &doc, T &out) noexcept(false) { - westmere::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -168949,7 +171808,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -168972,10 +171831,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -168984,10 +171851,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -169009,6 +171888,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -169777,15 +172658,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -169965,12 +172843,12 @@ namespace westmere { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -169983,7 +172861,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -169996,6 +172875,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -170005,7 +172885,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -170016,6 +172897,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -170043,8 +172925,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -170499,6 +173382,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -170518,6 +173402,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -171313,6 +174202,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -171340,7 +174237,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -171368,6 +174265,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -172698,6 +175601,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -172716,7 +175690,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -172730,7 +175705,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -172947,10 +175923,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -173182,14 +176158,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -173217,7 +176287,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -173227,6 +176297,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -173237,53 +176315,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -173309,6 +176388,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -173329,6 +176422,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -173486,6 +176585,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -174085,7 +177224,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -174243,7 +177382,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -174263,16 +177403,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -174632,7 +177772,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -174645,6 +177786,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -174654,7 +177796,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -174665,6 +177808,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -174679,6 +177823,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -174704,7 +177849,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -174964,25 +178109,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -175029,7 +178186,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -175299,7 +178489,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -175319,7 +178509,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -175351,6 +178541,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -175652,13 +178876,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -175718,10 +178951,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -175771,7 +179018,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -175780,8 +179027,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -175789,14 +179034,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -175804,9 +179042,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -175886,7 +179124,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -175913,8 +179151,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -175923,14 +179159,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -175938,9 +179167,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -175963,8 +179192,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -175973,14 +179200,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -175988,9 +179208,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -176035,6 +179255,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -176125,6 +179353,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -176148,15 +179381,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -176167,8 +179394,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -176317,15 +179543,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -176527,6 +179747,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -176586,6 +179823,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -176649,6 +179893,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -176765,7 +180015,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -178043,7 +181298,7 @@ namespace simd { simdjson_inline simd8(bool _value) : base8(splat(_value)) {} simdjson_inline int to_bitmask() const { return __lsx_vpickve2gr_w(__lsx_vmskltz_b(*this), 0); } - simdjson_inline bool any() const { return 0 == __lsx_vpickve2gr_hu(__lsx_vmsknz_b(*this), 0); } + simdjson_inline bool any() const { return !__lsx_bz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -178586,7 +181841,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -178594,6 +181857,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -178911,6 +182183,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -179055,7 +182333,7 @@ struct simdjson_result : public lsx::implementati public: simdjson_inline simdjson_result(lsx::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -179963,7 +183241,7 @@ struct simdjson_result : public lsx::implementation_simdjs public: simdjson_inline simdjson_result(lsx::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -180236,7 +183514,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -180362,7 +183640,7 @@ struct simdjson_result : public lsx::implementati public: simdjson_inline simdjson_result(lsx::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -180439,7 +183717,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -180448,6 +183726,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -180464,6 +183758,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -180643,6 +183943,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -180715,7 +184021,7 @@ struct simdjson_result : public lsx::implementatio simdjson_inline simdjson_result(lsx::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -180878,7 +184184,7 @@ struct simdjson_result : public lsx::implementation_si public: simdjson_inline simdjson_result(lsx::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -180930,7 +184236,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -181046,6 +184352,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -181085,6 +184405,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -181111,7 +184432,7 @@ struct simdjson_result : public lsx::implementat public: simdjson_inline simdjson_result(lsx::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -181573,6 +184894,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -181580,6 +184905,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -181600,7 +184929,7 @@ struct simdjson_result : public lsx::implementation_simdj public: simdjson_inline simdjson_result(lsx::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -181636,7 +184965,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -181883,7 +185212,7 @@ struct simdjson_result : public lsx::implementation_simdjs public: simdjson_inline simdjson_result(lsx::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -181964,7 +185293,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -182022,6 +185351,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -182046,7 +185376,7 @@ struct simdjson_result : public lsx::implementati simdjson_inline simdjson_result(lsx::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -182096,7 +185426,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -183219,7 +186549,7 @@ struct simdjson_result : public lsx::implementation_sim public: simdjson_inline simdjson_result(lsx::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -183349,7 +186679,7 @@ template<> struct simdjson_result : public lsx::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(lsx::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -183496,7 +186826,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -183846,7 +187176,7 @@ struct simdjson_result : public lsx::implementat public: simdjson_inline simdjson_result(lsx::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -183974,7 +187304,7 @@ struct simdjson_result : public lsx::implementation_simdjs public: simdjson_inline simdjson_result(lsx::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -185518,7 +188848,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -185540,7 +188870,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -185988,7 +189318,7 @@ struct simdjson_result : public lsx::implementation_simdj public: simdjson_inline simdjson_result(lsx::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -186107,7 +189437,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -186148,6 +189478,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -186165,7 +189496,7 @@ struct simdjson_result : public lsx::implementat public: simdjson_inline simdjson_result(lsx::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -186224,7 +189555,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -186268,7 +189599,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -186296,7 +189627,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -186324,7 +189655,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -186586,6 +189917,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -186639,6 +189974,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -186656,7 +190001,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); lsx::ondemand::array arr; if constexpr (std::is_same_v, lsx::ondemand::array>) { arr = val; @@ -186736,35 +190081,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - lsx::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, lsx::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + lsx::ondemand::object obj; + if constexpr (std::is_same_v, lsx::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -186781,27 +190113,6 @@ error_code tag_invoke(deserialize_tag, lsx::ondemand::object &obj, T &out) noexc return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, lsx::ondemand::value &val, T &out) noexcept(false) { - lsx::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, lsx::ondemand::document &doc, T &out) noexcept(false) { - lsx::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, lsx::ondemand::document_reference &doc, T &out) noexcept(false) { - lsx::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -186824,7 +190135,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -186847,10 +190158,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -186859,10 +190178,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -186884,6 +190215,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -187652,15 +190985,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -187840,12 +191170,12 @@ namespace lsx { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -187858,7 +191188,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -187871,6 +191202,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -187880,7 +191212,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -187891,6 +191224,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -187918,8 +191252,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -188374,6 +191709,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -188393,6 +191729,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -189188,6 +192529,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -189215,7 +192564,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -189243,6 +192592,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -190573,6 +193928,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -190591,7 +194017,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -190605,7 +194032,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -190822,10 +194250,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -191057,14 +194485,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -191092,7 +194614,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -191102,6 +194624,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -191112,53 +194642,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -191184,6 +194715,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -191204,6 +194749,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -191361,6 +194912,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -191960,7 +195551,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -192118,7 +195709,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -192138,16 +195730,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -192507,7 +196099,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -192520,6 +196113,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -192529,7 +196123,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -192540,6 +196135,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -192554,6 +196150,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -192579,7 +196176,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -192839,25 +196436,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -192904,7 +196513,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -193174,7 +196816,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -193194,7 +196836,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -193226,6 +196868,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -193527,13 +197203,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -193593,10 +197278,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -193646,7 +197345,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -193655,8 +197354,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -193664,14 +197361,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -193679,9 +197369,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -193761,7 +197451,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -193788,8 +197478,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -193798,14 +197486,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -193813,9 +197494,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -193838,8 +197519,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -193848,14 +197527,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -193863,9 +197535,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -193910,6 +197582,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -194000,6 +197680,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -194023,15 +197708,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -194042,8 +197721,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -194192,15 +197870,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -194402,6 +198074,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -194461,6 +198150,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -194524,6 +198220,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -194640,7 +198342,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -195709,7 +199416,18 @@ template struct simd8x64; /* amalgamation skipped (editor-only): #endif // SIMDJSON_CONDITIONAL_INCLUDE */ #include +#if defined(__clang__) && !defined(__loongarch_asx) +// LLVM's lasxintrin.h only declares the LASX types and functions when +// __loongarch_asx is defined, that is, when the code is compiled with -mlasx. +// For runtime dispatching, we include it inside a target("lasx") region +// (see simdjson/lasx/begin.h) where the LASX builtins are available, so we +// define __loongarch_asx while including the header. +#define __loongarch_asx 1 #include +#undef __loongarch_asx +#else +#include +#endif static_assert(sizeof(__m256i) <= simdjson::SIMDJSON_PADDING, "insufficient padding for LoongArch ASX"); @@ -195928,10 +199646,7 @@ namespace simd { __m256i mask = __lasx_xvmskltz_b(*this); return (__lasx_xvpickve2gr_w(mask, 4) << 16) | (__lasx_xvpickve2gr_w(mask, 0)); } - simdjson_inline bool any() const { - __m256i v = __lasx_xvmsknz_b(*this); - return (0 == __lasx_xvpickve2gr_w(v, 0)) && (0 == __lasx_xvpickve2gr_w(v, 4)); - } + simdjson_inline bool any() const { return !__lasx_xbz_v(this->value); } simdjson_inline simd8 operator~() const { return *this ^ true; } }; @@ -196484,7 +200199,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -196492,6 +200215,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -196809,6 +200541,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -196953,7 +200691,7 @@ struct simdjson_result : public lasx::implementa public: simdjson_inline simdjson_result(lasx::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -197861,7 +201599,7 @@ struct simdjson_result : public lasx::implementation_simd public: simdjson_inline simdjson_result(lasx::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -198134,7 +201872,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -198260,7 +201998,7 @@ struct simdjson_result : public lasx::implementa public: simdjson_inline simdjson_result(lasx::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -198337,7 +202075,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -198346,6 +202084,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -198362,6 +202116,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -198541,6 +202301,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -198613,7 +202379,7 @@ struct simdjson_result : public lasx::implementat simdjson_inline simdjson_result(lasx::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -198776,7 +202542,7 @@ struct simdjson_result : public lasx::implementation_ public: simdjson_inline simdjson_result(lasx::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -198828,7 +202594,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -198944,6 +202710,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -198983,6 +202763,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -199009,7 +202790,7 @@ struct simdjson_result : public lasx::implement public: simdjson_inline simdjson_result(lasx::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -199471,6 +203252,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -199478,6 +203263,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -199498,7 +203287,7 @@ struct simdjson_result : public lasx::implementation_sim public: simdjson_inline simdjson_result(lasx::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -199534,7 +203323,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -199781,7 +203570,7 @@ struct simdjson_result : public lasx::implementation_simd public: simdjson_inline simdjson_result(lasx::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -199862,7 +203651,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -199920,6 +203709,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -199944,7 +203734,7 @@ struct simdjson_result : public lasx::implementa simdjson_inline simdjson_result(lasx::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -199994,7 +203784,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -201117,7 +204907,7 @@ struct simdjson_result : public lasx::implementation_s public: simdjson_inline simdjson_result(lasx::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -201247,7 +205037,7 @@ template<> struct simdjson_result : public lasx::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(lasx::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -201394,7 +205184,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -201744,7 +205534,7 @@ struct simdjson_result : public lasx::implement public: simdjson_inline simdjson_result(lasx::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -201872,7 +205662,7 @@ struct simdjson_result : public lasx::implementation_simd public: simdjson_inline simdjson_result(lasx::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -203416,7 +207206,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -203438,7 +207228,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -203886,7 +207676,7 @@ struct simdjson_result : public lasx::implementation_sim public: simdjson_inline simdjson_result(lasx::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -204005,7 +207795,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -204046,6 +207836,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -204063,7 +207854,7 @@ struct simdjson_result : public lasx::implement public: simdjson_inline simdjson_result(lasx::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -204122,7 +207913,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -204166,7 +207957,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -204194,7 +207985,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -204222,7 +208013,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -204484,6 +208275,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -204537,6 +208332,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -204554,7 +208359,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); lasx::ondemand::array arr; if constexpr (std::is_same_v, lasx::ondemand::array>) { arr = val; @@ -204634,35 +208439,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - lasx::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, lasx::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + lasx::ondemand::object obj; + if constexpr (std::is_same_v, lasx::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -204679,27 +208471,6 @@ error_code tag_invoke(deserialize_tag, lasx::ondemand::object &obj, T &out) noex return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, lasx::ondemand::value &val, T &out) noexcept(false) { - lasx::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, lasx::ondemand::document &doc, T &out) noexcept(false) { - lasx::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, lasx::ondemand::document_reference &doc, T &out) noexcept(false) { - lasx::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -204722,7 +208493,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -204745,10 +208516,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -204757,10 +208536,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -204782,6 +208573,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -205550,15 +209343,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -205738,12 +209528,12 @@ namespace lasx { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -205756,7 +209546,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -205769,6 +209560,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -205778,7 +209570,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -205789,6 +209582,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -205816,8 +209610,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -206272,6 +210067,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -206291,6 +210087,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -207086,6 +210887,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -207113,7 +210922,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -207141,6 +210950,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -208471,6 +212286,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -208489,7 +212375,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -208503,7 +212390,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -208720,10 +212608,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -208955,14 +212843,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -208990,7 +212972,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -209000,6 +212982,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -209010,53 +213000,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -209082,6 +213073,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -209102,6 +213107,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -209259,6 +213270,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -209858,7 +213909,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -210016,7 +214067,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -210036,16 +214088,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -210405,7 +214457,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -210418,6 +214471,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -210427,7 +214481,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -210438,6 +214493,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -210452,6 +214508,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -210477,7 +214534,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -210737,25 +214794,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -210802,7 +214871,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -211072,7 +215174,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -211092,7 +215194,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -211124,6 +215226,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -211425,13 +215561,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -211491,10 +215636,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -211544,7 +215703,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -211553,8 +215712,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -211562,14 +215719,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -211577,9 +215727,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -211659,7 +215809,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -211686,8 +215836,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -211696,14 +215844,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -211711,9 +215852,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -211736,8 +215877,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -211746,14 +215885,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -211761,9 +215893,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -211808,6 +215940,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -211898,6 +216038,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -211921,15 +216066,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -211940,8 +216079,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -212090,15 +216228,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -212300,6 +216432,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -212359,6 +216508,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -212422,6 +216578,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -212538,7 +216700,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept { @@ -213793,7 +217960,9 @@ namespace simd { simdjson_inline simd8 operator|(const simd8 other) const { return __riscv_vmor(*this, other, VL8); } simdjson_inline simd8 operator&(const simd8 other) const { return __riscv_vmand(*this, other, VL8); } simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vmxor(*this, other, VL8); } - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(other, *this, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vmandn(*this, other, VL8); } + simdjson_inline simd8 operator==(const simd8 other) const { return __riscv_vmxnor(*this, other, VL8); } + simdjson_inline bool any() const { return __riscv_vfirst(value, VL8) >= 0; } simdjson_inline simd8 operator~() const { return __riscv_vmnot(*this, VL8); } simdjson_inline simd8& operator|=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast | other; return *this_cast; } simdjson_inline simd8& operator&=(const simd8 other) { auto this_cast = static_cast*>(this); *this_cast = *this_cast & other; return *this_cast; } @@ -213845,9 +218014,9 @@ namespace simd { simdjson_inline simd8 operator^(const simd8 other) const { return __riscv_vxor_vv_u8m1( value, other, VL8); } simdjson_inline simd8 operator~() const { return __riscv_vnot_v_u8m1(value, VL8); } #if __riscv_zvbb - simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(other, value, VL8); } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return __riscv_vandn_vv_u8m1(value, other, VL8); } #else - simdjson_inline simd8 bit_andnot(const simd8 other) const { return other & ~*this; } + simdjson_inline simd8 bit_andnot(const simd8 other) const { return *this & ~other; } #endif simdjson_inline simd8& operator|=(const simd8 other) { value = *this | other; return *this; } simdjson_inline simd8& operator&=(const simd8 other) { value = *this & other; return *this; } @@ -213875,6 +218044,8 @@ namespace simd { simdjson_inline simd8& operator-=(const simd8 other) { value = *this - other; return *this; } // Order-specific operations + simdjson_inline simd8 max_val(const simd8 other) const { return __riscv_vmaxu(value, other, VL8); } + simdjson_inline simd8 min_val(const simd8 other) const { return __riscv_vminu(value, other, VL8); } simdjson_inline simd8 operator<=(const simd8 other) const { return __riscv_vmsleu(value, other, VL8); } simdjson_inline simd8 operator>=(const simd8 other) const { return __riscv_vmsgeu(value, other, VL8); } simdjson_inline simd8 operator<(const simd8 other) const { return __riscv_vmsltu(value, other, VL8); } @@ -213886,10 +218057,17 @@ namespace simd { simdjson_inline simd8 lt_bits(const simd8 other) const { return simd8(*this < other); } // Bit-specific operations + simdjson_inline simd8 bits_not_set() const { return *this == uint8_t(0); } + simdjson_inline simd8 bits_not_set(simd8 bits) const { return (*this & bits).bits_not_set(); } + simdjson_inline simd8 any_bits_set() const { return ~this->bits_not_set(); } + simdjson_inline simd8 any_bits_set(simd8 bits) const { return ~this->bits_not_set(bits); } + simdjson_inline bool bits_not_set_anywhere() const { return !any_bits_set_anywhere(); } simdjson_inline bool any_bits_set_anywhere() const { return __riscv_vfirst(__riscv_vmsne(value, 0, VL8), VL8) >= 0; } + simdjson_inline bool bits_not_set_anywhere(simd8 bits) const { return !any_bits_set_anywhere(bits); } simdjson_inline bool any_bits_set_anywhere(simd8 bits) const { return (*this & bits).any_bits_set_anywhere(); } + simdjson_inline bool is_ascii() const { return bits_not_set_anywhere(uint8_t(0x80)); } template simdjson_inline simd8 shr() const { return __riscv_vsrl(value, N, VL8); } template @@ -214392,7 +218570,15 @@ class value_iterator { token_position _start_position{}; public: - simdjson_inline value_iterator() noexcept = default; + simdjson_inline value_iterator() noexcept {} + simdjson_inline value_iterator(const value_iterator &other) noexcept + : _json_iter{other._json_iter}, _depth{other._depth}, _start_position{other._start_position} {} + simdjson_inline value_iterator &operator=(const value_iterator &other) noexcept { + _json_iter = other._json_iter; + _depth = other._depth; + _start_position = other._start_position; + return *this; + } /** * Skips a non-iterated or partially-iterated JSON value, whether it is a scalar, array or object. @@ -214400,6 +218586,15 @@ class value_iterator { * Optimized for scalars. */ simdjson_warn_unused simdjson_inline error_code skip_child() noexcept; + /** + * Skips the current child, knowing where it starts: if the iterator is still there, the child + * was not read. + */ + simdjson_warn_unused simdjson_inline error_code skip_child(token_position child_start) noexcept; + /** Skips the value of the current field, if it was not read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_child() noexcept; + /** Skips the value of a field whose colon was just read. */ + simdjson_warn_unused simdjson_inline error_code skip_field_value() noexcept; /** * Tell whether the iterator is at the EOF mark @@ -214717,6 +218912,12 @@ class value_iterator { /** @} */ protected: + /** + * Whether actual_key, the key we just read along with its colon, equals key. + * Unlike raw_json_string::unsafe_is_equal, key may hold unescaped quotes. + */ + simdjson_inline bool key_matches(raw_json_string actual_key, std::string_view key) const noexcept; + /** * Restarts an array iteration. * @returns Whether the array has any elements (returns false for empty). @@ -214861,7 +219062,7 @@ struct simdjson_result : public rvv_vls::impl public: simdjson_inline simdjson_result(rvv_vls::ondemand::value_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -215769,7 +219970,7 @@ struct simdjson_result : public rvv_vls::implementatio public: simdjson_inline simdjson_result(rvv_vls::ondemand::value &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result get_array() noexcept; simdjson_inline simdjson_result get_object() noexcept; @@ -216042,7 +220243,7 @@ class token_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline token_iterator() noexcept = default; + simdjson_inline token_iterator() noexcept {} simdjson_inline token_iterator(token_iterator &&other) noexcept = default; simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default; simdjson_inline token_iterator(const token_iterator &other) noexcept = default; @@ -216168,7 +220369,7 @@ struct simdjson_result : public rvv_vls::impl public: simdjson_inline simdjson_result(rvv_vls::ondemand::token_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -216245,7 +220446,7 @@ class json_iterator { #endif public: - simdjson_inline json_iterator() noexcept = default; + simdjson_inline json_iterator() noexcept {} simdjson_inline json_iterator(json_iterator &&other) noexcept; simdjson_inline json_iterator &operator=(json_iterator &&other) noexcept; simdjson_inline explicit json_iterator(const json_iterator &other) noexcept = default; @@ -216254,6 +220455,22 @@ class json_iterator { * Skips a JSON value, whether it is a scalar, array or object. */ simdjson_warn_unused simdjson_inline error_code skip_child(depth_t parent_depth) noexcept; + /** + * Skips the value of a field whose colon was just read. + */ + simdjson_warn_unused simdjson_inline error_code skip_value(depth_t parent_depth) noexcept; + /** + * Skips what is left of a child that was entered or read in part: the iterator is past the + * start of the child. + */ + simdjson_warn_unused simdjson_inline error_code skip_partial(depth_t parent_depth) noexcept; + /** Skips the tokens of a container that was entered until the depth returns to parent_depth. */ + simdjson_warn_unused simdjson_inline error_code skip_rest(depth_t parent_depth) noexcept; + /** Tells whether the token just consumed cannot follow the token before it. */ + simdjson_inline bool out_of_place(uint8_t current) const noexcept; + simdjson_inline error_code out_of_place_error() noexcept; + /** Tells whether the string before pos, followed by a colon, is a key. */ + static bool string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept; /** * Tell whether the iterator is still at the start @@ -216270,6 +220487,12 @@ class json_iterator { #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline bool allow_incomplete_json() const noexcept; simdjson_inline size_t remaining_input_length(const uint8_t *json) const noexcept; + /** + * The last scalar of an incomplete document may be cut by the end of the input, and the number + * and atom parsers would read on into the bytes past it. Returns a copy of such a scalar padded + * with spaces, and json itself for any other scalar. + */ + simdjson_inline const uint8_t *scalar_within_input(const uint8_t *json) noexcept; #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @@ -216449,6 +220672,12 @@ class json_iterator { * overflow. */ simdjson_inline simdjson_result unescape(raw_json_string in, bool allow_replacement) noexcept; + /** + * Unescape the raw_json_string into receiver. Unlike unescape(), this doesn't leave the string in + * the parser's string buffer. + */ + template + simdjson_warn_unused simdjson_inline error_code unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept; simdjson_inline simdjson_result unescape_wobbly(raw_json_string in) noexcept; simdjson_inline void reenter_child(token_position position, depth_t child_depth) noexcept; @@ -216521,7 +220750,7 @@ struct simdjson_result : public rvv_vls::imple simdjson_inline simdjson_result(rvv_vls::ondemand::json_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -216684,7 +220913,7 @@ struct simdjson_result : public rvv_vls::implement public: simdjson_inline simdjson_result(rvv_vls::ondemand::json_type &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private }; @@ -216736,7 +220965,7 @@ class raw_json_string { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline raw_json_string() noexcept = default; + simdjson_inline raw_json_string() noexcept {} /** * Create a new invalid raw_json_string pointed at the given location in the JSON. @@ -216852,6 +221081,20 @@ class raw_json_string { && ends_with_unpaired_backslash(target); } + /** + * True when target is a compile-time constant that holds no quote character. + * Always false with compilers lacking __builtin_constant_p. + */ + static simdjson_inline bool is_known_free_from_quote(std::string_view target) noexcept; + + /** + * Compares the current instance, a key, to the std::string_view target: returns + * true if they are byte-by-byte equal (no escaping is done). Unlike + * unsafe_is_equal, target may hold unescaped quote characters: it then never + * matches. The key must be followed by the colon at `colon`. + */ + simdjson_inline bool unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept; + /** * This will set the inner pointer to zero, effectively making * this instance unusable. @@ -216891,6 +221134,7 @@ class raw_json_string { friend class object; friend class field; friend class parser; + friend class value_iterator; friend struct simdjson_result; }; @@ -216917,7 +221161,7 @@ struct simdjson_result : public rvv_vls::imp public: simdjson_inline simdjson_result(rvv_vls::ondemand::raw_json_string &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline ~simdjson_result() noexcept = default; ///< @private simdjson_inline simdjson_result raw() const noexcept; @@ -217379,6 +221623,10 @@ class parser { static simdjson_inline simdjson_warn_unused std::unique_ptr& get_parser_instance(); /** Get the thread-local parser instance, it might be null */ static simdjson_inline simdjson_warn_unused std::unique_ptr& get_threadlocal_parser_if_exists(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + static simdjson_inline bool can_end_incomplete_json(uint8_t c) noexcept; + simdjson_warn_unused simdjson_inline error_code end_incomplete_json(padded_string_view json) noexcept; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON /** @private [for benchmarking access] The implementation to use */ std::unique_ptr implementation{}; size_t _capacity{0}; @@ -217386,6 +221634,10 @@ class parser { size_t _max_depth{DEFAULT_MAX_DEPTH}; size_t _document_len{0}; std::unique_ptr string_buf{}; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + /** Copy of a scalar cut by the end of an incomplete document, see json_iterator::scalar_within_input(). */ + uint8_t incomplete_scalar_buf[1074+8+1+SIMDJSON_PADDING]{}; +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON #if SIMDJSON_DEVELOPMENT_CHECKS std::unique_ptr start_positions{}; @@ -217406,7 +221658,7 @@ struct simdjson_result : public rvv_vls::implementati public: simdjson_inline simdjson_result(rvv_vls::ondemand::parser &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -217442,7 +221694,7 @@ class array { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline array() noexcept = default; + simdjson_inline array() noexcept {} /** * Begin array iteration. @@ -217689,7 +221941,7 @@ struct simdjson_result : public rvv_vls::implementatio public: simdjson_inline simdjson_result(rvv_vls::ondemand::array &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -217770,7 +222022,7 @@ class array_iterator { using reference = value_type; /** Create a new, invalid array iterator. */ - simdjson_inline array_iterator() noexcept = default; + simdjson_inline array_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~array_iterator() noexcept; @@ -217828,6 +222080,7 @@ class array_iterator { simdjson_inline array_iterator(const value_iterator &_iter, array* _parent) noexcept; #endif value_iterator iter{}; + token_position element_start{}; simdjson_inline array_iterator(const value_iterator &iter) noexcept; @@ -217852,7 +222105,7 @@ struct simdjson_result : public rvv_vls::impl simdjson_inline simdjson_result(rvv_vls::ondemand::array_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -217902,7 +222155,7 @@ class document { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline document() noexcept = default; + simdjson_inline document() noexcept {} simdjson_inline document(const document &other) noexcept = delete; // pass your documents by reference, not by copy simdjson_inline document(document &&other) noexcept = default; simdjson_inline document &operator=(const document &other) noexcept = delete; @@ -219025,7 +223278,7 @@ struct simdjson_result : public rvv_vls::implementa public: simdjson_inline simdjson_result(rvv_vls::ondemand::document &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -219155,7 +223408,7 @@ template<> struct simdjson_result : public rvv_vls::implementation_simdjson_result_base { public: simdjson_inline simdjson_result(rvv_vls::ondemand::document_reference value, error_code error) noexcept; - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline error_code rewind() noexcept; simdjson_inline simdjson_result get_array() & noexcept; @@ -219302,7 +223555,7 @@ namespace ondemand { #ifdef SIMDJSON_THREADS_ENABLED /** @private Custom worker class **/ struct stage1_worker { - stage1_worker() noexcept = default; + stage1_worker() noexcept {} stage1_worker(const stage1_worker&) = delete; stage1_worker(stage1_worker&&) = delete; stage1_worker operator=(const stage1_worker&) = delete; @@ -219652,7 +223905,7 @@ struct simdjson_result : public rvv_vls::imp public: simdjson_inline simdjson_result(rvv_vls::ondemand::document_stream &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} }; } // namespace simdjson @@ -219780,7 +224033,7 @@ struct simdjson_result : public rvv_vls::implementatio public: simdjson_inline simdjson_result(rvv_vls::ondemand::field &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result unescaped_key(bool allow_replacement = false) noexcept; #if SIMDJSON_SUPPORTS_CHAR8_T @@ -221324,7 +225577,7 @@ struct object_position { * Default-constructed so a variable can be declared and assigned later, * matching e.g. document()/object(). Not a valid position to revert to. */ - simdjson_inline object_position() noexcept = default; + simdjson_inline object_position() noexcept {} private: token_position position{}; @@ -221346,7 +225599,7 @@ class object { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object() noexcept = default; + simdjson_inline object() noexcept {} /** * Get an iterator to the start of the object. We recommend using a range-based for loop. @@ -221794,7 +226047,7 @@ struct simdjson_result : public rvv_vls::implementati public: simdjson_inline simdjson_result(rvv_vls::ondemand::object &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} simdjson_inline simdjson_result begin() & noexcept; simdjson_inline simdjson_result begin() && noexcept; @@ -221913,7 +226166,7 @@ class object_iterator { * * Exists so you can declare a variable and later assign to it before use. */ - simdjson_inline object_iterator() noexcept = default; + simdjson_inline object_iterator() noexcept {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline ~object_iterator() noexcept; @@ -221954,6 +226207,7 @@ class object_iterator { * is first used, and never changes afterwards. */ value_iterator iter{}; + token_position value_start{}; simdjson_inline object_iterator(const value_iterator &iter) noexcept; friend struct simdjson_result; @@ -221971,7 +226225,7 @@ struct simdjson_result : public rvv_vls::imp public: simdjson_inline simdjson_result(rvv_vls::ondemand::object_iterator &&value) noexcept; ///< @private simdjson_inline simdjson_result(error_code error) noexcept; ///< @private - simdjson_inline simdjson_result() noexcept = default; + simdjson_inline simdjson_result() noexcept {} // // Iterator interface @@ -222030,7 +226284,7 @@ class array_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline array_range_iterator() noexcept = default; + simdjson_inline array_range_iterator() noexcept {} simdjson_inline explicit array_range_iterator(array_iterator iter) noexcept; /** @@ -222074,7 +226328,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class array_range { public: - simdjson_inline array_range() noexcept = default; + simdjson_inline array_range() noexcept {} simdjson_inline explicit array_range(array& arr) noexcept; simdjson_inline array_range_iterator begin() noexcept; @@ -222102,7 +226356,7 @@ class object_range_iterator { using reference = simdjson_result; using difference_type = std::ptrdiff_t; - simdjson_inline object_range_iterator() noexcept = default; + simdjson_inline object_range_iterator() noexcept {} simdjson_inline explicit object_range_iterator(object_iterator iter) noexcept; simdjson_inline simdjson_result operator*() const noexcept; @@ -222130,7 +226384,7 @@ SIMDJSON_POP_DISABLE_WARNINGS */ class object_range { public: - simdjson_inline object_range() noexcept = default; + simdjson_inline object_range() noexcept {} simdjson_inline explicit object_range(object& obj) noexcept; simdjson_inline object_range_iterator begin() noexcept; @@ -222392,6 +226646,10 @@ error_code tag_invoke(deserialize_tag, auto &val, char &out) noexcept { // any string-like type (can be constructed from std::string_view) template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(std::is_nothrow_constructible_v) { + if constexpr (std::is_same_v) { + // get_string(std::string&) has a faster path for short strings. + return val.get_string(out); + } std::string_view str; SIMDJSON_TRY(val.get_string().get(str)); if constexpr (requires { out.assign(str.data(), str.size()); }) { @@ -222445,6 +226703,16 @@ struct pop_back_guard { if (armed) { container.pop_back(); } } }; + +// Empties the optional on destruction while armed. +template +struct reset_guard { + T &optional; + bool armed{true}; + ~reset_guard() { + if (armed) { optional.reset(); } + } +}; } // namespace details /** @@ -222462,7 +226730,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the container must default constructible."); + "The specified type inside the container must be default constructible."); rvv_vls::ondemand::array arr; if constexpr (std::is_same_v, rvv_vls::ondemand::array>) { arr = val; @@ -222542,35 +226810,22 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { * We want to support std::map and std::unordered_map but only for * string-keyed types. */ - template +template error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { using value_type = typename std::remove_cvref_t::mapped_type; static_assert( - deserializable, - "The specified value type inside the container must itself be deserializable"); + deserializable, + "The specified value type inside the container must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified value type inside the container must default constructible."); - rvv_vls::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - for (auto field : obj) { - std::string_view key; - SIMDJSON_TRY(field.unescaped_key().get(key)); - value_type this_value; - SIMDJSON_TRY(field.value().get().get(this_value)); - [[maybe_unused]] std::pair result = out.emplace(key, this_value); - // unclear what to do if the key already exists - // if (result.second == false) { - // // key already exists - // } - } - (void)out; - return SUCCESS; -} + "The specified value type inside the container must be default constructible."); -template -error_code tag_invoke(deserialize_tag, rvv_vls::ondemand::object &obj, T &out) noexcept(false) { - using value_type = typename std::remove_cvref_t::mapped_type; + rvv_vls::ondemand::object obj; + if constexpr (std::is_same_v, rvv_vls::ondemand::object>) { + obj = val; + } else { + SIMDJSON_TRY(val.get_object().get(obj)); + } out.clear(); for (auto field : obj) { @@ -222587,27 +226842,6 @@ error_code tag_invoke(deserialize_tag, rvv_vls::ondemand::object &obj, T &out) n return SUCCESS; } -template -error_code tag_invoke(deserialize_tag, rvv_vls::ondemand::value &val, T &out) noexcept(false) { - rvv_vls::ondemand::object obj; - SIMDJSON_TRY(val.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, rvv_vls::ondemand::document &doc, T &out) noexcept(false) { - rvv_vls::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - -template -error_code tag_invoke(deserialize_tag, rvv_vls::ondemand::document_reference &doc, T &out) noexcept(false) { - rvv_vls::ondemand::object obj; - SIMDJSON_TRY(doc.get_object().get(obj)); - return simdjson::deserialize(obj, out); -} - /** * This CPO (Customization Point Object) will help deserialize into @@ -222630,7 +226864,7 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { "The specified type inside the unique_ptr must itself be deserializable"); static_assert( std::is_default_constructible_v, - "The specified type inside the unique_ptr must default constructible."); + "The specified type inside the unique_ptr must be default constructible."); // A null pointer is serialized as null. bool is_null_value; @@ -222653,10 +226887,18 @@ error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept(false) { /** * This CPO (Customization Point Object) will help deserialize into optional types. */ -template -error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deserializable::value_type, decltype(val)>) { +template +error_code tag_invoke(deserialize_tag, ValT &val, T &out) noexcept( + nothrow_deserializable::value_type, std::remove_cvref_t>) { using value_type = typename std::remove_cvref_t::value_type; + static_assert( + deserializable, + "The specified type inside the optional must itself be deserializable"); + static_assert( + std::is_default_constructible_v, + "The specified type inside the optional must be default constructible."); + // Check if the value is null bool is_null_value; SIMDJSON_TRY( val.is_null().get(is_null_value) ); @@ -222665,10 +226907,22 @@ error_code tag_invoke(deserialize_tag, auto &val, T &out) noexcept(nothrow_deser return SUCCESS; } + // On an error or an exception (a user tag_invoke may throw), out is left as + // it was: empty, or holding its previous value. if (!out) { + // Deserialize in place (no temporary to move, which matters for large + // types), and empty out again if that fails. out.emplace(); + details::reset_guard> guard{out}; + SIMDJSON_TRY(val.template get(*out)); + guard.armed = false; + } else { + // out holds a value: deserialize into a fresh value and replace it on + // success. (A container is thus replaced rather than appended to.) + value_type temp; + SIMDJSON_TRY(val.template get(temp)); + out.emplace(std::move(temp)); } - SIMDJSON_TRY(val.template get(out.value())); return SUCCESS; } @@ -222690,6 +226944,8 @@ constexpr bool user_defined_type = (std::is_class_v !concepts::appendable_containers // Smart pointers are handled by the generic smart-pointer tag_invoke. && !concepts::smart_pointer +// String-keyed maps are handled by the generic string_view_keyed_map tag_invoke. +&& !concepts::string_view_keyed_map // simdjson's own types (array, object, value, raw_json_string, number, // document, document_reference) have dedicated get() specializations and // must never go through reflection. @@ -223458,15 +227714,12 @@ inline simdjson_result array::at_pointer(std::string_view json_pointer) n size_t i; SIMDJSON_TRY(internal::parse_json_pointer_array_index(json_pointer, array_index, i)); // Get the child - auto child = at(array_index); + value child; // If there is an error, it ends here - if(child.error()) { - return child; - } - + SIMDJSON_TRY(at(array_index).get(child)); // If there is a /, we're not done yet, call recursively. if (i < json_pointer.length()) { - child = child.at_pointer(json_pointer.substr(i)); + return child.at_pointer(json_pointer.substr(i)); } return child; } @@ -223646,12 +227899,12 @@ namespace rvv_vls { namespace ondemand { simdjson_inline array_iterator::array_iterator(const value_iterator &_iter) noexcept - : iter{_iter} + : iter{_iter}, element_start{_iter.json_iter().position()} {} #if SIMDJSON_DEVELOPMENT_CHECKS simdjson_inline array_iterator::array_iterator(const value_iterator &_iter, array* _parent) noexcept - : parent{_parent}, iter{_iter} + : parent{_parent}, iter{_iter}, element_start{_iter.json_iter().position()} { if (parent) parent->set_locked(true); } @@ -223664,7 +227917,8 @@ simdjson_inline array_iterator::~array_iterator() noexcept simdjson_inline array_iterator::array_iterator(array_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + element_start{other.element_start} { other.parent = nullptr; } @@ -223677,6 +227931,7 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + element_start = other.element_start; other.parent = nullptr; } @@ -223686,7 +227941,8 @@ simdjson_inline array_iterator& array_iterator::operator=(array_iterator&& other simdjson_inline array_iterator::array_iterator(const array_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + element_start{other.element_start} {} simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& other) noexcept { @@ -223697,6 +227953,7 @@ simdjson_inline array_iterator& array_iterator::operator=(const array_iterator& has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + element_start = other.element_start; } return *this; } @@ -223724,8 +227981,9 @@ simdjson_inline array_iterator &array_iterator::operator++() noexcept { // PERF NOTE this is a safety rail ... users should exit loops as soon as they receive an error, so we'll never get here. // However, it does not seem to make a perf difference, so we add it out of an abundance of caution. if (( error = iter.error() )) { return *this; } - if (( error = iter.skip_child() )) { return *this; } + if (( error = iter.skip_child(element_start) )) { return *this; } if (( error = iter.has_next_element().error() )) { return *this; } + element_start = iter.json_iter().position(); return *this; } @@ -224180,6 +228438,7 @@ simdjson_inline simdjson_result value::at_pointer(std::string_view json_p } simdjson_inline simdjson_result value::at_path(std::string_view json_path) noexcept { + if (json_path == "$") { return value(iter); } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -224199,6 +228458,11 @@ template template #endif inline error_code value::for_each_at_path_with_wildcard(std::string_view json_path, Func&& callback) noexcept { + if (json_path == "$") { + value self(iter); + callback(self); + return SUCCESS; + } // The path is split once: every value reached by a wildcard sees the same // remaining path, so re-parsing it at each one would be quadratic. const std::vector path = internal::split_json_path(json_path); @@ -224994,6 +229258,14 @@ simdjson_inline simdjson_result document::get_number() noexcept { simdjson_inline simdjson_result document::raw_json_token() noexcept { auto _iter = get_root_value_iterator(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (iter.allow_incomplete_json()) { + const uint32_t length = _iter.peek_root_length(); + const size_t remaining_input_length = iter.remaining_input_length(_iter.peek_start()); + return std::string_view(reinterpret_cast(_iter.peek_start()), + remaining_input_length < length ? uint32_t(remaining_input_length) : length); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return std::string_view(reinterpret_cast(_iter.peek_start()), _iter.peek_root_length()); } @@ -225021,7 +229293,7 @@ simdjson_inline simdjson_result document::at_path(std::string_view json_p // Rewind the document each time at_path is called. After a fatal error, // the document cannot be walked again: we report the error instead. SIMDJSON_TRY(iter.rewind()); - if (json_path.empty()) { + if (json_path.empty() || json_path == "$") { return this->get_value(); } json_type t; @@ -225049,6 +229321,12 @@ simdjson_inline error_code document::for_each_at_path_with_wildcard(std::string_ if (json_path.empty()) { return INVALID_JSON_POINTER; } + if (json_path == "$") { + value root; + SIMDJSON_TRY(get_value().get(root)); + callback(root); + return SUCCESS; + } json_type t; SIMDJSON_TRY(type().get(t)); switch (t) { @@ -226379,6 +230657,77 @@ inline size_t document_stream::next_batch_start() const noexcept { return batch_start + parser->implementation->structural_indexes[parser->implementation->n_structural_indexes]; } +// Whether the documents in [pos, end) are well formed. Balanced brackets are not +// enough: On-Demand may skip the ] in `[1,]` as a value. +inline bool stream_documents_are_well_formed(const uint8_t *batch, const uint32_t *pos, const uint32_t *end) noexcept { + uint64_t is_object[16]{}; // one bit per open container, up to depth 1024 + size_t depth = 0; + enum { value, first_value, key, first_key, colon, comma_or_close } expected = value; + for (; pos < end; pos++) { + const uint8_t c = batch[*pos]; + if (expected == comma_or_close && depth == 0) { expected = value; } // next document + const bool in_object = depth > 0 && ((is_object[(depth - 1) / 64] >> ((depth - 1) % 64)) & 1); + switch (expected) { + case value: case first_value: + if (c == ']' && expected == first_value) { depth--; expected = comma_or_close; break; } + if (c == ',' || c == ':' || c == ']' || c == '}') { return false; } + expected = comma_or_close; + if (c == '[' || c == '{') { + if (depth == 1024) { return false; } + const uint64_t bit = uint64_t(1) << (depth % 64); + is_object[depth / 64] = (c == '{') ? (is_object[depth / 64] | bit) : (is_object[depth / 64] & ~bit); + depth++; + expected = (c == '{') ? first_key : first_value; + } + break; + case key: case first_key: + if (c == '}' && expected == first_key) { depth--; expected = comma_or_close; break; } + if (c != '"') { return false; } + expected = colon; + break; + case colon: + if (c != ':') { return false; } + expected = value; + break; + case comma_or_close: + if (c == ',') { expected = in_object ? key : value; break; } + if (c != (in_object ? '}' : ']')) { return false; } + depth--; + break; + } + } + return depth == 0 && expected == comma_or_close; +} + +// If the byte past the batch could continue its last document, end the batch +// before an invalid tail, so that On-Demand cannot read past it. Once per batch. +simdjson_never_inline error_code end_batch_before_invalid_tail(internal::dom_parser_implementation &impl, const uint8_t *batch) noexcept { + const uint32_t n = impl.n_structural_indexes; + if (n == 0) { return SUCCESS; } + const uint32_t *const first = impl.structural_indexes.get(); + const uint32_t *const end = first + n; + const uint8_t next = batch[*end]; + const uint8_t last = batch[*(end - 1)]; + if (!(next == ',' || next == ':' || next == ']' || next == '}' || + last == ',' || last == ':' || last == '[' || last == '{')) { + return SUCCESS; + } + const uint32_t *start = end - 1; + for (; start > first; start--) { + const uint8_t c = batch[*start]; + const uint8_t previous = batch[*(start - 1)]; + if (c != ',' && c != ':' && c != ']' && c != '}' && + previous != '[' && previous != '{' && previous != ',' && previous != ':') { + break; + } + } + if (stream_documents_are_well_formed(batch, start, end)) { return SUCCESS; } + if (start == first) { return TAPE_ERROR; } + impl.n_structural_indexes = uint32_t(start - first); + impl.structural_indexes[impl.n_structural_indexes + 1] = *start; // for truncated_bytes() + return SUCCESS; +} + inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch_start) noexcept { // This code only updates the structural index in the parser, it does not update any json_iterator // instance. @@ -226397,7 +230746,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_final; break; } - return p.implementation->stage1(&buf[_batch_start], remaining, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], remaining, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } else { // Partial batch switch (format) { @@ -226411,7 +230761,8 @@ inline error_code document_stream::run_stage1(ondemand::parser &p, size_t _batch mode = stage1_mode::streaming_partial; break; } - return p.implementation->stage1(&buf[_batch_start], batch_size, mode); + error_code err = p.implementation->stage1(&buf[_batch_start], batch_size, mode); + return err ? err : end_batch_before_invalid_tail(*p.implementation, &buf[_batch_start]); } } @@ -226628,10 +230979,10 @@ simdjson_inline simdjson_warn_unused simdjson_result field:: template simdjson_inline simdjson_warn_unused error_code field::unescaped_key(string_type& receiver, bool allow_replacement) noexcept { - std::string_view key; - SIMDJSON_TRY( unescaped_key(allow_replacement).get(key) ); - internal::assign_utf8(receiver, key); - return SUCCESS; + SIMDJSON_ASSUME(first.buf != nullptr); // We would like to call .alive() but Visual Studio won't let us. + error_code key_error = second.iter.json_iter().unescape(first, receiver, allow_replacement); + first.consume(); + return key_error; } simdjson_inline raw_json_string field::key() const noexcept { @@ -226863,14 +231214,108 @@ static constexpr int8_t skip_depth_delta[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; +// Class of the first token of a value: 0 for a scalar, 1 for an array or an object, 2 for a +// token that cannot start a value. +static constexpr uint8_t value_start_class[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +}; + +simdjson_inline bool json_iterator::out_of_place(uint8_t current) const noexcept { + // A value or a key follows a colon, a comma or an opening bracket; nothing else does. + if (_depth <= 1) { return false; } + const uint8_t before = *peek(-2) | 0x20; + const bool after_start = before == ':' || before == ',' || before == '{'; + current |= 0x20; + const bool ends = current == ':' || current == ',' || current == '}'; + return after_start == ends; +} + +simdjson_inline error_code json_iterator::out_of_place_error() noexcept { + return report_error(TAPE_ERROR, "Missing comma, colon or value"); +} + +simdjson_never_inline bool json_iterator::string_is_key(const uint8_t *json, token_position pos, token_position root, token_position end) noexcept { + // The string before pos is followed by a colon. After a colon or a bracket, it is a value; + // after a comma, it is a key if its container is an object. + const uint8_t before = pos - 2 >= root ? json[pos[-2]] : ':'; + if (before == '{') { return true; } + if (before == ':' || before == '[') { return false; } + int32_t depth = 0; + while (pos < end) { + const uint8_t c = json[*pos++]; + depth += skip_depth_delta[c]; + if (depth < 0) { return c == '}'; } + } + return true; +} + // GCC 7 warns when the first line of this function is inlined away into oblivion due to the caller // relating depth and parent_depth, which is a desired effect. The warning does not show up if the // skip_child() function is not marked inline). SIMDJSON_PUSH_DISABLE_WARNINGS SIMDJSON_DISABLE_STRICT_OVERFLOW_WARNING +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_rest(depth_t parent_depth) noexcept { + // Now that we've considered the first value, we only increment/decrement for arrays/objects. + // + // Without logging, look up the depth change of each token in a table instead of branching + // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps + // the position and depth in locals, because the byte loads are char loads that may alias + // token._position and _depth, which would force a store of both on every token. + if (!logger::LOG_ENABLED) { + token_position pos = token.position(); + const token_position end = end_position(); + const uint8_t *const json = token.buf; + depth_t depth = _depth; + while (pos < end) { + depth += skip_depth_delta[json[*pos++]]; + if (depth <= parent_depth) { + token.set_position(pos); + _depth = depth; + return SUCCESS; + } + } + token.set_position(pos); + _depth = depth; + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); + } + + while (position() < end_position()) { + switch (*return_current_and_advance()) { + case '[': case '{': + logger::log_start_value(*this, "skip"); + _depth++; + break; + // TODO consider whether matching braces is a requirement: if non-matching braces indicates + // *missing* braces, then future lookups are not in the object/arrays they think they are, + // violating the rule "validate enough structure that the user can be confident they are + // looking at the right values." + // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + if (depth() <= parent_depth) { return SUCCESS; } + break; + default: + logger::log_value(*this, "skip", ""); + break; + } + } + + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +} + simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_t parent_depth) noexcept { if (depth() <= parent_depth) { return SUCCESS; } - switch (*return_current_and_advance()) { + const uint8_t current = *return_current_and_advance(); + if (simdjson_unlikely(out_of_place(current))) { return out_of_place_error(); } + switch (current) { // TODO consider whether matching braces is a requirement: if non-matching braces indicates // *missing* braces, then future lookups are not in the object/arrays they think they are, // violating the rule "validate enough structure that the user can be confident they are @@ -226898,7 +231343,7 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; case '"': // At the end, peek() would read the sentinel, which points into the padding. - if(!at_end() && *peek() == ':') { + if(!at_end() && *peek() == ':' && string_is_key(token.buf, token.position(), _root, end_position())) { // We are at a key!!! // This might happen if you just started an object and you skip it immediately. // Performance note: it would be nice to get rid of this check as it is somewhat @@ -226908,6 +231353,14 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ return_current_and_advance(); // eat up the ':' break; // important!!! } + if (_depth > 1 && *peek(-2) == '{') { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } simdjson_fallthrough; // Anything else must be a scalar value default: @@ -226918,53 +231371,54 @@ simdjson_warn_unused simdjson_inline error_code json_iterator::skip_child(depth_ break; } - // Now that we've considered the first value, we only increment/decrement for arrays/objects. - // - // Without logging, look up the depth change of each token in a table instead of branching - // on it: the next byte is hard to predict, so a switch mispredicts often. The loop also keeps - // the position and depth in locals, because the byte loads are char loads that may alias - // token._position and _depth, which would force a store of both on every token. - if (!logger::LOG_ENABLED) { - token_position pos = token.position(); - const token_position end = end_position(); - const uint8_t *const json = token.buf; - depth_t depth = _depth; - while (pos < end) { - depth += skip_depth_delta[json[*pos++]]; - if (depth <= parent_depth) { - token.set_position(pos); - _depth = depth; - return SUCCESS; - } - } - token.set_position(pos); - _depth = depth; - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); - } + return skip_rest(parent_depth); +} - while (position() < end_position()) { - switch (*return_current_and_advance()) { - case '[': case '{': - logger::log_start_value(*this, "skip"); - _depth++; - break; - // TODO consider whether matching braces is a requirement: if non-matching braces indicates - // *missing* braces, then future lookups are not in the object/arrays they think they are, - // violating the rule "validate enough structure that the user can be confident they are - // looking at the right values." - // PERF TODO we can eliminate the switch here with a lookup of how much to add to depth - case ']': case '}': - logger::log_end_value(*this, "skip"); - _depth--; - if (depth() <= parent_depth) { return SUCCESS; } - break; - default: - logger::log_value(*this, "skip", ""); - break; - } +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_value(depth_t parent_depth) noexcept { + const uint8_t value_class = value_start_class[*return_current_and_advance()]; + if (value_class == 0) { + logger::log_value(*this, "skip"); + _depth--; + return SUCCESS; } + if (value_class == 2) { return out_of_place_error(); } + logger::log_start_value(*this, "skip"); + return skip_rest(parent_depth); +} - return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "not enough close braces"); +simdjson_warn_unused simdjson_inline error_code json_iterator::skip_partial(depth_t parent_depth) noexcept { + if (depth() <= parent_depth) { return SUCCESS; } + if (depth() != parent_depth + 1) { return skip_child(parent_depth); } + // We are in the child container, past its start: at a key, a colon, a comma or the closing + // bracket. Another value can only follow a missing comma. + switch (*return_current_and_advance()) { + case ',': case ':': + logger::log_value(*this, "skip"); + return skip_rest(parent_depth); + case ']': case '}': + logger::log_end_value(*this, "skip"); + _depth--; + return SUCCESS; + case '"': + if (!at_end() && *peek() == ':') { + logger::log_value(*this, "key"); + return_current_and_advance(); + return skip_rest(parent_depth); + } +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json() && at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object key cut before its colon"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + simdjson_fallthrough; + default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (simdjson_unlikely(allow_incomplete_json() && position() > end_position())) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Value cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + return out_of_place_error(); + } } SIMDJSON_POP_DISABLE_WARNINGS @@ -226990,6 +231444,20 @@ simdjson_inline size_t json_iterator::remaining_input_length(const uint8_t *json const uint8_t *end = token.buf + parser->_document_len; return json < end ? size_t(end - json) : 0; } + +simdjson_inline const uint8_t *json_iterator::scalar_within_input(const uint8_t *json) noexcept { + // Only the last token can run to the end of the input. + const uint32_t n = parser->implementation->n_structural_indexes; + if (simdjson_likely(json != token.buf + parser->implementation->structural_indexes[n - 1])) { return json; } + uint8_t *tmpbuf = parser->incomplete_scalar_buf; + constexpr size_t capacity = sizeof(parser->incomplete_scalar_buf) - SIMDJSON_PADDING; + const size_t length = remaining_input_length(json); + // A scalar too long for the buffer is left out entirely, so that it does not parse. + const size_t copied = length <= capacity ? length : 0; + std::memcpy(tmpbuf, json, copied); + std::memset(tmpbuf + copied, ' ', sizeof(parser->incomplete_scalar_buf) - copied); + return tmpbuf; +} #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON simdjson_inline token_position json_iterator::root_position() const noexcept { @@ -227010,6 +231478,12 @@ simdjson_inline void json_iterator::assert_at_root() const noexcept { } simdjson_inline void json_iterator::assert_more_tokens(uint32_t required_tokens) const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (allow_incomplete_json()) { + SIMDJSON_ASSUME( token._position + required_tokens - 1 <= end_position() ); + return; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON assert_valid_position(token._position + required_tokens - 1); } @@ -227167,6 +231641,46 @@ simdjson_inline simdjson_result json_iterator::unescape(raw_js #endif } +template +simdjson_warn_unused simdjson_inline error_code json_iterator::unescape(raw_json_string in, string_type& receiver, bool allow_replacement) noexcept { +// The inline capacity of std::string must be known at compile time, hence constexpr std::string. +#if SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + if constexpr (std::is_same_v) { + // Short strings without escapes go into the string's inline (SSO) buffer with two overlapping + // copies of fixed size. A memcpy of the exact length is much slower here, because its branches + // on the length mispredict. Like parse_string, this reads past the closing quote into the padding. + constexpr size_t sso_capacity = std::string().capacity(); // 22 with libc++, 15 with libstdc++ and MSVC + constexpr size_t chunk = sso_capacity >= 16 ? 16 : 8; + if constexpr (sso_capacity >= chunk && sso_capacity <= 2 * chunk) { + const uint8_t *const start = reinterpret_cast(in.raw()); + uint8_t scratch[backslash_and_quote::BYTES_PROCESSED]; + for (size_t offset = 0; offset <= sso_capacity; offset += backslash_and_quote::BYTES_PROCESSED) { + auto bs_quote = backslash_and_quote{}.copy_and_find(start + offset, scratch); + if (bs_quote.has_quote_first()) { + const size_t length = offset + size_t(bs_quote.quote_index()); + if (length > sso_capacity) { break; } + receiver.resize_and_overwrite(sso_capacity, [&](char *dst, size_t) noexcept { + std::memcpy(dst, start, chunk); + std::memcpy(dst + sso_capacity - chunk, start + sso_capacity - chunk, chunk); + return length; + }); + return SUCCESS; + } + if (bs_quote.has_backslash()) { break; } + } + } + } +#endif // SIMDJSON_SUPPORTS_RESIZE_AND_OVERWRITE && SIMDJSON_SUPPORTS_CONSTEXPR_STRING + + // Anything else: unescape into the string buffer, copy it out, then give the buffer space back. + auto saved_string_buf_loc = _string_buf_loc; + std::string_view content; + SIMDJSON_TRY( unescape(in, allow_replacement).get(content) ); + internal::assign_utf8(receiver, content); + _string_buf_loc = saved_string_buf_loc; + return SUCCESS; +} + simdjson_inline simdjson_result json_iterator::unescape_wobbly(raw_json_string in) noexcept { #if SIMDJSON_DEVELOPMENT_CHECKS auto result = parser->unescape_wobbly(in, _string_buf_loc); @@ -227766,7 +232280,7 @@ simdjson_flatten simdjson_inline for_each_result object::for_each(Func&& on_matc if (!it.is_open()) { break; } // Skip the value (a no-op if the callback consumed it) and step to the next // field; has_next_field() ends the container on '}', which closes the loop. - if ((error = it.skip_child())) { it.abandon(); return {error, matched}; } + if ((error = it.skip_field_child())) { it.abandon(); return {error, matched}; } if ((error = it.has_next_field().error())) { return {error, matched}; } } return {SUCCESS, matched}; @@ -227924,7 +232438,8 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) size_t slash = json_pointer.find('/'); std::string_view key = json_pointer.substr(0, slash); // Grab the child with the given key - simdjson_result child; + value child; + error_code error; // If there is an escape character in the key, unescape it and then get the child. size_t escape = key.find('~'); @@ -227944,16 +232459,16 @@ inline simdjson_result object::at_pointer(std::string_view json_pointer) } escape = unescaped.find('~', escape+1); } while (escape != std::string::npos); - child = find_field(unescaped); // Take note find_field does not unescape keys when matching + error = find_field(unescaped).get(child); // Take note find_field does not unescape keys when matching } else { - child = find_field(key); + error = find_field(key).get(child); } - if(child.error()) { - return child; // we do not continue if there was an error + if(error) { + return error; // we do not continue if there was an error } // If there is a /, we have to recurse and look up more of the path if (slash != std::string_view::npos) { - child = child.at_pointer(json_pointer.substr(slash)); + return child.at_pointer(json_pointer.substr(slash)); } return child; } @@ -228313,7 +232828,8 @@ simdjson_inline object_iterator::~object_iterator() noexcept simdjson_inline object_iterator::object_iterator(object_iterator&& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{other.parent}, - iter{std::move(other.iter)} + iter{std::move(other.iter)}, + value_start{other.value_start} { other.parent = nullptr; } @@ -228326,6 +232842,7 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot has_been_referenced = other.has_been_referenced; parent = other.parent; iter = std::move(other.iter); + value_start = other.value_start; other.parent = nullptr; } @@ -228335,7 +232852,8 @@ simdjson_inline object_iterator& object_iterator::operator=(object_iterator&& ot simdjson_inline object_iterator::object_iterator(const object_iterator& other) noexcept : has_been_referenced{other.has_been_referenced}, parent{nullptr}, - iter{other.iter} + iter{other.iter}, + value_start{other.value_start} {} simdjson_inline object_iterator& object_iterator::operator=(const object_iterator& other) noexcept { @@ -228346,6 +232864,7 @@ simdjson_inline object_iterator& object_iterator::operator=(const object_iterato has_been_referenced = other.has_been_referenced; parent = nullptr; iter = other.iter; + value_start = other.value_start; } return *this; } @@ -228360,6 +232879,7 @@ simdjson_inline simdjson_result object_iterator::operator*() noexcept { error_code error = iter.error(); if (error) { iter.abandon(); return error; } auto result = field::start(iter); + value_start = iter.json_iter().position(); // TODO this is a safety rail ... users should exit loops as soon as they receive an error. // Nonetheless, let's see if performance is OK with this if statement--the compiler may give it to us for free. if (result.error()) { iter.abandon(); } @@ -228385,7 +232905,7 @@ simdjson_inline object_iterator &object_iterator::operator++() noexcept { if (!iter.is_open()) { return *this; } // Iterator will be released if there is an error simdjson_unused error_code error; - if ((error = iter.skip_child() )) { return *this; } + if ((error = iter.skip_child(value_start) )) { return *this; } simdjson_unused bool has_value; if ((error = iter.has_next_field().get(has_value) )) { return *this; }; @@ -228645,25 +233165,37 @@ simdjson_inline parser::parser(size_t max_capacity) noexcept } simdjson_warn_unused simdjson_inline error_code parser::allocate(size_t new_capacity, size_t new_max_depth) noexcept { + // Reject an invalid depth before touching anything, as the implementation + // would: a failed allocate() leaves the parser as it was. + if (new_max_depth == 0 || new_max_depth > SIMDJSON_MAX_DEPTH) { return CAPACITY; } if (new_capacity > max_capacity()) { return CAPACITY; } if (string_buf && new_capacity <= capacity() && new_max_depth <= max_depth()) { return SUCCESS; } // string_capacity copied from document::allocate - _capacity = 0; - if(5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { + if (5 * (new_capacity / 3) + SIMDJSON_PADDING < SIMDJSON_PADDING) { return CAPACITY; // overflow, only happen on legacy 32-bit systems with very large capacity } size_t string_capacity = SIMDJSON_ROUNDUP_N(5 * (new_capacity / 3) + SIMDJSON_PADDING, 64); - string_buf.reset(new (std::nothrow) uint8_t[string_capacity]); + std::unique_ptr new_string_buf(new (std::nothrow) uint8_t[string_capacity]); + if (!new_string_buf) { return MEMALLOC; } #if SIMDJSON_DEVELOPMENT_CHECKS - start_positions.reset(new (std::nothrow) token_position[new_max_depth]); + std::unique_ptr new_start_positions(new (std::nothrow) token_position[new_max_depth]); + if (!new_start_positions) { return MEMALLOC; } #endif if (implementation) { - SIMDJSON_TRY( implementation->set_capacity(new_capacity) ); - SIMDJSON_TRY( implementation->set_max_depth(new_max_depth) ); + error_code err = implementation->allocate(new_capacity, new_max_depth); + if (err) { return err; } } else { - SIMDJSON_TRY( simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation) ); + error_code err = simdjson::get_active_implementation()->create_dom_parser_implementation(new_capacity, new_max_depth, implementation); + if (err) { + implementation.reset(); + return err; + } } + string_buf = std::move(new_string_buf); +#if SIMDJSON_DEVELOPMENT_CHECKS + start_positions = std::move(new_start_positions); +#endif _capacity = new_capacity; _max_depth = new_max_depth; return SUCCESS; @@ -228710,7 +233242,40 @@ simdjson_warn_unused simdjson_inline simdjson_result parser::iterate_a if (err != UNCLOSED_STRING) return err; } - return document::start({ reinterpret_cast(json.data()), this, true }); + SIMDJSON_TRY( end_incomplete_json(json) ); + const uint8_t *buf = reinterpret_cast(json.data()); + const uint8_t first = buf[implementation->structural_indexes[0]]; + const bool lone_container = implementation->n_structural_indexes == 1 && (first == '{' || first == '['); + return document::start({ buf, this, !lone_container }); +} + +simdjson_inline bool parser::can_end_incomplete_json(uint8_t c) noexcept { + return c != '"' && c != ',' && c != ']' && c != '}'; +} + +simdjson_warn_unused simdjson_inline error_code parser::end_incomplete_json(padded_string_view json) noexcept { + const uint8_t *buf = reinterpret_cast(json.data()); + const size_t len = json.length(); + uint32_t *indexes = implementation->structural_indexes.get(); + uint32_t n = implementation->n_structural_indexes; + while (n > 1) { + const uint8_t last = buf[indexes[n - 1]]; + if (last != ',' && last != ':' && last != '[' && last != '{') { break; } + n--; + } + implementation->n_structural_indexes = n; + indexes[n] = uint32_t(len); + indexes[n + 1] = uint32_t(len); + indexes[n + 2] = 0; + const uint8_t first = buf[indexes[0]]; + if (n == 1 && first != '{' && first != '[') { return SUCCESS; } + if (simdjson_likely(can_end_incomplete_json(buf[len]))) { return SUCCESS; } + size_t end = len + 1; + while (end < len + SIMDJSON_PADDING && !can_end_incomplete_json(buf[end])) { end++; } + if (end == len + SIMDJSON_PADDING) { return INSUFFICIENT_PADDING; } + indexes[n] = uint32_t(end); + indexes[n + 1] = uint32_t(end); + return SUCCESS; } #endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON @@ -228980,7 +233545,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(std::string_v while(pos < target.size()) { pos = target.find('"', pos); if(pos == std::string_view::npos) { return true; } - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -229000,7 +233565,7 @@ simdjson_inline bool raw_json_string::is_free_from_unescaped_quote(const char* t const char * result = strchr(target+pos, '"'); if(result == nullptr) { return true; } pos = result - target; - if(pos != 0 && target[pos-1] != '\\') { return false; } + if(pos == 0 || target[pos-1] != '\\') { return false; } if(pos > 1 && target[pos-2] == '\\') { size_t backslash_count{2}; for(size_t i = 3; i <= pos; i++) { @@ -229032,6 +233597,40 @@ simdjson_inline bool raw_json_string::unsafe_is_equal(std::string_view target) c return is_equal(target); } +simdjson_inline bool raw_json_string::is_known_free_from_quote(std::string_view target) noexcept { +#if defined(__GNUC__) || defined(__clang__) + // Copy the size first: GCC folds __builtin_constant_p of an expression + // holding a function call to false before inlining. When target is a + // compile-time constant, the size is a constant and the scan below folds + // away. Otherwise we return false, and the scan is never emitted. + const size_t size{target.size()}; + if(!__builtin_constant_p(size)) { return false; } + bool has_quote{false}; + for(char c : target) { has_quote |= (c == '"'); } + return !has_quote; +#else + (void)target; + return false; +#endif +} + +simdjson_inline bool raw_json_string::unsafe_is_equal_key(std::string_view target, const uint8_t *colon) const noexcept { + // Past SIMDJSON_PADDING, is_equal stops at the quote terminating the key. + if(target.size() > SIMDJSON_PADDING) { return is_equal(target); } + const char * quote{raw() + target.size()}; + if((*quote != '"') || memcmp(raw(), target.data(), target.size())) { return false; } + // The bytes of target are followed by a quote. Only whitespace separates the + // quote closing the key from the colon: a quote right before the colon + // therefore closes the key, and target is the key. + const char * c{reinterpret_cast(colon)}; + if(simdjson_likely(quote + 1 == c)) { return true; } + // Otherwise there is whitespace before the colon, or the quote is escaped + // (target ends with an unpaired backslash), or target holds an unescaped + // quote: its bytes then run past the quote closing the key, and so past + // the colon, which can only be followed by whitespace before a quote. + return (quote < c) && !escapes_trailing_quote(target); +} + simdjson_inline bool raw_json_string::is_equal(std::string_view target) const noexcept { const char * r{raw()}; size_t pos{0}; @@ -229333,13 +233932,22 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::start if (size_t(_depth) >= _json_iter->parser->max_depth()) { return report_error(DEPTH_ERROR, "Exceeded max depth!"); } _json_iter->set_start_position(_depth, start_position()); #endif - if (*_json_iter->peek() == '}') { + // skip_child() relies on a started object beginning with a key. + const uint8_t *first = _json_iter->peek(); + if (simdjson_likely(*first == '"')) { return true; } + if (*first == '}') { logger::log_value(*_json_iter, "empty object"); _json_iter->return_current_and_advance(); SIMDJSON_TRY(end_container()); return false; } - return true; +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->end_position() - _json_iter->position() <= 1) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Object cut before its first key"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + abandon(); + return report_error(TAPE_ERROR, "Object does not start with a key"); } simdjson_warn_unused simdjson_inline error_code value_iterator::check_root_object() noexcept { @@ -229399,10 +234007,24 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n case ',': return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between object fields"); } } +simdjson_inline bool value_iterator::key_matches(raw_json_string actual_key, std::string_view key) const noexcept { + // A key known at compile time to hold no quote cannot run past actual_key: + // the usual comparison is then exact, and the cheapest. + if (raw_json_string::is_known_free_from_quote(key)) { return actual_key.unsafe_is_equal(key); } + // Otherwise, the colon we just checked bounds actual_key, so that a key + // holding an unescaped quote cannot match the JSON after it. + return actual_key.unsafe_is_equal_key(key, _json_iter->peek(-1)); +} + simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_field_raw(const std::string_view key) noexcept { error_code error; bool has_value; @@ -229452,7 +234074,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // ``` // } else { - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS if (_json_iter->start_position(_depth) != start_position()) { return OUT_OF_ORDER_ITERATION; } @@ -229461,8 +234083,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ while (has_value) { // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -229470,14 +234090,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // key and the value. It will also increment the depth by one. if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - //if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -229485,9 +234098,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); // Skip the value entirely + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -229567,7 +234180,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // If someone queried a key but they not did access the value, then we are left pointing // at the ':' and we need to move forward through the value... If the value was // processed then skip_child() does not move the iterator (but may adjust the depth). - if ((error = skip_child() )) { abandon(); return error; } + if ((error = skip_field_child() )) { abandon(); return error; } search_start = _json_iter->position(); if ((error = has_next_field().get(has_value) )) { abandon(); return error; } #if SIMDJSON_DEVELOPMENT_CHECKS @@ -229594,8 +234207,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). if ((error = field_key().get(actual_key) )) { abandon(); return error; }; @@ -229604,14 +234215,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ if ((error = field_value() )) { abandon(); return error; } // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -229619,9 +234223,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // The has_next_field() advances the pointer and check that either ',' or '}' is found. // It returns true if ',' is found, false otherwise. If anything other than ',' or '}' is found, // then we are in error and we abort. @@ -229644,8 +234248,6 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // Get the key and colon, stopping at the value. raw_json_string actual_key; - // size_t max_key_length = _json_iter->peek_length() - 2; // -2 for the two quotes - // Note: _json_iter->peek_length() - 2 might overflow if _json_iter->peek_length() < 2. // field_key() advances the pointer and checks that '"' is found (corresponding to a key). // The depth is left unchanged by field_key(). error = field_key().get(actual_key); SIMDJSON_ASSUME(!error); @@ -229654,14 +234256,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ error = field_value(); SIMDJSON_ASSUME(!error); // If it matches, stop and return - // We could do it this way if we wanted to allow arbitrary - // key content (including escaped quotes). - // if (actual_key.unsafe_is_equal(max_key_length, key)) { - // Instead we do the following which may trigger buffer overruns if the - // user provides an adversarial key (containing a well placed unescaped quote - // character and being longer than the number of bytes remaining in the JSON - // input). - if (actual_key.unsafe_is_equal(key)) { + if (key_matches(actual_key, key)) { logger::log_event(*this, "match", key, -2); // If we return here, then we return while pointing at the ':' that we just checked. return true; @@ -229669,9 +234264,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::find_ // No match: skip the value and see if , or } is next logger::log_event(*this, "no match", key, -2); - // The call to skip_child is meant to skip over the value corresponding to the key. - // After skip_child(), we are right before the next comma (',') or the final brace ('}'). - SIMDJSON_TRY( skip_child() ); + // The call to skip_field_value is meant to skip over the value corresponding to the key. + // After skip_field_value(), we are right before the next comma (',') or the final brace ('}'). + if ((error = skip_field_value() )) { _json_iter->_depth = 0; return error; } // If we reached the end of the key-value pair we started from, then we know // that the key is not there so we return false. We are either right before // the next comma or the final brace. @@ -229716,6 +234311,14 @@ simdjson_warn_unused simdjson_inline error_code value_iterator::field_key_with_l simdjson_warn_unused simdjson_inline error_code value_iterator::field_value() noexcept { assert_at_next(); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + // An incomplete document may end with a key, possibly cut before its closing + // quote. The next token is then the end-of-input sentinel, which points at the + // byte past the input: it must not be taken for the colon. + if (_json_iter->allow_incomplete_json() && _json_iter->at_end()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Missing colon at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (*_json_iter->return_current_and_advance() != ':') { return report_error(TAPE_ERROR, "Missing colon in object field"); } _json_iter->descend_to(depth()+1); return SUCCESS; @@ -229806,6 +234409,11 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iterator::has_n _json_iter->descend_to(depth()+1); return true; default: +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json() && _json_iter->position() > _json_iter->end_position()) { + return report_error(INCOMPLETE_ARRAY_OR_OBJECT, "Container cut at the end of an incomplete document"); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return report_error(TAPE_ERROR, "Missing comma between array elements"); } } @@ -229829,15 +234437,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_string(string_type& receiver, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_string(allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_raw_json_string().get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_wobbly_string() noexcept { return get_raw_json_string().unescape_wobbly(json_iter()); @@ -229848,8 +234450,7 @@ simdjson_warn_unused simdjson_inline simdjson_result value_iter #ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (_json_iter->allow_incomplete_json()) { const size_t remaining_input_length = _json_iter->remaining_input_length(json); - const uint32_t max_len = remaining_input_length < peek_start_length() ? uint32_t(remaining_input_length) : peek_start_length(); - if (!raw_json_string_is_quote_terminated(json, max_len)) { + if (!raw_json_string_is_quote_terminated(json, uint32_t(remaining_input_length))) { return STRING_ERROR; } } @@ -229998,15 +234599,9 @@ simdjson_warn_unused simdjson_inline simdjson_result value_ite } template simdjson_warn_unused simdjson_inline error_code value_iterator::get_root_string(string_type& receiver, bool check_trailing, bool allow_replacement) noexcept { - std::string_view content; - // Save the string buffer location so that we can restore it after get_string - auto saved_string_buf_loc = _json_iter->string_buf_loc(); - auto err = get_root_string(check_trailing, allow_replacement).get(content); - if (err) { return err; } - internal::assign_utf8(receiver, content); - // Restore the string buffer location, effectively discarding any temporary string storage - _json_iter->string_buf_loc() = saved_string_buf_loc; - return SUCCESS; + raw_json_string raw; + SIMDJSON_TRY( get_root_raw_json_string(check_trailing).get(raw) ); + return _json_iter->unescape(raw, receiver, allow_replacement); } simdjson_warn_unused simdjson_inline simdjson_result value_iterator::get_root_wobbly_string(bool check_trailing) noexcept { return get_root_raw_json_string(check_trailing).unescape_wobbly(json_iter()); @@ -230208,6 +234803,23 @@ simdjson_inline simdjson_result value_iterator::is_root_null(bool check_tr return result; } +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child(token_position child_start) noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->position() == child_start) { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_child() noexcept { + if (_json_iter->depth() <= depth()) { return SUCCESS; } + if (_json_iter->depth() == depth() + 1 && *_json_iter->peek(-1) == ':') { return _json_iter->skip_value(depth()); } + return _json_iter->skip_partial(depth()); +} + +simdjson_warn_unused simdjson_inline error_code value_iterator::skip_field_value() noexcept { + SIMDJSON_ASSUME( _json_iter->_depth == _depth + 1 ); + return _json_iter->skip_value(depth()); +} + simdjson_warn_unused simdjson_inline error_code value_iterator::skip_child() noexcept { SIMDJSON_ASSUME( _json_iter->token._position > _start_position ); SIMDJSON_ASSUME( _json_iter->_depth >= _depth ); @@ -230267,6 +234879,13 @@ simdjson_inline const uint8_t *value_iterator::peek_start() const noexcept { return _json_iter->peek(start_position()); } simdjson_inline uint32_t value_iterator::peek_start_length() const noexcept { +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + const uint32_t length = _json_iter->peek_length(start_position()); + const size_t remaining_input_length = _json_iter->remaining_input_length(peek_start()); + return remaining_input_length < length ? uint32_t(remaining_input_length) : length; + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON return _json_iter->peek_length(start_position()); } simdjson_inline uint32_t value_iterator::peek_root_length() const noexcept { @@ -230330,6 +234949,12 @@ simdjson_inline const uint8_t *value_iterator::peek_root_scalar(const char *type } simdjson_inline const uint8_t *value_iterator::peek_non_root_scalar(const char *type) noexcept { logger::log_value(*_json_iter, start_position(), depth(), type); +#ifdef SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON + if (_json_iter->allow_incomplete_json()) { + if (is_at_start()) { assert_at_non_root_start(); } + return _json_iter->scalar_within_input(peek_start()); + } +#endif // SIMDJSON_EXPERIMENTAL_ALLOW_INCOMPLETE_JSON if (!is_at_start()) { return peek_start(); } assert_at_non_root_start(); @@ -230446,7 +235071,12 @@ inline void value_iterator::assert_at_root() const noexcept { inline void value_iterator::assert_at_non_root_start() const noexcept { assert_at_start(); - SIMDJSON_ASSUME( _depth > 1 ); + // The value is usually inside an array or object (depth > 1), but a root + // array or object obtained with document::get_value() is a value at depth 1. + // Scalar getters may be called on it: they see the leading '[' or '{' and + // fail (INCORRECT_TYPE, or NUMBER_ERROR for get_number() and friends) + // without advancing the iterator. + SIMDJSON_ASSUME( _depth > 0 ); } inline void value_iterator::assert_is_valid() const noexcept {