Skip to content

fix: using stream operator not set eofbit - #146

Open
Night12138 wants to merge 1 commit into
kazuho:masterfrom
Night12138:fix/no_eof
Open

fix: using stream operator not set eofbit#146
Night12138 wants to merge 1 commit into
kazuho:masterfrom
Night12138:fix/no_eof

Conversation

@Night12138

@Night12138 Night12138 commented Feb 11, 2022

Copy link
Copy Markdown

In the current version, there are the following issue

#include <iostream>
#include "picojson.hpp"

int main()
{
    while (!std::cin.eof()) // won't break
    {
        picojson::value v;
        std::cin >> v;
        // processing
        const auto &err = picojson::get_last_error();
        std::cout << "err: " << err << std::endl;
        std::cout << "res: " << v << std::endl;
    }
    return 0;
}

at Line 6, when read eof state from std::cin always return false.

fix: add beg/end judge in parse(value &out, std::istream &is) at Line 1135

inline std::string parse(value &out, std::istream &is) {
  std::string err;
  const auto &beg = std::istreambuf_iterator<char>(is.rdbuf());
  const auto &end = std::istreambuf_iterator<char>();
  parse(out, beg, end, &err);
  if (beg == end) {
    is.setstate(std::ios_base::eofbit); // set eof state because stream has no more data available
  }
  return err;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant