From 06ca1979b0353ad61a98cb46b1674865bcaa4151 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 3 Jan 2022 20:45:26 +0100 Subject: [PATCH] :recycle: remove stringstream --- include/nlohmann/detail/output/serializer.hpp | 23 +++++++++++++------ single_include/nlohmann/json.hpp | 23 +++++++++++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 7cce7d5c7c..cc63ab1d3d 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -10,7 +10,6 @@ #include // numeric_limits #include // string, char_traits #include // setfill, setw -#include // stringstream #include // is_same #include // move @@ -501,9 +500,7 @@ class serializer { case error_handler_t::strict: { - std::stringstream ss; - ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (byte | 0); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + ss.str(), BasicJsonType())); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + hex_bytes(byte | 0), BasicJsonType())); } case error_handler_t::ignore: @@ -595,9 +592,7 @@ class serializer { case error_handler_t::strict: { - std::stringstream ss; - ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (static_cast(s.back()) | 0); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + ss.str(), BasicJsonType())); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + hex_bytes(static_cast(s.back() | 0)), BasicJsonType())); } case error_handler_t::ignore: @@ -664,6 +659,20 @@ class serializer } } + /*! + * @brief convert a byte to a uppercase hex representation + * @param[in] byte byte to represent + * @return representation ("00".."FF") + */ + inline std::string hex_bytes(std::uint8_t byte) + { + std::string result = "FF"; + constexpr const char* nibble_to_hex = "0123456789ABCDEF"; + result[0] = nibble_to_hex[byte / 16]; + result[1] = nibble_to_hex[byte % 16]; + return result; + } + // templates to avoid warnings about useless casts template ::value, int> = 0> bool is_negative_number(NumberType x) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bfa5c823c4..e573adc135 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14935,7 +14935,6 @@ class binary_writer #include // numeric_limits #include // string, char_traits #include // setfill, setw -#include // stringstream #include // is_same #include // move @@ -16544,9 +16543,7 @@ class serializer { case error_handler_t::strict: { - std::stringstream ss; - ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (byte | 0); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + ss.str(), BasicJsonType())); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + hex_bytes(byte | 0), BasicJsonType())); } case error_handler_t::ignore: @@ -16638,9 +16635,7 @@ class serializer { case error_handler_t::strict: { - std::stringstream ss; - ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (static_cast(s.back()) | 0); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + ss.str(), BasicJsonType())); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + hex_bytes(static_cast(s.back() | 0)), BasicJsonType())); } case error_handler_t::ignore: @@ -16707,6 +16702,20 @@ class serializer } } + /*! + * @brief convert a byte to a uppercase hex representation + * @param[in] byte byte to represent + * @return representation ("00".."FF") + */ + inline std::string hex_bytes(std::uint8_t byte) + { + std::string result = "FF"; + constexpr const char* nibble_to_hex = "0123456789ABCDEF"; + result[0] = nibble_to_hex[byte / 16]; + result[1] = nibble_to_hex[byte % 16]; + return result; + } + // templates to avoid warnings about useless casts template ::value, int> = 0> bool is_negative_number(NumberType x)