diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index a26e30f3..9f5961e6 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -154,11 +154,6 @@ class parse_error : public exception static std::string position_string(const position_t& pos) { - if (pos.chars_read_total == 0) - { - return ""; - } - return " at line " + std::to_string(pos.lines_read + 1) + ", column " + std::to_string(pos.chars_read_current_line); } diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 815cac81..05eb112e 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -1278,12 +1278,18 @@ scan_number_done: next_unget = true; --position.chars_read_total; - --position.chars_read_current_line; // in case we "unget" a newline, we have to also decrement the lines_read - if (position.lines_read != 0 and position.chars_read_current_line == 0) + if (position.chars_read_current_line == 0) { - --position.lines_read; + if (position.lines_read > 0) + { + --position.lines_read; + } + } + else + { + --position.chars_read_current_line; } if (JSON_LIKELY(current != std::char_traits::eof())) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 957cfc24..e551784a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -795,11 +795,6 @@ class parse_error : public exception static std::string position_string(const position_t& pos) { - if (pos.chars_read_total == 0) - { - return ""; - } - return " at line " + std::to_string(pos.lines_read + 1) + ", column " + std::to_string(pos.chars_read_current_line); } @@ -3595,12 +3590,18 @@ scan_number_done: next_unget = true; --position.chars_read_total; - --position.chars_read_current_line; // in case we "unget" a newline, we have to also decrement the lines_read - if (position.lines_read != 0 and position.chars_read_current_line == 0) + if (position.chars_read_current_line == 0) { - --position.lines_read; + if (position.lines_read > 0) + { + --position.lines_read; + } + } + else + { + --position.chars_read_current_line; } if (JSON_LIKELY(current != std::char_traits::eof())) diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 1050f05b..8dac4a44 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -1292,6 +1292,9 @@ TEST_CASE("parser class") CHECK(accept_helper("\"\\u01") == false); CHECK(accept_helper("\"\\u012") == false); + // unget of newline + CHECK(parser_helper("\n123\n") == 123); + // invalid escapes for (int c = 1; c < 128; ++c) {