| 151 | } |
| 152 | |
| 153 | double |
| 154 | svtod(swoc::TextView text, swoc::TextView *parsed) { |
| 155 | // @return 10^e |
| 156 | auto pow10 = [](int e) -> double { |
| 157 | double zret = 1.0; |
| 158 | double scale = 10.0; |
| 159 | if (e < 0) { // flip the scale and make @a e positive. |
| 160 | e = -e; |
| 161 | scale = 0.1; |
| 162 | } |
| 163 | |
| 164 | // Walk the bits in the exponent @a e and multiply the scale for set bits. |
| 165 | while (e) { |
| 166 | if (e & 1) { |
| 167 | zret *= scale; |
| 168 | } |
| 169 | scale *= scale; |
| 170 | e >>= 1; |
| 171 | } |
| 172 | return zret; |
| 173 | }; |
| 174 | |
| 175 | if (text.empty()) { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | auto org_text = text; // save this to update @a parsed. |
| 180 | // Check just once and dump to a local copy if needed. |
| 181 | TextView local_parsed; |
| 182 | if (!parsed) { |
| 183 | parsed = &local_parsed; |
| 184 | } |
| 185 | |
| 186 | // Handle leading sign. |
| 187 | int sign = 1; |
| 188 | if (*text == '-') { |
| 189 | ++text; |
| 190 | sign = -1; |
| 191 | } else if (*text == '+') { |
| 192 | ++text; |
| 193 | } |
| 194 | // Parse the leading whole part as an integer. |
| 195 | intmax_t whole = svto_radix<10>(text); |
| 196 | parsed->assign(org_text.data(), text.data()); |
| 197 | |
| 198 | if (text.empty()) { |
| 199 | return whole; |
| 200 | } |
| 201 | |
| 202 | double frac = 0.0; |
| 203 | if (*text == '.') { // fractional part. |
| 204 | ++text; |
| 205 | double scale = 0.1; |
| 206 | while (text && isdigit(*text)) { |
| 207 | frac += scale * (*text++ - '0'); |
| 208 | scale /= 10.0; |
| 209 | } |
| 210 | } |
no test coverage detected