mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-06 16:56:57 +08:00
reduce size of from_chars_result_t to 4 bytes. Cleanup for usage FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN.
This commit is contained in:
parent
a591ca2fb8
commit
a081ebe6ce
@ -292,7 +292,7 @@ report_parse_error(UC const *p, parse_error error) noexcept {
|
|||||||
template <bool basic_json_fmt, typename UC>
|
template <bool basic_json_fmt, typename UC>
|
||||||
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t<UC>
|
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t<UC>
|
||||||
parse_number_string(UC const *p, UC const *pend,
|
parse_number_string(UC const *p, UC const *pend,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
// Cyclomatic complexity https://en.wikipedia.org/wiki/Cyclomatic_complexity
|
// Cyclomatic complexity https://en.wikipedia.org/wiki/Cyclomatic_complexity
|
||||||
// Consider refactoring the 'parse_number_string' function.
|
// Consider refactoring the 'parse_number_string' function.
|
||||||
// FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN fix this.
|
// FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN fix this.
|
||||||
@ -489,7 +489,7 @@ parse_number_string(UC const *p, UC const *pend,
|
|||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
||||||
parse_int_string(UC const *p, UC const *pend, T &value,
|
parse_int_string(UC const *p, UC const *pend, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
|
|
||||||
from_chars_result_t<UC> answer;
|
from_chars_result_t<UC> answer;
|
||||||
|
|
||||||
@ -582,7 +582,9 @@ parse_int_string(UC const *p, UC const *pend, T &value,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
#ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
||||||
|
value = T(i);
|
||||||
|
#else
|
||||||
if (negative) {
|
if (negative) {
|
||||||
#ifdef FASTFLOAT_VISUAL_STUDIO
|
#ifdef FASTFLOAT_VISUAL_STUDIO
|
||||||
#pragma warning(push)
|
#pragma warning(push)
|
||||||
@ -600,9 +602,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
|
|||||||
#pragma warning(pop)
|
#pragma warning(pop)
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
#endif
|
|
||||||
value = T(i);
|
value = T(i);
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@ -33,29 +33,29 @@
|
|||||||
|
|
||||||
namespace fast_float {
|
namespace fast_float {
|
||||||
|
|
||||||
enum class chars_format : uint64_t;
|
enum class chars_format : uint8_t;
|
||||||
|
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
||||||
namespace detail {
|
namespace detail {
|
||||||
constexpr chars_format basic_json_fmt = chars_format(1 << 5);
|
constexpr chars_format basic_json_fmt = chars_format(1 << 4);
|
||||||
constexpr chars_format basic_fortran_fmt = chars_format(1 << 6);
|
constexpr chars_format basic_fortran_fmt = chars_format(1 << 5);
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum class chars_format : uint64_t {
|
enum class chars_format : uint8_t {
|
||||||
scientific = 1 << 0,
|
scientific = 1 << 0,
|
||||||
fixed = 1 << 2,
|
fixed = 1 << 1,
|
||||||
general = fixed | scientific,
|
general = fixed | scientific,
|
||||||
hex = 1 << 3,
|
hex = 1 << 2,
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
||||||
no_infnan = 1 << 4,
|
no_infnan = 1 << 3,
|
||||||
// RFC 8259: https://datatracker.ietf.org/doc/html/rfc8259#section-6
|
// RFC 8259: https://datatracker.ietf.org/doc/html/rfc8259#section-6
|
||||||
json = uint64_t(detail::basic_json_fmt) | general | no_infnan,
|
json = uint64_t(detail::basic_json_fmt) | general | no_infnan,
|
||||||
// Extension of RFC 8259 where, e.g., "inf" and "nan" are allowed.
|
// Extension of RFC 8259 where, e.g., "inf" and "nan" are allowed.
|
||||||
json_or_infnan = uint64_t(detail::basic_json_fmt) | general,
|
json_or_infnan = uint64_t(detail::basic_json_fmt) | general,
|
||||||
fortran = uint64_t(detail::basic_fortran_fmt) | general,
|
fortran = uint64_t(detail::basic_fortran_fmt) | general,
|
||||||
allow_leading_plus = 1 << 7,
|
allow_leading_plus = 1 << 6,
|
||||||
skip_white_space = 1 << 8,
|
skip_white_space = 1 << 7,
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -70,14 +70,18 @@ template <typename UC> struct parse_options_t {
|
|||||||
FASTFLOAT_CONSTEXPR20 explicit parse_options_t(
|
FASTFLOAT_CONSTEXPR20 explicit parse_options_t(
|
||||||
chars_format fmt = chars_format::general, UC dot = UC('.'),
|
chars_format fmt = chars_format::general, UC dot = UC('.'),
|
||||||
int const b = 10) noexcept
|
int const b = 10) noexcept
|
||||||
: format(fmt), decimal_point(dot), base(b) {}
|
: format(fmt), decimal_point(dot), base(uint8_t(b)) {
|
||||||
|
#ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
||||||
|
assert(b >= 2 && b <= 36);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/** Which number formats are accepted */
|
/** Which number formats are accepted */
|
||||||
chars_format format;
|
chars_format format;
|
||||||
/** The character used as decimal point */
|
/** The character used as decimal point */
|
||||||
UC decimal_point;
|
UC decimal_point;
|
||||||
/** The base used for integers */
|
/** The base used for integers */
|
||||||
uint32_t base; /* only allowed from 2 to 36 */
|
uint8_t base; /* only allowed from 2 to 36 */
|
||||||
FASTFLOAT_ASSUME(base >= 2 && base <= 36);
|
FASTFLOAT_ASSUME(base >= 2 && base <= 36);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -146,7 +146,7 @@ template <typename T> struct from_chars_caller {
|
|||||||
template <typename UC>
|
template <typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
||||||
call(UC const *first, UC const *last, T &value,
|
call(UC const *first, UC const *last, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
return from_chars_advanced(first, last, value, options);
|
return from_chars_advanced(first, last, value, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -156,7 +156,7 @@ template <> struct from_chars_caller<std::float32_t> {
|
|||||||
template <typename UC>
|
template <typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
||||||
call(UC const *first, UC const *last, std::float32_t &value,
|
call(UC const *first, UC const *last, std::float32_t &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
// if std::float32_t is defined, and we are in C++23 mode; macro set for
|
// if std::float32_t is defined, and we are in C++23 mode; macro set for
|
||||||
// float32; set value to float due to equivalence between float and
|
// float32; set value to float due to equivalence between float and
|
||||||
// float32_t
|
// float32_t
|
||||||
@ -173,7 +173,7 @@ template <> struct from_chars_caller<std::float64_t> {
|
|||||||
template <typename UC>
|
template <typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
||||||
call(UC const *first, UC const *last, std::float64_t &value,
|
call(UC const *first, UC const *last, std::float64_t &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
// if std::float64_t is defined, and we are in C++23 mode; macro set for
|
// if std::float64_t is defined, and we are in C++23 mode; macro set for
|
||||||
// float64; set value as double due to equivalence between double and
|
// float64; set value as double due to equivalence between double and
|
||||||
// float64_t
|
// float64_t
|
||||||
@ -301,7 +301,7 @@ from_chars_advanced(parsed_number_string_t<UC> const &pns, T &value) noexcept {
|
|||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
||||||
from_chars_float_advanced(UC const *first, UC const *last, T &value,
|
from_chars_float_advanced(UC const *first, UC const *last, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
|
|
||||||
static_assert(is_supported_float_type<T>::value,
|
static_assert(is_supported_float_type<T>::value,
|
||||||
"only some floating-point types are supported");
|
"only some floating-point types are supported");
|
||||||
@ -365,7 +365,7 @@ from_chars(UC const *first, UC const *last, T &value, int const base) noexcept {
|
|||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
||||||
from_chars_int_advanced(UC const *first, UC const *last, T &value,
|
from_chars_int_advanced(UC const *first, UC const *last, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
|
|
||||||
static_assert(is_supported_integer_type<T>::value,
|
static_assert(is_supported_integer_type<T>::value,
|
||||||
"only integer types are supported");
|
"only integer types are supported");
|
||||||
@ -378,20 +378,16 @@ from_chars_int_advanced(UC const *first, UC const *last, T &value,
|
|||||||
first++;
|
first++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
if (first == last || options.base < 2 || options.base > 36) {
|
||||||
// We are in parser code with external loop that checks bounds.
|
|
||||||
FASTFLOAT_ASSUME(first < last);
|
|
||||||
#endif
|
|
||||||
if (
|
|
||||||
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
|
|
||||||
first == last ||
|
|
||||||
#endif
|
|
||||||
options.base < 2 || options.base > 36) {
|
|
||||||
from_chars_result_t<UC> answer;
|
from_chars_result_t<UC> answer;
|
||||||
answer.ec = std::errc::invalid_argument;
|
answer.ec = std::errc::invalid_argument;
|
||||||
answer.ptr = first;
|
answer.ptr = first;
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// We are in parser code with external loop that checks bounds.
|
||||||
|
FASTFLOAT_ASSUME(first < last);
|
||||||
|
#endif
|
||||||
|
|
||||||
return parse_int_string(first, last, value, options);
|
return parse_int_string(first, last, value, options);
|
||||||
}
|
}
|
||||||
@ -404,7 +400,7 @@ template <> struct from_chars_advanced_caller<1> {
|
|||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
||||||
call(UC const *first, UC const *last, T &value,
|
call(UC const *first, UC const *last, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
return from_chars_float_advanced(first, last, value, options);
|
return from_chars_float_advanced(first, last, value, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -413,7 +409,7 @@ template <> struct from_chars_advanced_caller<2> {
|
|||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 static from_chars_result_t<UC>
|
||||||
call(UC const *first, UC const *last, T &value,
|
call(UC const *first, UC const *last, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
return from_chars_int_advanced(first, last, value, options);
|
return from_chars_int_advanced(first, last, value, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -421,7 +417,7 @@ template <> struct from_chars_advanced_caller<2> {
|
|||||||
template <typename T, typename UC>
|
template <typename T, typename UC>
|
||||||
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
|
||||||
from_chars_advanced(UC const *first, UC const *last, T &value,
|
from_chars_advanced(UC const *first, UC const *last, T &value,
|
||||||
parse_options_t<UC> const &options) noexcept {
|
parse_options_t<UC> const options) noexcept {
|
||||||
return from_chars_advanced_caller<
|
return from_chars_advanced_caller<
|
||||||
size_t(is_supported_float_type<T>::value) +
|
size_t(is_supported_float_type<T>::value) +
|
||||||
2 * size_t(is_supported_integer_type<T>::value)>::call(first, last, value,
|
2 * size_t(is_supported_integer_type<T>::value)>::call(first, last, value,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user