Commit 29fd00d2 authored by Tom Jackson's avatar Tom Jackson Committed by Facebook Github Bot

double_conversion eats the 'e' in '123e'

Summary: Until I do a PR on `double_conversion`, we need to work around this by catching the trailing 'e' case.

Reviewed By: luciang

Differential Revision: D7481130

fbshipit-source-id: 2734dbe834e6cd69c6dfe41d6b4e9a7a548a7da8
parent ed0d101c
...@@ -365,6 +365,22 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept { ...@@ -365,6 +365,22 @@ Expected<Tgt, ConversionCode> str_to_floating(StringPiece* src) noexcept {
(result == 0.0 && std::isspace((*src)[size_t(length) - 1]))) { (result == 0.0 && std::isspace((*src)[size_t(length) - 1]))) {
return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING); return makeUnexpected(ConversionCode::EMPTY_INPUT_STRING);
} }
if (length >= 2) {
const char* suffix = src->data() + length - 1;
// double_conversion doesn't update length correctly when there is an
// incomplete exponent specifier. Converting "12e-f-g" shouldn't consume
// any more than "12", but it will consume "12e-".
// "123-" should only parse "123"
if (*suffix == '-' || *suffix == '+') {
--suffix;
--length;
}
// "12e-f-g" or "12euro" should only parse "12"
if (*suffix == 'e' || *suffix == 'E') {
--length;
}
}
src->advance(size_t(length)); src->advance(size_t(length));
return Tgt(result); return Tgt(result);
} }
......
...@@ -589,6 +589,16 @@ TEST(Conv, StringPieceToDouble) { ...@@ -589,6 +589,16 @@ TEST(Conv, StringPieceToDouble) {
make_tuple(" 0.0 zorro", " zorro", 0.0), make_tuple(" 0.0 zorro", " zorro", 0.0),
make_tuple(" 0.0 zorro ", " zorro ", 0.0), make_tuple(" 0.0 zorro ", " zorro ", 0.0),
make_tuple("0.0zorro", "zorro", 0.0), make_tuple("0.0zorro", "zorro", 0.0),
make_tuple("0.0eb", "eb", 0.0),
make_tuple("0.0EB", "EB", 0.0),
make_tuple("0eb", "eb", 0.0),
make_tuple("0EB", "EB", 0.0),
make_tuple("12e", "e", 12.0),
make_tuple("12e-", "e-", 12.0),
make_tuple("12e+", "e+", 12.0),
make_tuple("12e-f-g", "e-f-g", 12.0),
make_tuple("12e+f+g", "e+f+g", 12.0),
make_tuple("12euro", "euro", 12.0),
}; };
for (const auto& s : strs) { for (const auto& s : strs) {
StringPiece pc(get<0>(s)); StringPiece pc(get<0>(s));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment