* Added separate config macros FASTFLOAT_ISNOT_CHECKED_BOUNDS.

This commit is contained in:
IRainman 2025-11-09 19:08:11 +03:00
parent c8e4d89fef
commit 497e65b03f
2 changed files with 19 additions and 15 deletions

View File

@ -380,15 +380,17 @@ int main() {
## You also can also use some additional options (currently only configure by macroses): ## You also can also use some additional options (currently only configure by macroses):
There is a really common use case in mathematical and other abstract syntax tree (AST) like parsers, There is a really common use case in mathematical and other abstract syntax tree (AST)-like parsers that already processes
that already process sign and all other symbols before any number by itself. In this case you can the sign and all other symbols before any number by itself. In this case you can use FastFloat to only parse positive numbers
use FastFloat for only parse positive numbers in all supported formats with macros in all supported formats with macros `FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN`, which significantly reduce the code size
`FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN`, that significantly reduce the code size and improve and improve performance. You also can use macros `FASTFLOAT_ISNOT_CHECKED_BOUNDS` if your code already checks bounds;
performance. Additionally you can use macros `FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED` if you it's very likely because all parsers need to check the first character by itself before parsing. Additionally, you can use
only uneed `FE_TONEAREST` rounding mode in the parsing: this option is also improve performance a bit. macros `FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED` if you only need `FE_TONEAREST` rounding mode in the parsing;
this option also improves performance a bit and reduces code size.
```C++ ```C++
#define FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #define FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
#define FASTFLOAT_ISNOT_CHECKED_BOUNDS
#define FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED #define FASTFLOAT_ONLY_ROUNDS_TO_NEAREST_SUPPORTED
#include "fast_float/fast_float.h" #include "fast_float/fast_float.h"
#include <iostream> #include <iostream>

View File

@ -336,14 +336,16 @@ from_chars_float_advanced(UC const *first, UC const *last, T &value,
++first; ++first;
} }
} }
#endif
#ifdef FASTFLOAT_ISNOT_CHECKED_BOUNDS
// We are in parser code with external loop that checks bounds.
FASTFLOAT_ASSUME(first < last);
#else
if (first == last) { if (first == last) {
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 #endif
parsed_number_string_t<UC> const pns = parsed_number_string_t<UC> const pns =
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
@ -498,19 +500,19 @@ from_chars_int_advanced(UC const *first, UC const *last, T &value,
static_assert(is_supported_char_type<UC>::value, static_assert(is_supported_char_type<UC>::value,
"only char, wchar_t, char16_t and char32_t are supported"); "only char, wchar_t, char16_t and char32_t are supported");
#ifdef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN
// We are in parser code with external loop that checks bounds.
FASTFLOAT_ASSUME(first < last);
// base is already checked in the parse_options_t constructor.
#else
if (chars_format_t(options.format & chars_format::skip_white_space)) { if (chars_format_t(options.format & chars_format::skip_white_space)) {
while ((first != last) && fast_float::is_space(*first)) { while ((first != last) && fast_float::is_space(*first)) {
++first; ++first;
} }
} }
#endif
#ifdef FASTFLOAT_ISNOT_CHECKED_BOUNDS
// We are in parser code with external loop that checks bounds.
FASTFLOAT_ASSUME(first < last);
#endif #endif
if ( if (
#ifndef FASTFLOAT_ONLY_POSITIVE_C_NUMBER_WO_INF_NAN #ifndef FASTFLOAT_ISNOT_CHECKED_BOUNDS
first == last || first == last ||
#endif #endif
options.base < 2 || options.base > 36) { options.base < 2 || options.base > 36) {