From 112a6f4e53bc3417c1fc30d1b3097b0b68556cfb Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 18 Jun 2017 13:03:14 +0200 Subject: [PATCH] :hammer: replaced lexer's error message by const char* --- src/json.hpp | 23 ++++++++++++----------- test/src/unit-class_parser.cpp | 16 ++++++++-------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 2ed408c8c..a1c15528e 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -8896,7 +8896,8 @@ class basic_json } ++processed_chars; - return buffer[buffer_pos++] & 0xFF;; + assert(buffer_pos < buffer.size()); + return buffer[buffer_pos++] & 0xFF; } std::string read(size_t offset, size_t length) override @@ -8971,7 +8972,7 @@ class basic_json input_buffer_adapter(const input_buffer_adapter&) = delete; input_buffer_adapter& operator=(input_buffer_adapter&) = delete; - int get_character() override + int get_character() noexcept override { if (JSON_LIKELY(cursor < limit)) { @@ -11510,13 +11511,13 @@ class basic_json } else { - error_message = "invalid string: surrogate " + codepoint_to_string(codepoint1) + " must be followed by U+DC00..U+DFFF instead of " + codepoint_to_string(codepoint2); + error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF"; return token_type::parse_error; } } else { - error_message = "invalid string: surrogate " + codepoint_to_string(codepoint1) + " must be followed by U+DC00..U+DFFF"; + error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF"; return token_type::parse_error; } } @@ -11524,7 +11525,7 @@ class basic_json { if (JSON_UNLIKELY(0xDC00 <= codepoint1 and codepoint1 <= 0xDFFF)) { - error_message = "invalid string: surrogate " + codepoint_to_string(codepoint1) + " must follow U+D800..U+DBFF"; + error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF"; return token_type::parse_error; } @@ -11609,7 +11610,7 @@ class basic_json case 0x1e: case 0x1f: { - error_message = "invalid string: control character " + codepoint_to_string(current) + " must be escaped"; + error_message = "invalid string: control character must be escaped"; return token_type::parse_error; } @@ -12450,7 +12451,7 @@ scan_number_done: } /// return syntax error message - const std::string& get_error_message() const noexcept + const char* get_error_message() const noexcept { return error_message; } @@ -12544,7 +12545,7 @@ scan_number_done: size_t yylen = 0; /// a description of occurred lexer errors - std::string error_message = ""; + const char* error_message = ""; // number values number_integer_t value_integer = 0; @@ -12975,7 +12976,7 @@ scan_number_done: std::string error_msg = "syntax error - "; if (last_token == lexer::token_type::parse_error) { - error_msg += m_lexer.get_error_message() + "; last read: '" + m_lexer.get_token_string() + "'"; + error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" + m_lexer.get_token_string() + "'"; } else { @@ -12997,7 +12998,7 @@ scan_number_done: std::string error_msg = "syntax error - "; if (last_token == lexer::token_type::parse_error) { - error_msg += m_lexer.get_error_message() + "; last read '" + m_lexer.get_token_string() + "'"; + error_msg += std::string(m_lexer.get_error_message()) + "; last read '" + m_lexer.get_token_string() + "'"; } else { @@ -14189,7 +14190,7 @@ scan_number_done: case patch_operations::copy: { - const std::string from_path = get_value("copy", "from", true);; + const std::string from_path = get_value("copy", "from", true); const json_pointer from_ptr(from_path); // the "from" location must exist - use at() diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp index 5a2ff3507..920054d2f 100644 --- a/test/src/unit-class_parser.cpp +++ b/test/src/unit-class_parser.cpp @@ -98,18 +98,18 @@ TEST_CASE("parser class") // error: tab in string CHECK_THROWS_AS(parse_string("\"\t\"").parse(), json::parse_error); CHECK_THROWS_WITH(parse_string("\"\t\"").parse(), - "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character U+0009 must be escaped; last read '\"'"); + "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character must be escaped; last read '\"'"); // error: newline in string CHECK_THROWS_AS(parse_string("\"\n\"").parse(), json::parse_error); CHECK_THROWS_AS(parse_string("\"\r\"").parse(), json::parse_error); CHECK_THROWS_WITH(parse_string("\"\n\"").parse(), - "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character U+000A must be escaped; last read '\"'"); + "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character must be escaped; last read '\"'"); CHECK_THROWS_WITH(parse_string("\"\r\"").parse(), - "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character U+000D must be escaped; last read '\"'"); + "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character must be escaped; last read '\"'"); // error: backspace in string CHECK_THROWS_AS(parse_string("\"\b\"").parse(), json::parse_error); CHECK_THROWS_WITH(parse_string("\"\b\"").parse(), - "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character U+0008 must be escaped; last read '\"'"); + "[json.exception.parse_error.101] parse error at 2: syntax error - invalid string: control character must be escaped; last read '\"'"); // improve code coverage CHECK_THROWS_AS(parse_string("\uFF01").parse(), json::parse_error); CHECK_THROWS_AS(parse_string("[-4:1,]").parse(), json::parse_error); @@ -910,17 +910,17 @@ TEST_CASE("parser class") // missing part of a surrogate pair CHECK_THROWS_AS(json::parse("\"\\uD80C\""), json::parse_error); CHECK_THROWS_WITH(json::parse("\"\\uD80C\""), - "[json.exception.parse_error.101] parse error at 8: syntax error - invalid string: surrogate U+D80C must be followed by U+DC00..U+DFFF; last read '\"\\uD80C\"'"); + "[json.exception.parse_error.101] parse error at 8: syntax error - invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF; last read '\"\\uD80C\"'"); // invalid surrogate pair CHECK_THROWS_AS(json::parse("\"\\uD80C\\uD80C\""), json::parse_error); CHECK_THROWS_AS(json::parse("\"\\uD80C\\u0000\""), json::parse_error); CHECK_THROWS_AS(json::parse("\"\\uD80C\\uFFFF\""), json::parse_error); CHECK_THROWS_WITH(json::parse("\"\\uD80C\\uD80C\""), - "[json.exception.parse_error.101] parse error at 13: syntax error - invalid string: surrogate U+D80C must be followed by U+DC00..U+DFFF instead of U+D80C; last read '\"\\uD80C\\uD80C'"); + "[json.exception.parse_error.101] parse error at 13: syntax error - invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF; last read '\"\\uD80C\\uD80C'"); CHECK_THROWS_WITH(json::parse("\"\\uD80C\\u0000\""), - "[json.exception.parse_error.101] parse error at 13: syntax error - invalid string: surrogate U+D80C must be followed by U+DC00..U+DFFF instead of U+0000; last read '\"\\uD80C\\u0000'"); + "[json.exception.parse_error.101] parse error at 13: syntax error - invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF; last read '\"\\uD80C\\u0000'"); CHECK_THROWS_WITH(json::parse("\"\\uD80C\\uFFFF\""), - "[json.exception.parse_error.101] parse error at 13: syntax error - invalid string: surrogate U+D80C must be followed by U+DC00..U+DFFF instead of U+FFFF; last read '\"\\uD80C\\uFFFF'"); + "[json.exception.parse_error.101] parse error at 13: syntax error - invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF; last read '\"\\uD80C\\uFFFF'"); } SECTION("parse errors (accept)")