stylistic changes

This commit is contained in:
Alex Astashyn 2016-12-03 23:19:43 -05:00
parent 4eafaab816
commit c75efedc6e
2 changed files with 104 additions and 80 deletions

View file

@ -9061,22 +9061,6 @@ basic_json_parser_66:
} }
/*! /*!
@brief parse string into a built-in arithmetic type as if @brief parse string into a built-in arithmetic type as if
the current locale is POSIX. the current locale is POSIX.
@ -9086,6 +9070,9 @@ basic_json_parser_66:
throw if can't parse the entire string as a number, throw if can't parse the entire string as a number,
or if the destination type is integral and the value or if the destination type is integral and the value
is outside of the type's range. is outside of the type's range.
note: in floating-point case strtod may parse
past the token's end - this is not an error.
*/ */
struct strtonum struct strtonum
{ {
@ -9101,7 +9088,8 @@ basic_json_parser_66:
{ {
T val{0}; T val{0};
if(parse(val, std::is_integral<T>())) { if (parse(val, std::is_integral<T>()))
{
return val; return val;
} }
@ -9113,24 +9101,35 @@ basic_json_parser_66:
+ typeid(T).name()); + typeid(T).name());
} }
/// return true iff token matches ^[+-]\d+$ // return true iff token matches ^[+-]\d+$
//
// this is a helper to determine whether to
// parse the token into floating-point or
// integral type. We wouldn't need it if
// we had separate token types for interal
// and floating-point cases.
bool is_integral() const bool is_integral() const
{ {
const char* p = m_start; const char* p = m_start;
if(!p) { if (!p)
{
return false; return false;
} }
if(*p == '-' or *p == '+') { if (*p == '-' or *p == '+')
{
++p; ++p;
} }
if(p == m_end) { if (p == m_end)
{
return false; return false;
} }
while(p < m_end and *p >= '0' and *p <= '9') { while (p < m_end and *p >= '0'
and *p <= '9')
{
++p; ++p;
} }
@ -9141,6 +9140,9 @@ basic_json_parser_66:
const char* const m_start = nullptr; const char* const m_start = nullptr;
const char* const m_end = nullptr; const char* const m_end = nullptr;
// overloaded wrappers for strtod/strtof/strtold
// that will be called from parse<floating_point_t>
static void strtof(float& f, static void strtof(float& f,
const char* str, const char* str,
char** endptr) char** endptr)
@ -9173,20 +9175,22 @@ basic_json_parser_66:
std::locale()).decimal_point(); std::locale()).decimal_point();
// replace decimal separator with locale-specific // replace decimal separator with locale-specific
// version, if necessary; data will be repointed // version, when necessary; data will be repointed
// to either buf or tempstr containing the fixed // to either buf or tempstr containing the fixed
// string. // string.
std::string tempstr; std::string tempstr;
std::array<char, 64> buf; std::array<char, 64> buf;
do { do {
if(decimal_point_char == '.') { if (decimal_point_char == '.')
{
break; // don't need to convert break; // don't need to convert
} }
const size_t ds_pos = static_cast<size_t>( const size_t ds_pos = static_cast<size_t>(
std::find(m_start, m_end, '.') - m_start ); std::find(m_start, m_end, '.') - m_start );
if(ds_pos == len) { if (ds_pos == len)
{
break; // no decimal separator break; // no decimal separator
} }
@ -9194,12 +9198,15 @@ basic_json_parser_66:
// tempstr, if buffer is too small; // tempstr, if buffer is too small;
// replace decimal separator, and update // replace decimal separator, and update
// data to point to the modified bytes // data to point to the modified bytes
if(len + 1 < buf.size()) { if (len + 1 < buf.size())
{
std::copy(m_start, m_end, buf.data()); std::copy(m_start, m_end, buf.data());
buf[len] = 0; buf[len] = 0;
buf[ds_pos] = decimal_point_char; buf[ds_pos] = decimal_point_char;
data = buf.data(); data = buf.data();
} else { }
else
{
tempstr.assign(m_start, m_end); tempstr.assign(m_start, m_end);
tempstr[ds_pos] = decimal_point_char; tempstr[ds_pos] = decimal_point_char;
data = tempstr.c_str(); data = tempstr.c_str();
@ -9210,15 +9217,14 @@ basic_json_parser_66:
value = 0; value = 0;
strtof(value, data, &endptr); strtof(value, data, &endptr);
// note that reading past the end is OK, the data may be, // note that reading past the end is OK, the data may be,
// for example, "123.", where the parsed token only contains "123", // for example, "123.", where the parsed token only
// but strtod will read the dot as well. // contains "123", but strtod will read the dot as well.
const bool ok = endptr >= data + len const bool ok = endptr >= data + len
and len > 0; and len > 0;
if(ok and value == 0.0 and *data == '-') { if (ok and value == 0.0 and *data == '-')
{
// some implementations forget to negate the zero // some implementations forget to negate the zero
value = -0.0; value = -0.0;
} }
@ -9232,15 +9238,17 @@ basic_json_parser_66:
const char* beg = m_start; const char* beg = m_start;
const char* const end = m_end; const char* const end = m_end;
if(beg == end) { if (beg == end)
{
return false; return false;
} }
const bool is_negative = (*beg == '-'); const bool is_negative = (*beg == '-');
// json numbers can't start with '+' but // json numbers can't start with '+' but
// this code is not json-specific // this code is not strictly json-specific
if(is_negative or *beg == '+') { if (is_negative or *beg == '+')
{
++beg; // skip the leading sign ++beg; // skip the leading sign
} }
@ -9255,7 +9263,8 @@ basic_json_parser_66:
and ( T(-1) < 0 // type must be signed and ( T(-1) < 0 // type must be signed
or !is_negative); // if value is negative or !is_negative); // if value is negative
while(beg < end and valid) { while (beg < end and valid)
{
const uint8_t c = static_cast<uint8_t>(*beg - '0'); const uint8_t c = static_cast<uint8_t>(*beg - '0');
const unsigned_T upd_x = x * 10 + c; const unsigned_T upd_x = x * 10 + c;
@ -9310,7 +9319,8 @@ basic_json_parser_66:
const bool is_negative = *m_start == '-'; const bool is_negative = *m_start == '-';
try { try {
if(not num.is_integral()) { if (not num.is_integral())
{
; ;
} }
else if (is_negative) else if (is_negative)
@ -9325,7 +9335,9 @@ basic_json_parser_66:
result.m_value = static_cast<number_unsigned_t>(num); result.m_value = static_cast<number_unsigned_t>(num);
return; return;
} }
} catch (std::invalid_argument&) { }
catch (std::invalid_argument&)
{
; // overflow - will parse as double ; // overflow - will parse as double
} }

View file

@ -8210,22 +8210,6 @@ class basic_json
} }
/*! /*!
@brief parse string into a built-in arithmetic type as if @brief parse string into a built-in arithmetic type as if
the current locale is POSIX. the current locale is POSIX.
@ -8235,6 +8219,9 @@ class basic_json
throw if can't parse the entire string as a number, throw if can't parse the entire string as a number,
or if the destination type is integral and the value or if the destination type is integral and the value
is outside of the type's range. is outside of the type's range.
note: in floating-point case strtod may parse
past the token's end - this is not an error.
*/ */
struct strtonum struct strtonum
{ {
@ -8250,7 +8237,8 @@ class basic_json
{ {
T val{0}; T val{0};
if(parse(val, std::is_integral<T>())) { if (parse(val, std::is_integral<T>()))
{
return val; return val;
} }
@ -8262,24 +8250,35 @@ class basic_json
+ typeid(T).name()); + typeid(T).name());
} }
/// return true iff token matches ^[+-]\d+$ // return true iff token matches ^[+-]\d+$
//
// this is a helper to determine whether to
// parse the token into floating-point or
// integral type. We wouldn't need it if
// we had separate token types for interal
// and floating-point cases.
bool is_integral() const bool is_integral() const
{ {
const char* p = m_start; const char* p = m_start;
if(!p) { if (!p)
{
return false; return false;
} }
if(*p == '-' or *p == '+') { if (*p == '-' or *p == '+')
{
++p; ++p;
} }
if(p == m_end) { if (p == m_end)
{
return false; return false;
} }
while(p < m_end and *p >= '0' and *p <= '9') { while (p < m_end and *p >= '0'
and *p <= '9')
{
++p; ++p;
} }
@ -8290,6 +8289,9 @@ class basic_json
const char* const m_start = nullptr; const char* const m_start = nullptr;
const char* const m_end = nullptr; const char* const m_end = nullptr;
// overloaded wrappers for strtod/strtof/strtold
// that will be called from parse<floating_point_t>
static void strtof(float& f, static void strtof(float& f,
const char* str, const char* str,
char** endptr) char** endptr)
@ -8322,20 +8324,22 @@ class basic_json
std::locale()).decimal_point(); std::locale()).decimal_point();
// replace decimal separator with locale-specific // replace decimal separator with locale-specific
// version, if necessary; data will be repointed // version, when necessary; data will be repointed
// to either buf or tempstr containing the fixed // to either buf or tempstr containing the fixed
// string. // string.
std::string tempstr; std::string tempstr;
std::array<char, 64> buf; std::array<char, 64> buf;
do { do {
if(decimal_point_char == '.') { if (decimal_point_char == '.')
{
break; // don't need to convert break; // don't need to convert
} }
const size_t ds_pos = static_cast<size_t>( const size_t ds_pos = static_cast<size_t>(
std::find(m_start, m_end, '.') - m_start ); std::find(m_start, m_end, '.') - m_start );
if(ds_pos == len) { if (ds_pos == len)
{
break; // no decimal separator break; // no decimal separator
} }
@ -8343,12 +8347,15 @@ class basic_json
// tempstr, if buffer is too small; // tempstr, if buffer is too small;
// replace decimal separator, and update // replace decimal separator, and update
// data to point to the modified bytes // data to point to the modified bytes
if(len + 1 < buf.size()) { if (len + 1 < buf.size())
{
std::copy(m_start, m_end, buf.data()); std::copy(m_start, m_end, buf.data());
buf[len] = 0; buf[len] = 0;
buf[ds_pos] = decimal_point_char; buf[ds_pos] = decimal_point_char;
data = buf.data(); data = buf.data();
} else { }
else
{
tempstr.assign(m_start, m_end); tempstr.assign(m_start, m_end);
tempstr[ds_pos] = decimal_point_char; tempstr[ds_pos] = decimal_point_char;
data = tempstr.c_str(); data = tempstr.c_str();
@ -8359,15 +8366,14 @@ class basic_json
value = 0; value = 0;
strtof(value, data, &endptr); strtof(value, data, &endptr);
// note that reading past the end is OK, the data may be, // note that reading past the end is OK, the data may be,
// for example, "123.", where the parsed token only contains "123", // for example, "123.", where the parsed token only
// but strtod will read the dot as well. // contains "123", but strtod will read the dot as well.
const bool ok = endptr >= data + len const bool ok = endptr >= data + len
and len > 0; and len > 0;
if(ok and value == 0.0 and *data == '-') { if (ok and value == 0.0 and *data == '-')
{
// some implementations forget to negate the zero // some implementations forget to negate the zero
value = -0.0; value = -0.0;
} }
@ -8381,15 +8387,17 @@ class basic_json
const char* beg = m_start; const char* beg = m_start;
const char* const end = m_end; const char* const end = m_end;
if(beg == end) { if (beg == end)
{
return false; return false;
} }
const bool is_negative = (*beg == '-'); const bool is_negative = (*beg == '-');
// json numbers can't start with '+' but // json numbers can't start with '+' but
// this code is not json-specific // this code is not strictly json-specific
if(is_negative or *beg == '+') { if (is_negative or *beg == '+')
{
++beg; // skip the leading sign ++beg; // skip the leading sign
} }
@ -8404,7 +8412,8 @@ class basic_json
and ( T(-1) < 0 // type must be signed and ( T(-1) < 0 // type must be signed
or !is_negative); // if value is negative or !is_negative); // if value is negative
while(beg < end and valid) { while (beg < end and valid)
{
const uint8_t c = static_cast<uint8_t>(*beg - '0'); const uint8_t c = static_cast<uint8_t>(*beg - '0');
const unsigned_T upd_x = x * 10 + c; const unsigned_T upd_x = x * 10 + c;
@ -8459,7 +8468,8 @@ class basic_json
const bool is_negative = *m_start == '-'; const bool is_negative = *m_start == '-';
try { try {
if(not num.is_integral()) { if (not num.is_integral())
{
; ;
} }
else if (is_negative) else if (is_negative)
@ -8474,7 +8484,9 @@ class basic_json
result.m_value = static_cast<number_unsigned_t>(num); result.m_value = static_cast<number_unsigned_t>(num);
return; return;
} }
} catch (std::invalid_argument&) { }
catch (std::invalid_argument&)
{
; // overflow - will parse as double ; // overflow - will parse as double
} }