From 0a4a6a8399d1f4194d009c50ec076dd287cafa9e Mon Sep 17 00:00:00 2001 From: Alex Astashyn Date: Wed, 7 Dec 2016 19:53:27 -0500 Subject: [PATCH] Refactored to avoid using exceptions, as there are plans to support exceptionless mode --- src/json.hpp | 78 +++++++++++++++++++++++++++++++++++------------ src/json.hpp.re2c | 78 +++++++++++++++++++++++++++++++++++------------ 2 files changed, 118 insertions(+), 38 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index 1644d91f2..79d6bb024 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -9079,10 +9079,42 @@ basic_json_parser_66: struct strtonum { public: - strtonum(const char* start, const char* end) + template + struct maybe + { + T x; + bool valid; + + maybe(const T& xx) + : x(xx), valid(true) + {} + + maybe() : x(), valid(false) + {} + + operator bool() const + { + return valid; + } + + const T& operator*() const + { + return x; + } + }; + + strtonum(const char* start, const char* end) : m_start(start), m_end(end) {} + template::value>::type > + bool to(T& val) const + { + return parse(val, std::is_integral()); + } + template::value>::type > @@ -9361,31 +9393,39 @@ basic_json_parser_66: const bool is_negative = *m_start == '-'; - try { - if (not num.is_integral()) - { - ; - } - else if (is_negative) - { + result.m_type = value_t::discarded; + + if (not num.is_integral()) + { + ; // will parse as float below + } + else if (is_negative) + { + number_integer_t val{0}; + if(num.to(val)) { result.m_type = value_t::number_integer; - result.m_value = static_cast(num); - return; - } - else - { - result.m_type = value_t::number_unsigned; - result.m_value = static_cast(num); - return; + result.m_value = val; } } - catch (std::invalid_argument&) + else { - ; // overflow - will parse as double + number_unsigned_t val{0}; + if(num.to(val)) { + result.m_type = value_t::number_unsigned; + result.m_value = val; + } + } + + number_float_t val{0}; + if (result.m_type != value_t::discarded + or !num.to(val)) + { + return; // already have a value from above + // or couldn't parse as float_t } result.m_type = value_t::number_float; - result.m_value = static_cast(num); + result.m_value = val; // replace infinity and NAN by null if (not std::isfinite(result.m_value.number_float)) diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index b541db1e9..24c549755 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -8228,10 +8228,42 @@ class basic_json struct strtonum { public: - strtonum(const char* start, const char* end) + template + struct maybe + { + T x; + bool valid; + + maybe(const T& xx) + : x(xx), valid(true) + {} + + maybe() : x(), valid(false) + {} + + operator bool() const + { + return valid; + } + + const T& operator*() const + { + return x; + } + }; + + strtonum(const char* start, const char* end) : m_start(start), m_end(end) {} + template::value>::type > + bool to(T& val) const + { + return parse(val, std::is_integral()); + } + template::value>::type > @@ -8510,31 +8542,39 @@ class basic_json const bool is_negative = *m_start == '-'; - try { - if (not num.is_integral()) - { - ; - } - else if (is_negative) - { + result.m_type = value_t::discarded; + + if (not num.is_integral()) + { + ; // will parse as float below + } + else if (is_negative) + { + number_integer_t val{0}; + if(num.to(val)) { result.m_type = value_t::number_integer; - result.m_value = static_cast(num); - return; - } - else - { - result.m_type = value_t::number_unsigned; - result.m_value = static_cast(num); - return; + result.m_value = val; } } - catch (std::invalid_argument&) + else { - ; // overflow - will parse as double + number_unsigned_t val{0}; + if(num.to(val)) { + result.m_type = value_t::number_unsigned; + result.m_value = val; + } + } + + number_float_t val{0}; + if (result.m_type != value_t::discarded + or !num.to(val)) + { + return; // already have a value from above + // or couldn't parse as float_t } result.m_type = value_t::number_float; - result.m_value = static_cast(num); + result.m_value = val; // replace infinity and NAN by null if (not std::isfinite(result.m_value.number_float))