🐛 skipping BOM for iterators #602

I totally forgot about byte order marks in this scenario.
parent c043ba69
...@@ -8814,7 +8814,7 @@ class basic_json ...@@ -8814,7 +8814,7 @@ class basic_json
// store number of bytes in the buffer // store number of bytes in the buffer
fill_size = static_cast<size_t>(is.gcount()); fill_size = static_cast<size_t>(is.gcount());
// skip byte-order mark // skip byte order mark
if (fill_size >= 3 and buffer[0] == '\xEF' and buffer[1] == '\xBB' and buffer[2] == '\xBF') if (fill_size >= 3 and buffer[0] == '\xEF' and buffer[1] == '\xBB' and buffer[2] == '\xBF')
{ {
buffer_pos += 3; buffer_pos += 3;
...@@ -8911,7 +8911,13 @@ class basic_json ...@@ -8911,7 +8911,13 @@ class basic_json
public: public:
input_buffer_adapter(const char* b, size_t l) input_buffer_adapter(const char* b, size_t l)
: input_adapter(), cursor(b), limit(b + l), start(b) : input_adapter(), cursor(b), limit(b + l), start(b)
{} {
// skip byte order mark
if (l >= 3 and b[0] == '\xEF' and b[1] == '\xBB' and b[2] == '\xBF')
{
cursor += 3;
}
}
// delete because of pointer members // delete because of pointer members
input_buffer_adapter(const input_buffer_adapter&) = delete; input_buffer_adapter(const input_buffer_adapter&) = delete;
......
...@@ -1169,4 +1169,10 @@ TEST_CASE("regression tests") ...@@ -1169,4 +1169,10 @@ TEST_CASE("regression tests")
std::vector<uint8_t> vec = {'"', '\\', '"', 'X', '"', '"'}; std::vector<uint8_t> vec = {'"', '\\', '"', 'X', '"', '"'};
CHECK_THROWS_AS(json::parse(vec), json::parse_error); CHECK_THROWS_AS(json::parse(vec), json::parse_error);
} }
SECTION("issue #602 - BOM not skipped when using json:parse(iterator)")
{
std::string i = "\xef\xbb\xbf{\n \"foo\": true\n}";
CHECK_NOTHROW(json::parse(i.begin(), i.end()));
}
} }
...@@ -1012,12 +1012,21 @@ TEST_CASE("Unicode", "[hide]") ...@@ -1012,12 +1012,21 @@ TEST_CASE("Unicode", "[hide]")
SECTION("ignore byte-order-mark") SECTION("ignore byte-order-mark")
{ {
SECTION("in a stream")
{
// read a file with a UTF-8 BOM // read a file with a UTF-8 BOM
std::ifstream f("test/data/json_nlohmann_tests/bom.json"); std::ifstream f("test/data/json_nlohmann_tests/bom.json");
json j; json j;
CHECK_NOTHROW(f >> j); CHECK_NOTHROW(f >> j);
} }
SECTION("with an iterator")
{
std::string i = "\xef\xbb\xbf{\n \"foo\": true\n}";
CHECK_NOTHROW(json::parse(i.begin(), i.end()));
}
}
SECTION("error for incomplete/wrong BOM") SECTION("error for incomplete/wrong BOM")
{ {
CHECK_THROWS_AS(json::parse("\xef\xbb"), json::parse_error); CHECK_THROWS_AS(json::parse("\xef\xbb"), json::parse_error);
......
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