Read floating point number in simple format, imprecisely, from a non-0-terminated string.
| 98 | |
| 99 | /// Read floating point number in simple format, imprecisely, from a non-0-terminated string. |
| 100 | static double readFloatText(const char * buf, const char * end) |
| 101 | { |
| 102 | bool negative = false; |
| 103 | double x = 0; |
| 104 | bool after_point = false; |
| 105 | double power_of_ten = 1; |
| 106 | |
| 107 | if (buf == end) |
| 108 | throw JSONException("JSON: cannot parse floating point number: unexpected end of data."); |
| 109 | |
| 110 | bool run = true; |
| 111 | while (buf != end && run) |
| 112 | { |
| 113 | switch (*buf) |
| 114 | { |
| 115 | case '+': |
| 116 | break; |
| 117 | case '-': |
| 118 | negative = true; |
| 119 | break; |
| 120 | case '.': |
| 121 | after_point = true; |
| 122 | break; |
| 123 | case '0': |
| 124 | case '1': |
| 125 | case '2': |
| 126 | case '3': |
| 127 | case '4': |
| 128 | case '5': |
| 129 | case '6': |
| 130 | case '7': |
| 131 | case '8': |
| 132 | case '9': |
| 133 | if (after_point) |
| 134 | { |
| 135 | power_of_ten /= 10; |
| 136 | x += (*buf - '0') * power_of_ten; |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | x *= 10; |
| 141 | x += *buf - '0'; |
| 142 | } |
| 143 | break; |
| 144 | case 'e': |
| 145 | case 'E': |
| 146 | { |
| 147 | ++buf; |
| 148 | auto exponent = readIntText(buf, end); |
| 149 | x *= preciseExp10(static_cast<double>(exponent)); |
| 150 | |
| 151 | run = false; |
| 152 | break; |
| 153 | } |
| 154 | default: |
| 155 | run = false; |
| 156 | break; |
| 157 | } |
no test coverage detected