check feature macros in once place

This commit is contained in:
Anders Dalvander 2024-11-20 11:14:41 +01:00
parent 0bbba960f4
commit 48252a6483
3 changed files with 18 additions and 25 deletions

View File

@ -283,7 +283,7 @@ template <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 {
chars_format const fmt = options.format; chars_format const fmt = detail::adjust_for_feature_macros(options.format);
UC const decimal_point = options.decimal_point; UC const decimal_point = options.decimal_point;
parsed_number_string_t<UC> answer; parsed_number_string_t<UC> answer;
@ -291,15 +291,10 @@ parse_number_string(UC const *p, UC const *pend,
answer.too_many_digits = false; answer.too_many_digits = false;
// assume p < pend, so dereference without checks; // assume p < pend, so dereference without checks;
answer.negative = (*p == UC('-')); answer.negative = (*p == UC('-'));
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
if ((*p == UC('-')) ||
(!uint64_t(fmt & detail::basic_json_fmt) && *p == UC('+'))) {
#else
// 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) &&
!uint64_t(fmt & detail::basic_json_fmt) && *p == UC('+'))) { !uint64_t(fmt & detail::basic_json_fmt) && *p == UC('+'))) {
#endif
++p; ++p;
if (p == pend) { if (p == pend) {
return report_parse_error<UC>( return report_parse_error<UC>(
@ -479,7 +474,7 @@ 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> options) { parse_options_t<UC> options) {
chars_format const fmt = options.format; chars_format const fmt = detail::adjust_for_feature_macros(options.format);
int const base = options.base; int const base = options.base;
from_chars_result_t<UC> answer; from_chars_result_t<UC> answer;
@ -492,12 +487,8 @@ parse_int_string(UC const *p, UC const *pend, T &value,
answer.ptr = first; answer.ptr = first;
return answer; return answer;
} }
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
if ((*p == UC('-')) || (*p == UC('+'))) {
#else
if ((*p == UC('-')) || if ((*p == UC('-')) ||
(uint64_t(fmt & chars_format::allow_leading_plus) && (*p == UC('+')))) { (uint64_t(fmt & chars_format::allow_leading_plus) && (*p == UC('+')))) {
#endif
++p; ++p;
} }

View File

@ -841,6 +841,20 @@ operator^=(chars_format &lhs, chars_format rhs) noexcept {
return lhs = (lhs ^ rhs); return lhs = (lhs ^ rhs);
} }
namespace detail {
// adjust for deprecated feature macros
constexpr chars_format adjust_for_feature_macros(chars_format fmt) {
return fmt
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS
| chars_format::allow_leading_plus
#endif
#ifdef FASTFLOAT_SKIP_WHITE_SPACE
| chars_format::skip_white_space
#endif
;
}
} // namespace detail
} // namespace fast_float } // namespace fast_float
#endif #endif

View File

@ -27,14 +27,10 @@ from_chars_result_t<UC>
answer.ec = std::errc(); // be optimistic answer.ec = std::errc(); // be optimistic
// assume first < last, so dereference without checks; // assume first < last, so dereference without checks;
bool const minusSign = (*first == UC('-')); bool const minusSign = (*first == UC('-'));
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
if ((*first == UC('-')) || (*first == UC('+'))) {
#else
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here // C++17 20.19.3.(7.1) explicitly forbids '+' sign here
if ((*first == UC('-')) || if ((*first == UC('-')) ||
(uint64_t(fmt & chars_format::allow_leading_plus) && (uint64_t(fmt & chars_format::allow_leading_plus) &&
(*first == UC('+')))) { (*first == UC('+')))) {
#endif
++first; ++first;
} }
if (last - first >= 3) { if (last - first >= 3) {
@ -294,18 +290,14 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value,
static_assert(is_supported_char_type<UC>(), static_assert(is_supported_char_type<UC>(),
"only char, wchar_t, char16_t and char32_t are supported"); "only char, wchar_t, char16_t and char32_t are supported");
chars_format const fmt = options.format; chars_format const fmt = detail::adjust_for_feature_macros(options.format);
from_chars_result_t<UC> answer; from_chars_result_t<UC> answer;
#ifndef FASTFLOAT_SKIP_WHITE_SPACE // disabled by default
if (uint64_t(fmt & chars_format::skip_white_space)) { if (uint64_t(fmt & chars_format::skip_white_space)) {
#endif
while ((first != last) && fast_float::is_space(uint8_t(*first))) { while ((first != last) && fast_float::is_space(uint8_t(*first))) {
first++; first++;
} }
#ifndef FASTFLOAT_SKIP_WHITE_SPACE // disabled by default
} }
#endif
if (first == last) { if (first == last) {
answer.ec = std::errc::invalid_argument; answer.ec = std::errc::invalid_argument;
answer.ptr = first; answer.ptr = first;
@ -349,19 +341,15 @@ from_chars_int_advanced(UC const *first, UC const *last, T &value,
static_assert(is_supported_char_type<UC>(), static_assert(is_supported_char_type<UC>(),
"only char, wchar_t, char16_t and char32_t are supported"); "only char, wchar_t, char16_t and char32_t are supported");
chars_format const fmt = options.format; chars_format const fmt = detail::adjust_for_feature_macros(options.format);
int const base = options.base; int const base = options.base;
from_chars_result_t<UC> answer; from_chars_result_t<UC> answer;
#ifndef FASTFLOAT_SKIP_WHITE_SPACE // disabled by default
if (uint64_t(fmt & chars_format::skip_white_space)) { if (uint64_t(fmt & chars_format::skip_white_space)) {
#endif
while ((first != last) && fast_float::is_space(uint8_t(*first))) { while ((first != last) && fast_float::is_space(uint8_t(*first))) {
first++; first++;
} }
#ifndef FASTFLOAT_SKIP_WHITE_SPACE // disabled by default
} }
#endif
if (first == last || base < 2 || base > 36) { if (first == last || base < 2 || base > 36) {
answer.ec = std::errc::invalid_argument; answer.ec = std::errc::invalid_argument;
answer.ptr = first; answer.ptr = first;