mirror of
https://github.com/fastfloat/fast_float.git
synced 2025-12-06 16:56:57 +08:00
Merge pull request #308 from fastfloat/turing_json_into_macro_parameter
turning json option into macro parameter
This commit is contained in:
commit
e019768d49
@ -236,7 +236,7 @@ int main(int argc, char **argv) {
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
if(argc > 1) {
|
if (argc > 1) {
|
||||||
fileload(argv[1]);
|
fileload(argv[1]);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -279,7 +279,7 @@ report_parse_error(UC const *p, parse_error error) {
|
|||||||
|
|
||||||
// Assuming that you use no more than 19 digits, this will
|
// Assuming that you use no more than 19 digits, this will
|
||||||
// parse an ASCII string.
|
// parse an ASCII string.
|
||||||
template <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> options) noexcept {
|
parse_options_t<UC> options) noexcept {
|
||||||
@ -292,20 +292,20 @@ parse_number_string(UC const *p, UC const *pend,
|
|||||||
// assume p < pend, so dereference without checks;
|
// assume p < pend, so dereference without checks;
|
||||||
answer.negative = (*p == UC('-'));
|
answer.negative = (*p == UC('-'));
|
||||||
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here
|
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here
|
||||||
if ((*p == UC('-')) ||
|
if ((*p == UC('-')) || (uint64_t(fmt & chars_format::allow_leading_plus) &&
|
||||||
(uint64_t(fmt & chars_format::allow_leading_plus) &&
|
!basic_json_fmt && *p == UC('+'))) {
|
||||||
!uint64_t(fmt & detail::basic_json_fmt) && *p == UC('+'))) {
|
|
||||||
++p;
|
++p;
|
||||||
if (p == pend) {
|
if (p == pend) {
|
||||||
return report_parse_error<UC>(
|
return report_parse_error<UC>(
|
||||||
p, parse_error::missing_integer_or_dot_after_sign);
|
p, parse_error::missing_integer_or_dot_after_sign);
|
||||||
}
|
}
|
||||||
if (uint64_t(fmt & detail::basic_json_fmt)) {
|
FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
|
||||||
if (!is_integer(*p)) { // a sign must be followed by an integer
|
if (!is_integer(*p)) { // a sign must be followed by an integer
|
||||||
return report_parse_error<UC>(p,
|
return report_parse_error<UC>(p,
|
||||||
parse_error::missing_integer_after_sign);
|
parse_error::missing_integer_after_sign);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
if (!is_integer(*p) &&
|
if (!is_integer(*p) &&
|
||||||
(*p !=
|
(*p !=
|
||||||
decimal_point)) { // a sign must be followed by an integer or the dot
|
decimal_point)) { // a sign must be followed by an integer or the dot
|
||||||
@ -329,7 +329,7 @@ parse_number_string(UC const *p, UC const *pend,
|
|||||||
UC const *const end_of_integer_part = p;
|
UC const *const end_of_integer_part = p;
|
||||||
int64_t digit_count = int64_t(end_of_integer_part - start_digits);
|
int64_t digit_count = int64_t(end_of_integer_part - start_digits);
|
||||||
answer.integer = span<UC const>(start_digits, size_t(digit_count));
|
answer.integer = span<UC const>(start_digits, size_t(digit_count));
|
||||||
if (uint64_t(fmt & detail::basic_json_fmt)) {
|
FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
|
||||||
// at least 1 digit in integer part, without leading zeros
|
// at least 1 digit in integer part, without leading zeros
|
||||||
if (digit_count == 0) {
|
if (digit_count == 0) {
|
||||||
return report_parse_error<UC>(p, parse_error::no_digits_in_integer_part);
|
return report_parse_error<UC>(p, parse_error::no_digits_in_integer_part);
|
||||||
@ -358,14 +358,14 @@ parse_number_string(UC const *p, UC const *pend,
|
|||||||
answer.fraction = span<UC const>(before, size_t(p - before));
|
answer.fraction = span<UC const>(before, size_t(p - before));
|
||||||
digit_count -= exponent;
|
digit_count -= exponent;
|
||||||
}
|
}
|
||||||
if (uint64_t(fmt & detail::basic_json_fmt)) {
|
FASTFLOAT_IF_CONSTEXPR17(basic_json_fmt) {
|
||||||
// at least 1 digit in fractional part
|
// at least 1 digit in fractional part
|
||||||
if (has_decimal_point && exponent == 0) {
|
if (has_decimal_point && exponent == 0) {
|
||||||
return report_parse_error<UC>(p,
|
return report_parse_error<UC>(p,
|
||||||
parse_error::no_digits_in_fractional_part);
|
parse_error::no_digits_in_fractional_part);
|
||||||
}
|
}
|
||||||
} else if (digit_count ==
|
}
|
||||||
0) { // we must have encountered at least one integer!
|
else if (digit_count == 0) { // we must have encountered at least one integer!
|
||||||
return report_parse_error<UC>(p, parse_error::no_digits_in_mantissa);
|
return report_parse_error<UC>(p, parse_error::no_digits_in_mantissa);
|
||||||
}
|
}
|
||||||
int64_t exp_number = 0; // explicit exponential part
|
int64_t exp_number = 0; // explicit exponential part
|
||||||
|
|||||||
@ -27,6 +27,12 @@
|
|||||||
#define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 0
|
#define FASTFLOAT_HAS_IS_CONSTANT_EVALUATED 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606L
|
||||||
|
#define FASTFLOAT_IF_CONSTEXPR17(x) if constexpr (x)
|
||||||
|
#else
|
||||||
|
#define FASTFLOAT_IF_CONSTEXPR17(x) if (x)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Testing for relevant C++20 constexpr library features
|
// Testing for relevant C++20 constexpr library features
|
||||||
#if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED && FASTFLOAT_HAS_BIT_CAST && \
|
#if FASTFLOAT_HAS_IS_CONSTANT_EVALUATED && FASTFLOAT_HAS_BIT_CAST && \
|
||||||
defined(__cpp_lib_constexpr_algorithms) && \
|
defined(__cpp_lib_constexpr_algorithms) && \
|
||||||
|
|||||||
@ -305,7 +305,9 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value,
|
|||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
parsed_number_string_t<UC> pns =
|
parsed_number_string_t<UC> pns =
|
||||||
parse_number_string<UC>(first, last, options);
|
uint64_t(fmt & detail::basic_json_fmt)
|
||||||
|
? parse_number_string<true, UC>(first, last, options)
|
||||||
|
: parse_number_string<false, UC>(first, last, options);
|
||||||
if (!pns.valid) {
|
if (!pns.valid) {
|
||||||
if (uint64_t(fmt & chars_format::no_infnan)) {
|
if (uint64_t(fmt & chars_format::no_infnan)) {
|
||||||
answer.ec = std::errc::invalid_argument;
|
answer.ec = std::errc::invalid_argument;
|
||||||
|
|||||||
@ -131,7 +131,7 @@ int main() {
|
|||||||
for (std::size_t i = 0; i < reject.size(); ++i) {
|
for (std::size_t i = 0; i < reject.size(); ++i) {
|
||||||
auto const &f = reject[i].input;
|
auto const &f = reject[i].input;
|
||||||
auto const &expected_reason = reject[i].reason;
|
auto const &expected_reason = reject[i].reason;
|
||||||
auto answer = fast_float::parse_number_string(
|
auto answer = fast_float::parse_number_string<true>(
|
||||||
f.data(), f.data() + f.size(),
|
f.data(), f.data() + f.size(),
|
||||||
fast_float::parse_options(
|
fast_float::parse_options(
|
||||||
fast_float::chars_format::json |
|
fast_float::chars_format::json |
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user