MCPcopy Create free account
hub / github.com/KhronosGroup/SPIRV-Tools / NormalizeExponentInFloatString

Function NormalizeExponentInFloatString

test/hex_float_test.cpp:464–483  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

462// We don't care what happens to strings that are not floating point
463// strings.
464std::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
485TEST(NormalizeFloat, Sample) {
486 EXPECT_THAT(NormalizeExponentInFloatString(""), Eq(""));

Callers 2

TESTFunction · 0.85
TEST_PFunction · 0.85

Calls 4

c_strMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected