Merge pull request #277 from dalle/issue276-plus-minus-infnan

Issue276 plus/minus handling in parse_infnan
This commit is contained in:
Daniel Lemire 2024-11-19 10:39:49 -05:00 committed by GitHub
commit 8e5f76ce73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 12 deletions

View File

@ -8,3 +8,4 @@ Lénárd Szolnoki
Jan Pharago Jan Pharago
Maya Warrier Maya Warrier
Taha Khokhar Taha Khokhar
Anders Dalvander

View File

@ -476,7 +476,7 @@ parse_int_string(UC const *p, UC const *pend, T &value, int base) {
UC const *const first = p; UC const *const first = p;
bool negative = (*p == UC('-')); bool const negative = (*p == UC('-'));
if (!std::is_signed<T>::value && negative) { if (!std::is_signed<T>::value && negative) {
answer.ec = std::errc::invalid_argument; answer.ec = std::errc::invalid_argument;
answer.ptr = first; answer.ptr = first;

View File

@ -25,18 +25,16 @@ from_chars_result_t<UC> FASTFLOAT_CONSTEXPR14 parse_infnan(UC const *first,
from_chars_result_t<UC> answer{}; from_chars_result_t<UC> answer{};
answer.ptr = first; answer.ptr = first;
answer.ec = std::errc(); // be optimistic answer.ec = std::errc(); // be optimistic
bool minusSign = false; // assume first < last, so dereference without checks;
if (*first == bool const minusSign = (*first == UC('-'));
UC('-')) { // assume first < last, so dereference without checks;
// C++17 20.19.3.(7.1) explicitly forbids '+' here
minusSign = true;
++first;
}
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default #ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
if (*first == UC('+')) { if ((*first == UC('-')) || (*first == UC('+'))) {
#else
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here
if (*first == UC('-')) {
#endif
++first; ++first;
} }
#endif
if (last - first >= 3) { if (last - first >= 3) {
if (fastfloat_strncasecmp(first, str_const_nan<UC>(), 3)) { if (fastfloat_strncasecmp(first, str_const_nan<UC>(), 3)) {
answer.ptr = (first += 3); answer.ptr = (first += 3);
@ -93,7 +91,7 @@ fastfloat_really_inline bool rounds_to_nearest() noexcept {
// However, it is expected to be much faster than the fegetround() // However, it is expected to be much faster than the fegetround()
// function call. // function call.
// //
// The volatile keywoard prevents the compiler from computing the function // The volatile keyword prevents the compiler from computing the function
// at compile-time. // at compile-time.
// There might be other ways to prevent compile-time optimizations (e.g., // There might be other ways to prevent compile-time optimizations (e.g.,
// asm). The value does not need to be std::numeric_limits<float>::min(), any // asm). The value does not need to be std::numeric_limits<float>::min(), any

View File

@ -41,7 +41,10 @@ bool eddelbuettel() {
"-.1", "-.1",
"+.1", "+.1",
"1e+1", "1e+1",
"+1e1"}; "+1e1",
"-+0",
"-+inf",
"-+nan"};
std::vector<std::pair<bool, double>> expected_results = { std::vector<std::pair<bool, double>> expected_results = {
{true, std::numeric_limits<double>::infinity()}, {true, std::numeric_limits<double>::infinity()},
{true, 3.16227766016838}, {true, 3.16227766016838},
@ -75,6 +78,9 @@ bool eddelbuettel() {
{true, 0.1}, {true, 0.1},
{true, 10}, {true, 10},
{true, 10}, {true, 10},
{false, -1},
{false, -1},
{false, -1},
}; };
for (size_t i = 0; i < inputs.size(); i++) { for (size_t i = 0; i < inputs.size(); i++) {
const std::string &input = inputs[i]; const std::string &input = inputs[i];