diff --git a/include/jsoncons/json_parser.hpp b/include/jsoncons/json_parser.hpp index d3d6634dc..b439c5203 100644 --- a/include/jsoncons/json_parser.hpp +++ b/include/jsoncons/json_parser.hpp @@ -48,7 +48,6 @@ enum class json_parse_state : uint8_t expect_value, array, member_name, - cr, done }; @@ -297,8 +296,8 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< void skip_space(std::error_code& ec) { + bool prev_char_is_cr = false; const char_type* local_input_end = input_end_; - while (true) { if (input_ptr_ == local_input_end) @@ -313,77 +312,36 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< { case ' ': case '\t': + prev_char_is_cr = false; ++input_ptr_; ++position_; break; case '\r': - push_state(state_); + prev_char_is_cr = true; ++input_ptr_; + ++line_; ++position_; - state_ = json_parse_state::cr; - return; + mark_position_ = position_; + break; case '\n': ++input_ptr_; - ++line_; + if (prev_char_is_cr) + { + prev_char_is_cr = false; + } + else + { + ++line_; + } ++position_; mark_position_ = position_; - return; + break; default: return; } } } - void skip_whitespace(std::error_code& ec) - { - const char_type* local_input_end = input_end_; - - while (true) - { - if (input_ptr_ == local_input_end) - { - if (!chunk_rdr_->read_chunk(*this, ec)) - { - break; - } - local_input_end = input_end_; - } - switch (state_) - { - case json_parse_state::cr: - ++line_; - ++position_; - mark_position_ = position_; - switch (*input_ptr_) - { - case '\n': - ++input_ptr_; - ++position_; - state_ = pop_state(); - break; - default: - state_ = pop_state(); - break; - } - break; - - default: - switch (*input_ptr_) - { - case ' ': - case '\t': - case '\n': - case '\r': - skip_space(ec); - break; - default: - return; - } - break; - } - } - } - void begin_object(basic_json_visitor& visitor, std::error_code& ec) { if (JSONCONS_UNLIKELY(++nesting_depth_ > options_.max_nesting_depth())) @@ -539,10 +497,18 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< void check_done(std::error_code& ec) { - for (; input_ptr_ != input_end_; ++input_ptr_) + const char_type* local_input_end = input_end_; + while (true) { - char_type curr_char_ = *input_ptr_; - switch (curr_char_) + if (input_ptr_ == local_input_end) + { + if (!chunk_rdr_->read_chunk(*this, ec)) + { + break; + } + local_input_end = input_end_; + } + switch (*input_ptr_) { case '\n': case '\r': @@ -558,6 +524,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< } break; } + ++input_ptr_; } } @@ -652,9 +619,6 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< case json_parse_state::done: more_ = false; break; - case json_parse_state::cr: - state_ = pop_state(); - break; default: err_handler_(json_errc::unexpected_eof, *this); ec = json_errc::unexpected_eof; @@ -682,21 +646,6 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< state_ = json_parse_state::done; more_ = false; break; - case json_parse_state::cr: - ++line_; - mark_position_ = position_; - switch (*input_ptr_) - { - case '\n': - ++input_ptr_; - ++position_; - state_ = pop_state(); - break; - default: - state_ = pop_state(); - break; - } - break; case json_parse_state::start: { switch (*input_ptr_) @@ -709,18 +658,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< return; } break; - case '\r': - push_state(state_); - ++input_ptr_; - ++position_; - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -818,18 +756,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< ++input_ptr_; ++position_; break; - case '\r': - ++input_ptr_; - ++position_; - push_state(state_); - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -898,18 +825,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< ++input_ptr_; ++position_; break; - case '\r': - ++input_ptr_; - ++position_; - push_state(state_); - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -972,18 +888,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< ++input_ptr_; ++position_; break; - case '\r': - ++input_ptr_; - ++position_; - push_state(state_); - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -1052,18 +957,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< ++input_ptr_; ++position_; break; - case '\r': - push_state(state_); - state_ = json_parse_state::cr; - ++input_ptr_; - ++position_; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -1106,18 +1000,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< ++input_ptr_; ++position_; break; - case '\r': - push_state(state_); - ++input_ptr_; - ++position_; - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -1244,18 +1127,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< ++input_ptr_; ++position_; break; - case '\r': - ++input_ptr_; - ++position_; - push_state(state_); - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - break; + case '\r': case '\n': case ' ':case '\t': skip_space(ec); break; @@ -1389,17 +1261,8 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< { switch (*input_ptr_) { - case '\r': - push_state(state_); - ++input_ptr_; - ++position_; - state_ = json_parse_state::cr; - break; - case '\n': - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; + case '\r': case '\n': + skip_space(ec); break; case '*': ++input_ptr_; @@ -1727,22 +1590,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< } switch (*input_ptr_) { - case '\r': - end_integer_value(visitor, ec); - if (ec) return; - ++input_ptr_; - ++position_; - push_state(state_); - state_ = json_parse_state::cr; - return; - case '\n': - end_integer_value(visitor, ec); - if (ec) return; - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - return; + case '\r':case '\n': case ' ':case '\t': end_integer_value(visitor, ec); if (ec) return; @@ -1807,22 +1655,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< } switch (*input_ptr_) { - case '\r': - end_integer_value(visitor, ec); - if (ec) return; - push_state(state_); - ++input_ptr_; - ++position_; - state_ = json_parse_state::cr; - return; - case '\n': - end_integer_value(visitor, ec); - if (ec) return; - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - return; + case '\r':case '\n': case ' ':case '\t': end_integer_value(visitor, ec); if (ec) return; @@ -1909,22 +1742,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< } switch (*input_ptr_) { - case '\r': - end_fraction_value(visitor, ec); - if (ec) return; - push_state(state_); - ++input_ptr_; - ++position_; - state_ = json_parse_state::cr; - return; - case '\n': - end_fraction_value(visitor, ec); - if (ec) return; - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - return; + case '\r':case '\n': case ' ':case '\t': end_fraction_value(visitor, ec); if (ec) return; @@ -2042,22 +1860,7 @@ class basic_json_parser : public ser_context, public virtual basic_parser_input< } switch (*input_ptr_) { - case '\r': - end_fraction_value(visitor, ec); - if (ec) return; - ++input_ptr_; - ++position_; - push_state(state_); - state_ = json_parse_state::cr; - return; - case '\n': - end_fraction_value(visitor, ec); - if (ec) return; - ++input_ptr_; - ++line_; - ++position_; - mark_position_ = position_; - return; + case '\r':case '\n': case ' ':case '\t': end_fraction_value(visitor, ec); if (ec) return; diff --git a/include/jsoncons/json_reader.hpp b/include/jsoncons/json_reader.hpp index bf037818d..2bb60d192 100644 --- a/include/jsoncons/json_reader.hpp +++ b/include/jsoncons/json_reader.hpp @@ -20,7 +20,7 @@ #include #include -namespace jsoncons { +namespace jsoncons { // utf8_other_json_input_adapter @@ -319,23 +319,7 @@ namespace jsoncons { return; } - while (!source_.eof()) - { - parser_.skip_space(ec); - if (parser_.source_exhausted()) - { - auto s1 = source_.read_buffer(ec); - if (ec) return; - if (s1.size() > 0) - { - parser_.set_buffer(s1.data(),s1.size()); - } - } - else - { - break; - } - } + parser_.skip_space(ec); } void check_done() @@ -365,32 +349,7 @@ namespace jsoncons { ec = json_errc::source_error; return; } - if (source_.eof()) - { - parser_.check_done(ec); - if (ec) return; - } - else - { - do - { - if (parser_.source_exhausted()) - { - auto s = source_.read_buffer(ec); - if (ec) return; - if (s.size() > 0) - { - parser_.set_buffer(s.data(),s.size()); - } - } - if (!parser_.source_exhausted()) - { - parser_.check_done(ec); - if (ec) return; - } - } - while (!eof()); - } + parser_.check_done(ec); } bool eof() const diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f0d678373..cb653ce5c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -58,137 +58,136 @@ endif() target_include_directories (catch INTERFACE ${CATCH_INCLUDE_DIR}) -add_executable(unit_tests - fuzz_regression/src/fuzz_regression_tests.cpp - #bson/src/bson_cursor_tests.cpp - #bson/src/bson_encoder_tests.cpp - #bson/src/bson_reader_tests.cpp - #bson/src/bson_decimal128_tests.cpp - #bson/src/bson_oid_tests.cpp - #bson/src/bson_test_suite.cpp - #bson/src/encode_decode_bson_tests.cpp - #cbor/src/cbor_bitset_traits_tests.cpp - #cbor/src/cbor_cursor_tests.cpp - #cbor/src/cbor_event_reader_tests.cpp - #cbor/src/cbor_encoder_tests.cpp - #cbor/src/cbor_event_visitor_tests.cpp - #cbor/src/cbor_reader_tests.cpp - #cbor/src/cbor_tests.cpp - #cbor/src/cbor_typed_array_tests.cpp - #cbor/src/decode_cbor_tests.cpp - #cbor/src/encode_cbor_tests.cpp - #csv/src/csv_cursor_tests.cpp - #csv/src/csv_subfield_tests.cpp - #csv/src/csv_tests.cpp - #csv/src/encode_decode_csv_tests.cpp - #jmespath/src/jmespath_expression_tests.cpp - #jmespath/src/jmespath_tests.cpp - #mergepatch/src/mergepatch_test_suite.cpp - #jsonpatch/src/jsonpatch_test_suite.cpp - #jsonpatch/src/jsonpatch_tests.cpp - #jsonpath/src/jsonpath_flatten_tests.cpp - #jsonpath/src/path_node_tests.cpp - #jsonpath/src/json_location_tests.cpp - #jsonpath/src/json_location_parser_tests.cpp - #jsonpath/src/jsonpath_custom_function_tests.cpp - #jsonpath/src/jsonpath_json_query_tests.cpp - #jsonpath/src/jsonpath_expression_tests.cpp - #jsonpath/src/jsonpath_json_replace_tests.cpp - #jsonpath/src/jsonpath_select_paths_tests.cpp - #jsonpath/src/jsonpath_test_suite.cpp - #jsonpath/src/jsonpath_stateful_allocator_tests.cpp - #jsonpointer/src/jsonpointer_flatten_tests.cpp - #jsonpointer/src/jsonpointer_tests.cpp - #jsonschema/src/abort_early_tests.cpp - #jsonschema/src/json_schema_walk_tests.cpp - #jsonschema/src/format_validator_tests.cpp - #jsonschema/src/validation_report_tests.cpp - #jsonschema/src/dynamic_ref_tests.cpp - #jsonschema/src/jsonschema_defaults_tests.cpp - #jsonschema/src/jsonschema_draft4_tests.cpp - #jsonschema/src/jsonschema_draft6_tests.cpp - #jsonschema/src/jsonschema_draft7_tests.cpp - #jsonschema/src/jsonschema_draft201909_tests.cpp - #jsonschema/src/jsonschema_draft202012_tests.cpp - #jsonschema/src/schema_version_tests.cpp - #msgpack/src/decode_msgpack_tests.cpp - #msgpack/src/encode_msgpack_tests.cpp - #msgpack/src/msgpack_bitset_traits_tests.cpp - #msgpack/src/msgpack_cursor_tests.cpp - #msgpack/src/msgpack_event_reader_tests.cpp - #msgpack/src/msgpack_encoder_tests.cpp - #msgpack/src/msgpack_tests.cpp - #msgpack/src/msgpack_timestamp_tests.cpp - #corelib/src/bigint_tests.cpp - #corelib/src/source_adaptor_tests.cpp - #corelib/src/byte_string_tests.cpp - #corelib/src/value_converter_tests.cpp - #corelib/src/decode_traits_tests.cpp - #corelib/src/detail/optional_tests.cpp - #corelib/src/detail/span_tests.cpp - #corelib/src/detail/string_view_tests.cpp - #corelib/src/utility/heap_string_tests.cpp - #corelib/src/detail/to_integer_tests.cpp - #corelib/src/double_round_trip_tests.cpp - #corelib/src/double_to_string_tests.cpp - #corelib/src/dtoa_tests.cpp - #corelib/src/encode_decode_json_tests.cpp - #corelib/src/encode_traits_tests.cpp - #corelib/src/json_parser_recovery_tests.cpp - #corelib/src/json_array_tests.cpp - #corelib/src/json_as_tests.cpp - #corelib/src/json_bitset_traits_tests.cpp - #corelib/src/json_checker_tests.cpp - #corelib/src/json_comparator_tests.cpp - #corelib/src/json_const_pointer_tests.cpp - #corelib/src/json_assignment_tests.cpp - #corelib/src/json_constructor_tests.cpp - #corelib/src/json_cursor_tests.cpp - #corelib/src/json_encoder_tests.cpp - #corelib/src/json_exception_tests.cpp - #corelib/src/json_filter_tests.cpp - #corelib/src/json_in_place_update_tests.cpp - #corelib/src/json_integer_tests.cpp - #corelib/src/json_less_tests.cpp - #corelib/src/json_line_split_tests.cpp - #corelib/src/json_literal_operator_tests.cpp - #corelib/src/json_object_tests.cpp - #corelib/src/ojson_object_tests.cpp - #corelib/src/json_options_tests.cpp - #corelib/src/json_parse_error_tests.cpp - #corelib/src/json_parser_position_tests.cpp - #corelib/src/json_parser_tests.cpp - #corelib/src/json_push_back_tests.cpp - #corelib/src/json_reader_exception_tests.cpp - #corelib/src/json_reader_tests.cpp - #corelib/src/json_storage_tests.cpp - #corelib/src/json_swap_tests.cpp - #corelib/src/json_traits_macro_functional_tests.cpp - #corelib/src/json_traits_macro_tests.cpp - #corelib/src/json_traits_macro_limit_tests.cpp - #corelib/src/json_traits_name_macro_tests.cpp - #corelib/src/json_type_traits_chrono_tests.cpp - #corelib/src/json_type_traits_container_tests.cpp - #corelib/src/json_type_traits_tests.cpp - #corelib/src/uri_tests.cpp - #corelib/src/json_validation_tests.cpp - #corelib/src/jsoncons_tests.cpp - #corelib/src/JSONTestSuite_tests.cpp - #corelib/src/ojson_tests.cpp - #corelib/src/parse_string_tests.cpp - #corelib/src/short_string_tests.cpp - #corelib/src/source_tests.cpp - #corelib/src/staj_iterator_tests.cpp - #corelib/src/extension_traits_tests.cpp - #corelib/src/polymorphic_allocator_tests.cpp - #corelib/src/scoped_allocator_adaptor_tests.cpp - #corelib/src/string_to_double_tests.cpp - #corelib/src/unicode_conv_tests.cpp - #corelib/src/wjson_tests.cpp - #ubjson/src/decode_ubjson_tests.cpp - #ubjson/src/encode_ubjson_tests.cpp - #ubjson/src/ubjson_cursor_tests.cpp - #ubjson/src/ubjson_encoder_tests.cpp +add_executable(unit_tests + bson/src/bson_cursor_tests.cpp + bson/src/bson_encoder_tests.cpp + bson/src/bson_reader_tests.cpp + bson/src/bson_decimal128_tests.cpp + bson/src/bson_oid_tests.cpp + bson/src/bson_test_suite.cpp + bson/src/encode_decode_bson_tests.cpp + cbor/src/cbor_bitset_traits_tests.cpp + cbor/src/cbor_cursor_tests.cpp + cbor/src/cbor_event_reader_tests.cpp + cbor/src/cbor_encoder_tests.cpp + cbor/src/cbor_event_visitor_tests.cpp + cbor/src/cbor_reader_tests.cpp + cbor/src/cbor_tests.cpp + cbor/src/cbor_typed_array_tests.cpp + cbor/src/decode_cbor_tests.cpp + cbor/src/encode_cbor_tests.cpp + csv/src/csv_cursor_tests.cpp + csv/src/csv_subfield_tests.cpp + csv/src/csv_tests.cpp + csv/src/encode_decode_csv_tests.cpp + jmespath/src/jmespath_expression_tests.cpp + jmespath/src/jmespath_tests.cpp + mergepatch/src/mergepatch_test_suite.cpp + jsonpatch/src/jsonpatch_test_suite.cpp + jsonpatch/src/jsonpatch_tests.cpp + jsonpath/src/jsonpath_flatten_tests.cpp + jsonpath/src/path_node_tests.cpp + jsonpath/src/json_location_tests.cpp + jsonpath/src/json_location_parser_tests.cpp + jsonpath/src/jsonpath_custom_function_tests.cpp + jsonpath/src/jsonpath_json_query_tests.cpp + jsonpath/src/jsonpath_expression_tests.cpp + jsonpath/src/jsonpath_json_replace_tests.cpp + jsonpath/src/jsonpath_select_paths_tests.cpp + jsonpath/src/jsonpath_test_suite.cpp + jsonpath/src/jsonpath_stateful_allocator_tests.cpp + jsonpointer/src/jsonpointer_flatten_tests.cpp + jsonpointer/src/jsonpointer_tests.cpp + jsonschema/src/abort_early_tests.cpp + jsonschema/src/json_schema_walk_tests.cpp + jsonschema/src/format_validator_tests.cpp + jsonschema/src/validation_report_tests.cpp + jsonschema/src/dynamic_ref_tests.cpp + jsonschema/src/jsonschema_defaults_tests.cpp + jsonschema/src/jsonschema_draft4_tests.cpp + jsonschema/src/jsonschema_draft6_tests.cpp + jsonschema/src/jsonschema_draft7_tests.cpp + jsonschema/src/jsonschema_draft201909_tests.cpp + jsonschema/src/jsonschema_draft202012_tests.cpp + jsonschema/src/schema_version_tests.cpp + msgpack/src/decode_msgpack_tests.cpp + msgpack/src/encode_msgpack_tests.cpp + msgpack/src/msgpack_bitset_traits_tests.cpp + msgpack/src/msgpack_cursor_tests.cpp + msgpack/src/msgpack_event_reader_tests.cpp + msgpack/src/msgpack_encoder_tests.cpp + msgpack/src/msgpack_tests.cpp + msgpack/src/msgpack_timestamp_tests.cpp + corelib/src/bigint_tests.cpp + corelib/src/source_adaptor_tests.cpp + corelib/src/byte_string_tests.cpp + corelib/src/value_converter_tests.cpp + corelib/src/decode_traits_tests.cpp + corelib/src/detail/optional_tests.cpp + corelib/src/detail/span_tests.cpp + corelib/src/detail/string_view_tests.cpp + corelib/src/utility/heap_string_tests.cpp + corelib/src/detail/to_integer_tests.cpp + corelib/src/double_round_trip_tests.cpp + corelib/src/double_to_string_tests.cpp + corelib/src/dtoa_tests.cpp + corelib/src/encode_decode_json_tests.cpp + corelib/src/encode_traits_tests.cpp + corelib/src/json_parser_recovery_tests.cpp + corelib/src/json_array_tests.cpp + corelib/src/json_as_tests.cpp + corelib/src/json_bitset_traits_tests.cpp + corelib/src/json_checker_tests.cpp + corelib/src/json_comparator_tests.cpp + corelib/src/json_const_pointer_tests.cpp + corelib/src/json_assignment_tests.cpp + corelib/src/json_constructor_tests.cpp + corelib/src/json_cursor_tests.cpp + corelib/src/json_encoder_tests.cpp + corelib/src/json_exception_tests.cpp + corelib/src/json_filter_tests.cpp + corelib/src/json_in_place_update_tests.cpp + corelib/src/json_integer_tests.cpp + corelib/src/json_less_tests.cpp + corelib/src/json_line_split_tests.cpp + corelib/src/json_literal_operator_tests.cpp + corelib/src/json_object_tests.cpp + corelib/src/ojson_object_tests.cpp + corelib/src/json_options_tests.cpp + corelib/src/json_parser_error_tests.cpp + corelib/src/json_parser_position_tests.cpp + corelib/src/json_parser_tests.cpp + corelib/src/json_push_back_tests.cpp + corelib/src/json_reader_exception_tests.cpp + corelib/src/json_reader_tests.cpp + corelib/src/json_storage_tests.cpp + corelib/src/json_swap_tests.cpp + corelib/src/json_traits_macro_functional_tests.cpp + corelib/src/json_traits_macro_tests.cpp + corelib/src/json_traits_macro_limit_tests.cpp + corelib/src/json_traits_name_macro_tests.cpp + corelib/src/json_type_traits_chrono_tests.cpp + corelib/src/json_type_traits_container_tests.cpp + corelib/src/json_type_traits_tests.cpp + corelib/src/uri_tests.cpp + corelib/src/json_validation_tests.cpp + corelib/src/jsoncons_tests.cpp + corelib/src/JSONTestSuite_tests.cpp + corelib/src/ojson_tests.cpp + corelib/src/parse_string_tests.cpp + corelib/src/short_string_tests.cpp + corelib/src/source_tests.cpp + corelib/src/staj_iterator_tests.cpp + corelib/src/extension_traits_tests.cpp + corelib/src/polymorphic_allocator_tests.cpp + corelib/src/scoped_allocator_adaptor_tests.cpp + corelib/src/string_to_double_tests.cpp + corelib/src/unicode_conv_tests.cpp + corelib/src/wjson_tests.cpp + ubjson/src/decode_ubjson_tests.cpp + ubjson/src/encode_ubjson_tests.cpp + ubjson/src/ubjson_cursor_tests.cpp + ubjson/src/ubjson_encoder_tests.cpp corelib/src/testmain.cpp )