Commit 12efeadc authored by Perry Kundert's avatar Perry Kundert

Further simplify istream handling; use native unget

parent f585fe4e
...@@ -1398,6 +1398,7 @@ constexpr T static_const<T>::value; ...@@ -1398,6 +1398,7 @@ constexpr T static_const<T>::value;
struct input_adapter_protocol struct input_adapter_protocol
{ {
virtual int get_character() = 0; virtual int get_character() = 0;
virtual void unget_character() = 0;
virtual ~input_adapter_protocol() = default; virtual ~input_adapter_protocol() = default;
}; };
...@@ -1448,6 +1449,10 @@ class input_stream_adapter : public input_adapter_protocol ...@@ -1448,6 +1449,10 @@ class input_stream_adapter : public input_adapter_protocol
return c == std::char_traits<char>::eof() ? c : ( c & 0xFF ); return c == std::char_traits<char>::eof() ? c : ( c & 0xFF );
} }
void unget_character() override
{
is.unget();
}
private: private:
/// the associated input stream /// the associated input stream
...@@ -1482,6 +1487,14 @@ class input_buffer_adapter : public input_adapter_protocol ...@@ -1482,6 +1487,14 @@ class input_buffer_adapter : public input_adapter_protocol
return std::char_traits<char>::eof(); return std::char_traits<char>::eof();
} }
void unget_character() noexcept override
{
if (JSON_LIKELY(cursor > 0))
{
--cursor;
}
}
private: private:
/// pointer to the current character /// pointer to the current character
const char* cursor; const char* cursor;
...@@ -2647,8 +2660,8 @@ scan_number_done: ...@@ -2647,8 +2660,8 @@ scan_number_done:
int get() int get()
{ {
++chars_read; ++chars_read;
int c = next_unget ? (next_unget = false, current)
: (current = ia->get_character()); int c = current = ia->get_character();
token_string += static_cast<char>( c ); token_string += static_cast<char>( c );
return c; return c;
} }
...@@ -2657,7 +2670,7 @@ scan_number_done: ...@@ -2657,7 +2670,7 @@ scan_number_done:
void unget() void unget()
{ {
--chars_read; --chars_read;
next_unget = true;
if (token_string.size() > 0) if (token_string.size() > 0)
token_string.resize( token_string.size() - 1 ); token_string.resize( token_string.size() - 1 );
} }
...@@ -2825,9 +2838,6 @@ scan_number_done: ...@@ -2825,9 +2838,6 @@ scan_number_done:
/// the current character /// the current character
int current = std::char_traits<char>::eof(); int current = std::char_traits<char>::eof();
/// whether get() should return the last character again
bool next_unget = false;
/// the number of characters read /// the number of characters read
std::size_t chars_read = 0; std::size_t chars_read = 0;
/// the start position of the current token /// the start position of the current token
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment