Skip to content

Commit

Permalink
chunk_rdr space
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 6, 2024
1 parent c728371 commit a4434b9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 82 deletions.
102 changes: 59 additions & 43 deletions include/jsoncons/json_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@

namespace jsoncons {

class read_more_command
class chunk_reader
{
public:
virtual ~read_more_command() = default;
virtual bool read_more(std::error_code&)
virtual ~chunk_reader() = default;
virtual bool read_chunk(std::error_code&)
{
return false;
}
Expand Down Expand Up @@ -138,8 +138,8 @@ class basic_json_parser : public ser_context
std::vector<json_parse_state,parse_state_allocator_type> state_stack_;
std::vector<std::pair<std::basic_string<char_type>,double>> string_double_map_;

read_more_command default_more_command;
read_more_command* more_command_ = &default_more_command;
chunk_reader default_chunk_reader;
chunk_reader* chunk_rdr_ = &default_chunk_reader;

// Noncopyable and nonmoveable
basic_json_parser(const basic_json_parser&) = delete;
Expand Down Expand Up @@ -206,7 +206,7 @@ class basic_json_parser : public ser_context

basic_json_parser(const basic_json_decode_options<char_type>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
read_more_command* observer,
chunk_reader* observer,
const TempAllocator& temp_alloc = TempAllocator())
: options_(options),
err_handler_(err_handler),
Expand All @@ -225,7 +225,7 @@ class basic_json_parser : public ser_context
done_(false),
string_buffer_(temp_alloc),
state_stack_(temp_alloc),
more_command_(observer)
chunk_rdr_(observer)
{
string_buffer_.reserve(initial_string_buffer_capacity);

Expand Down Expand Up @@ -307,11 +307,19 @@ class basic_json_parser : public ser_context
return input_end_;
}

void skip_space()
void skip_space(std::error_code& ec)
{
const char_type* local_input_end = input_end_;
while (input_ptr_ != local_input_end)
while (true)
{
if (input_ptr_ == local_input_end)
{
if (!chunk_rdr_->read_chunk(ec))
{
break;
}
local_input_end = input_end_;
}
switch (*input_ptr_)
{
case ' ':
Expand All @@ -337,12 +345,20 @@ class basic_json_parser : public ser_context
}
}

void skip_whitespace()
void skip_whitespace(std::error_code& ec)
{
const char_type* local_input_end = input_end_;

while (input_ptr_ != local_input_end)
while (true)
{
if (input_ptr_ == local_input_end)
{
if (!chunk_rdr_->read_chunk(ec))
{
break;
}
local_input_end = input_end_;
}
switch (state_)
{
case json_parse_state::cr:
Expand All @@ -369,7 +385,7 @@ class basic_json_parser : public ser_context
case '\t':
case '\n':
case '\r':
skip_space();
skip_space(ec);
break;
default:
return;
Expand Down Expand Up @@ -688,7 +704,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
++input_ptr_;
Expand Down Expand Up @@ -807,7 +823,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
++input_ptr_;
Expand Down Expand Up @@ -887,7 +903,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
++input_ptr_;
Expand Down Expand Up @@ -962,7 +978,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
++input_ptr_;
Expand Down Expand Up @@ -1043,7 +1059,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
push_state(state_);
Expand Down Expand Up @@ -1097,7 +1113,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
push_state(state_);
Expand Down Expand Up @@ -1245,7 +1261,7 @@ class basic_json_parser : public ser_context
mark_position_ = position_;
break;
case ' ':case '\t':
skip_space();
skip_space(ec);
break;
case '/':
++input_ptr_;
Expand Down Expand Up @@ -1780,7 +1796,7 @@ class basic_json_parser : public ser_context
minus_sign:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -1807,7 +1823,7 @@ class basic_json_parser : public ser_context
zero:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
if (JSONCONS_UNLIKELY(ec))
{
Expand Down Expand Up @@ -1839,7 +1855,7 @@ class basic_json_parser : public ser_context
case ' ':case '\t':
end_integer_value(visitor, ec);
if (ec) return;
skip_space();
skip_space(ec);
return;
case '/':
end_integer_value(visitor, ec);
Expand Down Expand Up @@ -1889,7 +1905,7 @@ class basic_json_parser : public ser_context
integer:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
if (JSONCONS_UNLIKELY(ec))
{
Expand Down Expand Up @@ -1921,7 +1937,7 @@ class basic_json_parser : public ser_context
case ' ':case '\t':
end_integer_value(visitor, ec);
if (ec) return;
skip_space();
skip_space(ec);
return;
case '/':
end_integer_value(visitor, ec);
Expand Down Expand Up @@ -1970,7 +1986,7 @@ class basic_json_parser : public ser_context
fraction1:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -1993,7 +2009,7 @@ class basic_json_parser : public ser_context
fraction2:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
if (JSONCONS_UNLIKELY(ec))
{
Expand Down Expand Up @@ -2025,7 +2041,7 @@ class basic_json_parser : public ser_context
case ' ':case '\t':
end_fraction_value(visitor, ec);
if (ec) return;
skip_space();
skip_space(ec);
return;
case '/':
end_fraction_value(visitor, ec);
Expand Down Expand Up @@ -2073,7 +2089,7 @@ class basic_json_parser : public ser_context
exp1:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand Down Expand Up @@ -2105,7 +2121,7 @@ class basic_json_parser : public ser_context
exp2:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2129,7 +2145,7 @@ class basic_json_parser : public ser_context
exp3:
if (JSONCONS_UNLIKELY(input_ptr_ >= local_input_end)) // Buffer exhausted
{
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
if (JSONCONS_UNLIKELY(ec))
{
Expand Down Expand Up @@ -2161,7 +2177,7 @@ class basic_json_parser : public ser_context
case ' ':case '\t':
end_fraction_value(visitor, ec);
if (ec) return;
skip_space();
skip_space(ec);
return;
case '/':
end_fraction_value(visitor, ec);
Expand Down Expand Up @@ -2346,7 +2362,7 @@ class basic_json_parser : public ser_context
{
string_buffer_.append(sb,input_ptr_-sb);
position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2360,7 +2376,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand Down Expand Up @@ -2427,7 +2443,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2451,7 +2467,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2475,7 +2491,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2499,7 +2515,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand Down Expand Up @@ -2534,7 +2550,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand Down Expand Up @@ -2563,7 +2579,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand Down Expand Up @@ -2591,7 +2607,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2615,7 +2631,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2639,7 +2655,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand All @@ -2663,7 +2679,7 @@ class basic_json_parser : public ser_context
{
//string_buffer_.append(sb,input_ptr_-sb);
//position_ += (input_ptr_ - sb);
if (!more_command_->read_more(ec))
if (!chunk_rdr_->read_chunk(ec))
{
return;
}
Expand Down
Loading

0 comments on commit a4434b9

Please sign in to comment.