MCPcopy Create free account
hub / github.com/MikeMirzayanov/testlib / parseExponentialOptValue

Function parseExponentialOptValue

testlib.h:5455–5515  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5453long double optValueToLongDouble(const std::string &s);
5454
5455std::string parseExponentialOptValue(const std::string &s) {
5456 size_t pos = std::string::npos;
5457 for (size_t i = 0; i < s.length(); i++)
5458 if (s[i] == 'e' || s[i] == 'E') {
5459 if (pos != std::string::npos)
5460 __testlib_fail("Opts: expected typical exponential notation but '" + compress(s) + "' found");
5461 pos = i;
5462 }
5463 if (pos == std::string::npos)
5464 return s;
5465 std::string e = s.substr(pos + 1);
5466 if (!e.empty() && e[0] == '+')
5467 e = e.substr(1);
5468 if (e.empty())
5469 __testlib_fail("Opts: expected typical exponential notation but '" + compress(s) + "' found");
5470 if (e.length() > 20)
5471 __testlib_fail("Opts: expected typical exponential notation but '" + compress(s) + "' found");
5472 int ne = optValueToIntegral<int>(e, false);
5473 std::string num = s.substr(0, pos);
5474 if (num.length() > 20)
5475 __testlib_fail("Opts: expected typical exponential notation but '" + compress(s) + "' found");
5476 if (!num.empty() && num[0] == '+')
5477 num = num.substr(1);
5478 optValueToLongDouble(num);
5479 bool minus = false;
5480 if (num[0] == '-') {
5481 minus = true;
5482 num = num.substr(1);
5483 }
5484 for (int i = 0; i < +ne; i++) {
5485 size_t sep = num.find('.');
5486 if (sep == std::string::npos)
5487 num += '0';
5488 else {
5489 if (sep + 1 == num.length())
5490 num[sep] = '0';
5491 else
5492 std::swap(num[sep], num[sep + 1]);
5493 }
5494 }
5495 for (int i = 0; i < -ne; i++) {
5496 size_t sep = num.find('.');
5497 if (sep == std::string::npos)
5498 num.insert(num.begin() + int(num.length()) - 1, '.');
5499 else {
5500 if (sep == 0)
5501 num.insert(num.begin() + 1, '0');
5502 else
5503 std::swap(num[sep - 1], num[sep]);
5504 }
5505 }
5506 while (!num.empty() && num[0] == '0')
5507 num = num.substr(1);
5508 while (num.find('.') != std::string::npos && num.back() == '0')
5509 num = num.substr(0, num.length() - 1);
5510 if (!num.empty() && num.back() == '.')
5511 num = num.substr(0, num.length() - 1);
5512 if ((!num.empty() && num[0] == '.') || num.empty())

Callers 2

optValueToIntegralFunction · 0.70
optValueToLongDoubleFunction · 0.70

Calls 3

__testlib_failFunction · 0.70
compressFunction · 0.70
optValueToLongDoubleFunction · 0.70

Tested by

no test coverage detected