🔨 fixed another warning

Do not store eof() in a char buffer…
This commit is contained in:
Niels Lohmann 2017-04-06 19:54:08 +02:00
parent b992acc2e7
commit ff72f38863
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69

View file

@ -8485,8 +8485,7 @@ class basic_json
{ {
public: public:
cached_input_stream_adapter(std::istream& i, const size_t buffer_size) cached_input_stream_adapter(std::istream& i, const size_t buffer_size)
: is(i), start_position(is.tellg()), : is(i), start_position(is.tellg()), buffer(buffer_size, '\0')
buffer(buffer_size, std::char_traits<char>::eof())
{ {
// immediately abort if stream is erroneous // immediately abort if stream is erroneous
if (JSON_UNLIKELY(i.fail())) if (JSON_UNLIKELY(i.fail()))
@ -8494,12 +8493,13 @@ class basic_json
JSON_THROW(parse_error::create(111, 0, "bad input stream")); JSON_THROW(parse_error::create(111, 0, "bad input stream"));
} }
// initial fill; unfilled buffer characters remain EOF // initial fill
is.read(buffer.data(), static_cast<std::streamsize>(buffer.size())); is.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
// store number of bytes in the buffer
fill_size = static_cast<size_t>(is.gcount());
// skip byte-order mark // skip byte-order mark
assert(buffer.size() >= 3); if (fill_size >= 3 and buffer[0] == '\xEF' and buffer[1] == '\xBB' and buffer[2] == '\xBF')
if (buffer[0] == '\xEF' and buffer[1] == '\xBB' and buffer[2] == '\xBF')
{ {
buffer_pos += 3; buffer_pos += 3;
processed_chars += 3; processed_chars += 3;
@ -8516,22 +8516,28 @@ class basic_json
int get_character() override int get_character() override
{ {
// check if refilling is necessary // check if refilling is necessary and possible
if (JSON_UNLIKELY(buffer_pos == buffer.size())) if (buffer_pos == fill_size and not eof)
{ {
// refill // refill
is.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(buffer.size())); is.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
// set unfilled characters to EOF // store number of bytes in the buffer
std::fill_n(buffer.begin() + static_cast<int>(is.gcount()), fill_size = static_cast<size_t>(is.gcount());
buffer.size() - static_cast<size_t>(is.gcount()),
std::char_traits<char>::eof()); // remember that filling did not yield new input
if (fill_size == 0)
{
eof = true;
}
// the buffer is ready // the buffer is ready
buffer_pos = 0; buffer_pos = 0;
} }
++processed_chars; ++processed_chars;
const int res = buffer[buffer_pos++]; return eof
return (res == std::char_traits<char>::eof()) ? res : res & 0xFF; ? std::char_traits<char>::eof()
: buffer[buffer_pos++] & 0xFF;
} }
std::string read(size_t offset, size_t length) override std::string read(size_t offset, size_t length) override
@ -8568,6 +8574,11 @@ class basic_json
/// chars processed in the current buffer /// chars processed in the current buffer
size_t buffer_pos = 0; size_t buffer_pos = 0;
/// whether stream reached eof
bool eof = false;
/// how many chars have been copied to the buffer by last (re)fill
size_t fill_size = 0;
/// position of the stream when we started /// position of the stream when we started
const std::streampos start_position; const std::streampos start_position;