Skip to content

Commit 13ba430

Browse files
committed
reducing the overhead of 'json' and 'allow plus' options
1 parent 1504792 commit 13ba430

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

include/fast_float/ascii_number.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ report_parse_error(UC const *p, parse_error error) {
279279

280280
// Assuming that you use no more than 19 digits, this will
281281
// parse an ASCII string.
282-
template <typename UC>
282+
template <bool allow_leading_plus, bool basic_json_fmt, typename UC>
283283
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 parsed_number_string_t<UC>
284284
parse_number_string(UC const *p, UC const *pend,
285285
parse_options_t<UC> options) noexcept {
@@ -293,14 +293,14 @@ parse_number_string(UC const *p, UC const *pend,
293293
answer.negative = (*p == UC('-'));
294294
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here
295295
if ((*p == UC('-')) ||
296-
(uint64_t(fmt & chars_format::allow_leading_plus) &&
297-
!uint64_t(fmt & detail::basic_json_fmt) && *p == UC('+'))) {
296+
(allow_leading_plus &&
297+
!basic_json_fmt && *p == UC('+'))) {
298298
++p;
299299
if (p == pend) {
300300
return report_parse_error<UC>(
301301
p, parse_error::missing_integer_or_dot_after_sign);
302302
}
303-
if (uint64_t(fmt & detail::basic_json_fmt)) {
303+
if (basic_json_fmt) {
304304
if (!is_integer(*p)) { // a sign must be followed by an integer
305305
return report_parse_error<UC>(p,
306306
parse_error::missing_integer_after_sign);
@@ -329,7 +329,7 @@ parse_number_string(UC const *p, UC const *pend,
329329
UC const *const end_of_integer_part = p;
330330
int64_t digit_count = int64_t(end_of_integer_part - start_digits);
331331
answer.integer = span<UC const>(start_digits, size_t(digit_count));
332-
if (uint64_t(fmt & detail::basic_json_fmt)) {
332+
if (basic_json_fmt) {
333333
// at least 1 digit in integer part, without leading zeros
334334
if (digit_count == 0) {
335335
return report_parse_error<UC>(p, parse_error::no_digits_in_integer_part);
@@ -358,7 +358,7 @@ parse_number_string(UC const *p, UC const *pend,
358358
answer.fraction = span<UC const>(before, size_t(p - before));
359359
digit_count -= exponent;
360360
}
361-
if (uint64_t(fmt & detail::basic_json_fmt)) {
361+
if (basic_json_fmt) {
362362
// at least 1 digit in fractional part
363363
if (has_decimal_point && exponent == 0) {
364364
return report_parse_error<UC>(p,
@@ -470,11 +470,11 @@ parse_number_string(UC const *p, UC const *pend,
470470
return answer;
471471
}
472472

473-
template <typename T, typename UC>
473+
template <bool allow_leading_plus = false, typename T, typename UC>
474474
fastfloat_really_inline FASTFLOAT_CONSTEXPR20 from_chars_result_t<UC>
475475
parse_int_string(UC const *p, UC const *pend, T &value,
476476
parse_options_t<UC> options) {
477-
chars_format const fmt = detail::adjust_for_feature_macros(options.format);
477+
//chars_format const fmt = detail::adjust_for_feature_macros(options.format);
478478
int const base = options.base;
479479

480480
from_chars_result_t<UC> answer;
@@ -495,7 +495,7 @@ parse_int_string(UC const *p, UC const *pend, T &value,
495495
return answer;
496496
}
497497
if ((*p == UC('-')) ||
498-
(uint64_t(fmt & chars_format::allow_leading_plus) && (*p == UC('+')))) {
498+
(allow_leading_plus && (*p == UC('+')))) {
499499
++p;
500500
}
501501

include/fast_float/parse_number.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,11 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value,
304304
answer.ptr = first;
305305
return answer;
306306
}
307-
parsed_number_string_t<UC> pns =
308-
parse_number_string<UC>(first, last, options);
307+
bool allow_leading_plus = uint64_t(fmt & chars_format::allow_leading_plus);
308+
bool basic_json_fmt = uint64_t(fmt & detail::basic_json_fmt);
309+
parsed_number_string_t<UC> pns = allow_leading_plus ?
310+
(basic_json_fmt ? parse_number_string<true, true, UC>(first, last, options): parse_number_string<true, false, UC>(first, last, options)) :
311+
(basic_json_fmt ? parse_number_string<false, true, UC>(first, last, options): parse_number_string<false, false, UC>(first, last, options));
309312
if (!pns.valid) {
310313
if (uint64_t(fmt & chars_format::no_infnan)) {
311314
answer.ec = std::errc::invalid_argument;

tests/json_fmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ int main() {
131131
for (std::size_t i = 0; i < reject.size(); ++i) {
132132
auto const &f = reject[i].input;
133133
auto const &expected_reason = reject[i].reason;
134-
auto answer = fast_float::parse_number_string(
134+
auto answer = fast_float::parse_number_string<true, true>(
135135
f.data(), f.data() + f.size(),
136136
fast_float::parse_options(
137137
fast_float::chars_format::json |

0 commit comments

Comments
 (0)