Parse the input text to generate a number, and populate the result into item. */
| 265 | |
| 266 | /* Parse the input text to generate a number, and populate the result into item. */ |
| 267 | static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) |
| 268 | { |
| 269 | double number = 0; |
| 270 | unsigned char *after_end = NULL; |
| 271 | unsigned char number_c_string[64]; |
| 272 | unsigned char decimal_point = get_decimal_point(); |
| 273 | size_t i = 0; |
| 274 | |
| 275 | if ((input_buffer == NULL) || (input_buffer->content == NULL)) |
| 276 | { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | /* copy the number into a temporary buffer and replace '.' with the decimal point |
| 281 | * of the current locale (for strtod) |
| 282 | * This also takes care of '\0' not necessarily being available for marking the end of the input */ |
| 283 | for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) |
| 284 | { |
| 285 | switch (buffer_at_offset(input_buffer)[i]) |
| 286 | { |
| 287 | case '0': |
| 288 | case '1': |
| 289 | case '2': |
| 290 | case '3': |
| 291 | case '4': |
| 292 | case '5': |
| 293 | case '6': |
| 294 | case '7': |
| 295 | case '8': |
| 296 | case '9': |
| 297 | case '+': |
| 298 | case '-': |
| 299 | case 'e': |
| 300 | case 'E': |
| 301 | number_c_string[i] = buffer_at_offset(input_buffer)[i]; |
| 302 | break; |
| 303 | |
| 304 | case '.': |
| 305 | number_c_string[i] = decimal_point; |
| 306 | break; |
| 307 | |
| 308 | default: |
| 309 | goto loop_end; |
| 310 | } |
| 311 | } |
| 312 | loop_end: |
| 313 | number_c_string[i] = '\0'; |
| 314 | |
| 315 | number = strtod((const char*)number_c_string, (char**)&after_end); |
| 316 | if (number_c_string == after_end) |
| 317 | { |
| 318 | return false; /* parse_error */ |
| 319 | } |
| 320 | |
| 321 | item->valuedouble = number; |
| 322 | |
| 323 | /* use saturation in case of overflow */ |
| 324 | if (number >= INT_MAX) |
no test coverage detected