From cf14cbdd02aef4fc299164344fbf3140d4d5083d Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 22 Jul 2026 21:03:03 +0000 Subject: [PATCH] Deduplicate UBJSON/BJData length-type error message The "expected length type specification" parse error was built with an identical if/else block in both get_ubjson_string() and get_ubjson_size_value(), each branching on the input format to pick the marker list. Extract a single unexpected_length_type_message() helper that selects the markers via a ternary and appends the caller-supplied infix, removing four near-duplicate message strings and centralizing the marker list. Error messages are unchanged. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01Tv6SnfYbcx8y3y4PrECcto --- .../nlohmann/detail/input/binary_reader.hpp | 44 +++++++++---------- single_include/nlohmann/json.hpp | 44 +++++++++---------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 4f8ab79f2..3a1a82ef7 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1869,6 +1869,24 @@ class binary_reader return true; } + /*! + @brief create the error message for a missing/invalid length-type marker + + Used both when reading a UBJSON/BJData string and when reading an optimized + container size. The accepted markers depend on the input format, and the + size variant appends " after '#'" via @a infix. + + @param[in] last_token the offending byte as returned by get_token_string() + @param[in] infix extra context inserted after the marker list + @return the formatted error message + */ + std::string unexpected_length_type_message(const std::string& last_token, const char* infix) const + { + return concat("expected length type specification (", + input_format == input_format_t::bjdata ? "U, i, u, I, m, l, M, L" : "U, i, I, l, L", + ")", infix, "; last byte: 0x", last_token); + } + /*! @brief reads a UBJSON string @@ -1961,17 +1979,8 @@ class binary_reader break; } auto last_token = get_token_string(); - std::string message; - - if (input_format != input_format_t::bjdata) - { - message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token; - } - else - { - message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token; - } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr)); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format, unexpected_length_type_message(last_token, ""), "string"), nullptr)); } /*! @@ -2250,17 +2259,8 @@ class binary_reader break; } auto last_token = get_token_string(); - std::string message; - - if (input_format != input_format_t::bjdata) - { - message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token; - } - else - { - message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token; - } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr)); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format, unexpected_length_type_message(last_token, " after '#'"), "size"), nullptr)); } /*! diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 580b9bd02..d8cc2962d 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -12418,6 +12418,24 @@ class binary_reader return true; } + /*! + @brief create the error message for a missing/invalid length-type marker + + Used both when reading a UBJSON/BJData string and when reading an optimized + container size. The accepted markers depend on the input format, and the + size variant appends " after '#'" via @a infix. + + @param[in] last_token the offending byte as returned by get_token_string() + @param[in] infix extra context inserted after the marker list + @return the formatted error message + */ + std::string unexpected_length_type_message(const std::string& last_token, const char* infix) const + { + return concat("expected length type specification (", + input_format == input_format_t::bjdata ? "U, i, u, I, m, l, M, L" : "U, i, I, l, L", + ")", infix, "; last byte: 0x", last_token); + } + /*! @brief reads a UBJSON string @@ -12510,17 +12528,8 @@ class binary_reader break; } auto last_token = get_token_string(); - std::string message; - - if (input_format != input_format_t::bjdata) - { - message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token; - } - else - { - message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token; - } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr)); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format, unexpected_length_type_message(last_token, ""), "string"), nullptr)); } /*! @@ -12799,17 +12808,8 @@ class binary_reader break; } auto last_token = get_token_string(); - std::string message; - - if (input_format != input_format_t::bjdata) - { - message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token; - } - else - { - message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token; - } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr)); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, + exception_message(input_format, unexpected_length_type_message(last_token, " after '#'"), "size"), nullptr)); } /*!