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

Function stringToDouble

testlib.h:3527–3572  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3525}
3526
3527static inline double stringToDouble(InStream &in, const char *buffer) {
3528 double result;
3529
3530 size_t length = strlen(buffer);
3531
3532 int minusCount = 0;
3533 int plusCount = 0;
3534 int decimalPointCount = 0;
3535 int digitCount = 0;
3536 int eCount = 0;
3537
3538 for (size_t i = 0; i < length; i++) {
3539 if (('0' <= buffer[i] && buffer[i] <= '9') || buffer[i] == '.'
3540 || buffer[i] == 'e' || buffer[i] == 'E'
3541 || buffer[i] == '-' || buffer[i] == '+') {
3542 if ('0' <= buffer[i] && buffer[i] <= '9')
3543 digitCount++;
3544 if (buffer[i] == 'e' || buffer[i] == 'E')
3545 eCount++;
3546 if (buffer[i] == '-')
3547 minusCount++;
3548 if (buffer[i] == '+')
3549 plusCount++;
3550 if (buffer[i] == '.')
3551 decimalPointCount++;
3552 } else
3553 in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
3554 }
3555
3556 // If for sure is not a number in standard notation or in e-notation.
3557 if (digitCount == 0 || minusCount > 2 || plusCount > 2 || decimalPointCount > 1 || eCount > 1)
3558 in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
3559
3560 char *suffix = new char[length + 1];
3561 std::memset(suffix, 0, length + 1);
3562 int scanned = std::sscanf(buffer, "%lf%s", &result, suffix);
3563 bool empty = strlen(suffix) == 0;
3564 delete[] suffix;
3565
3566 if (scanned == 1 || (scanned == 2 && empty)) {
3567 if (__testlib_isNaN(result))
3568 in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
3569 return result;
3570 } else
3571 in.quit(_pe, ("Expected double, but \"" + __testlib_part(buffer) + "\" found").c_str());
3572}
3573
3574static inline double stringToDouble(InStream &in, const std::string& buffer) {
3575 for (size_t i = 0; i < buffer.length(); i++)

Callers 1

readRealMethod · 0.70

Calls 3

__testlib_partFunction · 0.70
__testlib_isNaNFunction · 0.70
quitMethod · 0.45

Tested by

no test coverage detected