From 125b0f4a6b44ab720220141d99964c8d55e74dfe Mon Sep 17 00:00:00 2001 From: Kevin Xu Date: Mon, 6 Dec 2021 19:51:27 -0500 Subject: [PATCH] move number parsing to numerizer class --- include/nlohmann/detail/input/lexer.hpp | 27 +++---------- include/nlohmann/detail/input/numerizer.hpp | 44 +++++++++++++++++++++ include/nlohmann/json_fwd.hpp | 9 ++++- 3 files changed, 56 insertions(+), 24 deletions(-) create mode 100644 include/nlohmann/detail/input/numerizer.hpp diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index 3a47167e9..9b2dc4fa6 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace nlohmann { @@ -99,7 +100,7 @@ class lexer_base This class organizes the lexical analysis during JSON deserialization. */ -template +template class lexer : public lexer_base { using number_integer_t = typename BasicJsonType::number_integer_t; @@ -900,24 +901,6 @@ class lexer : public lexer_base } } - JSON_HEDLEY_NON_NULL(2) - static void strtof(float& f, const char* str, char** endptr) noexcept - { - f = std::strtof(str, endptr); - } - - JSON_HEDLEY_NON_NULL(2) - static void strtof(double& f, const char* str, char** endptr) noexcept - { - f = std::strtod(str, endptr); - } - - JSON_HEDLEY_NON_NULL(2) - static void strtof(long double& f, const char* str, char** endptr) noexcept - { - f = std::strtold(str, endptr); - } - /*! @brief scan a number literal @@ -1242,7 +1225,7 @@ scan_number_done: // try to parse integers first and fall back to floats if (number_type == token_type::value_unsigned) { - const auto x = std::strtoull(token_buffer.data(), &endptr, 10); + const auto x = NumerizerType::strtoull(token_buffer.data(), &endptr, 10); // we checked the number format before JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); @@ -1258,7 +1241,7 @@ scan_number_done: } else if (number_type == token_type::value_integer) { - const auto x = std::strtoll(token_buffer.data(), &endptr, 10); + const auto x = NumerizerType::strtoll(token_buffer.data(), &endptr, 10); // we checked the number format before JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); @@ -1275,7 +1258,7 @@ scan_number_done: // this code is reached if we parse a floating-point number or if an // integer conversion above failed - strtof(value_float, token_buffer.data(), &endptr); + NumerizerType::strtof(value_float, token_buffer.data(), &endptr); // we checked the number format before JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); diff --git a/include/nlohmann/detail/input/numerizer.hpp b/include/nlohmann/detail/input/numerizer.hpp new file mode 100644 index 000000000..b40ef5b5d --- /dev/null +++ b/include/nlohmann/detail/input/numerizer.hpp @@ -0,0 +1,44 @@ + +#include // strtof, strtod, strtold, strtoll, strtoull + +namespace nlohmann +{ +namespace detail +{ + +struct numerizer +{ + JSON_HEDLEY_NON_NULL(2) + static void strtof(float& f, const char* str, char** endptr) noexcept + { + f = std::strtof(str, endptr); + } + + JSON_HEDLEY_NON_NULL(2) + static void strtof(double& f, const char* str, char** endptr) noexcept + { + f = std::strtod(str, endptr); + } + + JSON_HEDLEY_NON_NULL(2) + static void strtof(long double& f, const char* str, char** endptr) noexcept + { + f = std::strtold(str, endptr); + } + + JSON_HEDLEY_NON_NULL(2) + static auto strtoull(const char* str, char** endptr, int base) + { + return std::strtoull(str, endptr, base); + } + + JSON_HEDLEY_NON_NULL(2) + static auto strtoll(const char* str, char** endptr, int base) + { + return std::strtoll(str, endptr, base); + } +}; + + +} +} \ No newline at end of file diff --git a/include/nlohmann/json_fwd.hpp b/include/nlohmann/json_fwd.hpp index 66c685f8c..88cd01776 100644 --- a/include/nlohmann/json_fwd.hpp +++ b/include/nlohmann/json_fwd.hpp @@ -26,9 +26,14 @@ struct adl_serializer; namespace detail { -template +template class lexer; +struct numerizer; + +template +using basic_lexer = lexer; + template class serializer; } @@ -44,7 +49,7 @@ template class ObjectType = template class JSONSerializer = adl_serializer, class BinaryType = std::vector, - template class LexerType = detail::lexer, + template class LexerType = detail::basic_lexer, template class SerializerType = detail::serializer> class basic_json;