Converts a floating point string so that the exponent prefix is 'e', and the exponent value does not have leading zeros. The Microsoft runtime library likes to write things like "2.5E+010". Convert that to "2.5e+10". We don't care what happens to strings that are not floating point strings.
| 462 | // We don't care what happens to strings that are not floating point |
| 463 | // strings. |
| 464 | std::string NormalizeExponentInFloatString(std::string in) { |
| 465 | std::string result; |
| 466 | // Reserve one spot for the terminating null, even when the sscanf fails. |
| 467 | std::vector<char> prefix(in.size() + 1); |
| 468 | char e; |
| 469 | char plus_or_minus; |
| 470 | int exponent; // in base 10 |
| 471 | if ((4 == std::sscanf(in.c_str(), "%[-+.0123456789]%c%c%d", prefix.data(), &e, |
| 472 | &plus_or_minus, &exponent)) && |
| 473 | (e == 'e' || e == 'E') && |
| 474 | (plus_or_minus == '-' || plus_or_minus == '+')) { |
| 475 | // It looks like a floating point value with exponent. |
| 476 | std::stringstream out; |
| 477 | out << prefix.data() << 'e' << plus_or_minus << exponent; |
| 478 | result = out.str(); |
| 479 | } else { |
| 480 | result = in; |
| 481 | } |
| 482 | return result; |
| 483 | } |
| 484 | |
| 485 | TEST(NormalizeFloat, Sample) { |
| 486 | EXPECT_THAT(NormalizeExponentInFloatString(""), Eq("")); |