From 12efeadc2eead552c5155352b84cd4dd899e169c Mon Sep 17 00:00:00 2001 From: Perry Kundert Date: Tue, 3 Oct 2017 08:49:39 -0700 Subject: [PATCH] Further simplify istream handling; use native unget --- src/json.hpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 942acb87..160fa4eb 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1398,6 +1398,7 @@ constexpr T static_const::value; struct input_adapter_protocol { virtual int get_character() = 0; + virtual void unget_character() = 0; virtual ~input_adapter_protocol() = default; }; @@ -1448,6 +1449,10 @@ class input_stream_adapter : public input_adapter_protocol return c == std::char_traits::eof() ? c : ( c & 0xFF ); } + void unget_character() override + { + is.unget(); + } private: /// the associated input stream @@ -1482,6 +1487,14 @@ class input_buffer_adapter : public input_adapter_protocol return std::char_traits::eof(); } + void unget_character() noexcept override + { + if (JSON_LIKELY(cursor > 0)) + { + --cursor; + } + } + private: /// pointer to the current character const char* cursor; @@ -2647,8 +2660,8 @@ scan_number_done: int get() { ++chars_read; - int c = next_unget ? (next_unget = false, current) - : (current = ia->get_character()); + + int c = current = ia->get_character(); token_string += static_cast( c ); return c; } @@ -2657,7 +2670,7 @@ scan_number_done: void unget() { --chars_read; - next_unget = true; + if (token_string.size() > 0) token_string.resize( token_string.size() - 1 ); } @@ -2825,9 +2838,6 @@ scan_number_done: /// the current character int current = std::char_traits::eof(); - /// whether get() should return the last character again - bool next_unget = false; - /// the number of characters read std::size_t chars_read = 0; /// the start position of the current token