Skip to content

Commit 8e5f76c

Browse files
authored
Merge pull request #277 from dalle/issue276-plus-minus-infnan
Issue276 plus/minus handling in parse_infnan
2 parents e800cab + b635dec commit 8e5f76c

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Lénárd Szolnoki
88
Jan Pharago
99
Maya Warrier
1010
Taha Khokhar
11+
Anders Dalvander

include/fast_float/ascii_number.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ parse_int_string(UC const *p, UC const *pend, T &value, int base) {
476476

477477
UC const *const first = p;
478478

479-
bool negative = (*p == UC('-'));
479+
bool const negative = (*p == UC('-'));
480480
if (!std::is_signed<T>::value && negative) {
481481
answer.ec = std::errc::invalid_argument;
482482
answer.ptr = first;

include/fast_float/parse_number.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ from_chars_result_t<UC> FASTFLOAT_CONSTEXPR14 parse_infnan(UC const *first,
2525
from_chars_result_t<UC> answer{};
2626
answer.ptr = first;
2727
answer.ec = std::errc(); // be optimistic
28-
bool minusSign = false;
29-
if (*first ==
30-
UC('-')) { // assume first < last, so dereference without checks;
31-
// C++17 20.19.3.(7.1) explicitly forbids '+' here
32-
minusSign = true;
33-
++first;
34-
}
28+
// assume first < last, so dereference without checks;
29+
bool const minusSign = (*first == UC('-'));
3530
#ifdef FASTFLOAT_ALLOWS_LEADING_PLUS // disabled by default
36-
if (*first == UC('+')) {
31+
if ((*first == UC('-')) || (*first == UC('+'))) {
32+
#else
33+
// C++17 20.19.3.(7.1) explicitly forbids '+' sign here
34+
if (*first == UC('-')) {
35+
#endif
3736
++first;
3837
}
39-
#endif
4038
if (last - first >= 3) {
4139
if (fastfloat_strncasecmp(first, str_const_nan<UC>(), 3)) {
4240
answer.ptr = (first += 3);
@@ -93,7 +91,7 @@ fastfloat_really_inline bool rounds_to_nearest() noexcept {
9391
// However, it is expected to be much faster than the fegetround()
9492
// function call.
9593
//
96-
// The volatile keywoard prevents the compiler from computing the function
94+
// The volatile keyword prevents the compiler from computing the function
9795
// at compile-time.
9896
// There might be other ways to prevent compile-time optimizations (e.g.,
9997
// asm). The value does not need to be std::numeric_limits<float>::min(), any

tests/rcppfastfloat_test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ bool eddelbuettel() {
4141
"-.1",
4242
"+.1",
4343
"1e+1",
44-
"+1e1"};
44+
"+1e1",
45+
"-+0",
46+
"-+inf",
47+
"-+nan"};
4548
std::vector<std::pair<bool, double>> expected_results = {
4649
{true, std::numeric_limits<double>::infinity()},
4750
{true, 3.16227766016838},
@@ -75,6 +78,9 @@ bool eddelbuettel() {
7578
{true, 0.1},
7679
{true, 10},
7780
{true, 10},
81+
{false, -1},
82+
{false, -1},
83+
{false, -1},
7884
};
7985
for (size_t i = 0; i < inputs.size(); i++) {
8086
const std::string &input = inputs[i];

0 commit comments

Comments
 (0)