Skip to content

Commit 7aa9b2c

Browse files
javachefacebook-github-bot
authored andcommitted
Use updated fast_float::parse_options
Summary: X-link: https://github.com/facebookincubator/zstrong/pull/1216 `str_to_floating_fast_float_from_chars` can be simplified using the new upstream option `allow_leading_plus`. It does mean that we also support parsing `+nan` and `+infinity` which we previously didn't. Mapping to `Inf` is also handled by the parsing logic, so this custom branch can be removed. Reviewed By: Gownta Differential Revision: D70392258 fbshipit-source-id: 62972a6cadd1547d8ff0a3162510ae5e979d459f
1 parent 7881d1e commit 7aa9b2c

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

build/fbcode_builder/manifests/fast_float

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
name = fast_float
33

44
[download]
5-
url = https://github.com/fastfloat/fast_float/archive/refs/tags/v6.1.4.tar.gz
6-
sha256 = 12cb6d250824160ca16bcb9d51f0ca7693d0d10cb444f34f1093bc02acfce704
5+
url = https://github.com/fastfloat/fast_float/archive/refs/tags/v8.0.0.tar.gz
6+
sha256 = f312f2dc34c61e665f4b132c0307d6f70ad9420185fa831911bc24408acf625d
77

88
[build]
99
builder = cmake
10-
subdir = fast_float-6.1.4
10+
subdir = fast_float-8.0.0
1111

1212
[cmake.defines]
1313
FASTFLOAT_TEST = OFF

folly/Conv.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,11 @@ Expected<Tgt, ConversionCode> str_to_floating_fast_float_from_chars(
359359
return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
360360
}
361361

362-
if (*b == '+') {
363-
// This function supports a leading + sign, but fast_float does not.
364-
b += 1;
365-
if (b == e || (!std::isdigit(*b) && *b != '.')) {
366-
return makeUnexpected(ConversionCode::STRING_TO_FLOAT_ERROR);
367-
}
368-
}
369-
370362
Tgt result;
371-
auto [ptr, ec] = fast_float::from_chars(b, e, result);
363+
fast_float::parse_options options{
364+
fast_float::chars_format::general |
365+
fast_float::chars_format::allow_leading_plus};
366+
auto [ptr, ec] = fast_float::from_chars_advanced(b, e, result, options);
372367
bool isOutOfRange{ec == std::errc::result_out_of_range};
373368
bool isOk{ec == std::errc()};
374369
if (!isOk && !isOutOfRange) {
@@ -377,15 +372,6 @@ Expected<Tgt, ConversionCode> str_to_floating_fast_float_from_chars(
377372

378373
auto numMatchedChars = ptr - src->data();
379374
src->advance(numMatchedChars);
380-
381-
if (isOutOfRange) {
382-
if (*b == '-') {
383-
return -std::numeric_limits<Tgt>::infinity();
384-
} else {
385-
return std::numeric_limits<Tgt>::infinity();
386-
}
387-
}
388-
389375
return result;
390376
}
391377

folly/test/ConvTest.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,33 +1703,44 @@ void tryStringToFloat(const StrToFloat<String>& strToFloat) {
17031703
}
17041704

17051705
// NaN
1706-
const std::array<String, 6> kNanInputs{{
1706+
const std::array<String, 9> kNanInputs{{
17071707
"nan",
17081708
"NaN",
17091709
"NAN",
17101710
"-nan",
17111711
"-NaN",
17121712
"-NAN",
1713+
"+nan",
1714+
"+NaN",
1715+
"+NAN",
17131716
}};
17141717
for (const auto& input : kNanInputs) {
17151718
auto rv = strToFloat(input);
17161719
EXPECT_TRUE(std::isnan(rv.value())) << input;
17171720
}
17181721

1719-
EXPECT_EQ(strToFloat("+nan").error(), ConversionCode::STRING_TO_FLOAT_ERROR);
1720-
1721-
const std::array<String, 6> kInfinityInputs{{
1722+
const std::array<String, 12> kInfinityInputs{{
17221723
"-inf",
17231724
"-INF",
17241725
"-iNf",
17251726
"-infinity",
17261727
"-INFINITY",
17271728
"-INFInITY",
1729+
"+inf",
1730+
"+INF",
1731+
"+iNf",
1732+
"+infinity",
1733+
"+INFINITY",
1734+
"+INFInITY",
17281735
}};
17291736
for (const auto& input : kInfinityInputs) {
17301737
{
17311738
auto rv = strToFloat(input);
1732-
EXPECT_EQ(rv.value(), -numeric_limits<float>::infinity()) << input;
1739+
if (input[0] == '-') {
1740+
EXPECT_EQ(rv.value(), -numeric_limits<float>::infinity()) << input;
1741+
} else {
1742+
EXPECT_EQ(rv.value(), numeric_limits<float>::infinity()) << input;
1743+
}
17331744
}
17341745

17351746
{
@@ -1739,10 +1750,6 @@ void tryStringToFloat(const StrToFloat<String>& strToFloat) {
17391750
}
17401751
}
17411752

1742-
EXPECT_EQ(
1743-
strToFloat("+infinity").error(), ConversionCode::STRING_TO_FLOAT_ERROR);
1744-
EXPECT_EQ(strToFloat("+inf").error(), ConversionCode::STRING_TO_FLOAT_ERROR);
1745-
17461753
const std::array<String, 15> kScientificNotation{{
17471754
"123.4560e0",
17481755
"+123.4560e0",

0 commit comments

Comments
 (0)