Parse the input text to generate a number, and populate the result into item. */
| 303 | |
| 304 | /* Parse the input text to generate a number, and populate the result into item. */ |
| 305 | static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) |
| 306 | { |
| 307 | double number = 0; |
| 308 | unsigned char *after_end = NULL; |
| 309 | unsigned char number_c_string[64]; |
| 310 | unsigned char decimal_point = get_decimal_point(); |
| 311 | size_t i = 0; |
| 312 | |
| 313 | if ((input_buffer == NULL) || (input_buffer->content == NULL)) |
| 314 | { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /* copy the number into a temporary buffer and replace '.' with the decimal point |
| 319 | * of the current locale (for strtod) |
| 320 | * This also takes care of '\0' not necessarily being available for marking the end of the input */ |
| 321 | for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) |
| 322 | { |
| 323 | switch (buffer_at_offset(input_buffer)[i]) |
| 324 | { |
| 325 | case '0': |
| 326 | case '1': |
| 327 | case '2': |
| 328 | case '3': |
| 329 | case '4': |
| 330 | case '5': |
| 331 | case '6': |
| 332 | case '7': |
| 333 | case '8': |
| 334 | case '9': |
| 335 | case '+': |
| 336 | case '-': |
| 337 | case 'e': |
| 338 | case 'E': |
| 339 | number_c_string[i] = buffer_at_offset(input_buffer)[i]; |
| 340 | break; |
| 341 | |
| 342 | case '.': |
| 343 | number_c_string[i] = decimal_point; |
| 344 | break; |
| 345 | |
| 346 | default: |
| 347 | goto loop_end; |
| 348 | } |
| 349 | } |
| 350 | loop_end: |
| 351 | number_c_string[i] = '\0'; |
| 352 | |
| 353 | number = strtod((const char*)number_c_string, (char**)&after_end); |
| 354 | if (number_c_string == after_end) |
| 355 | { |
| 356 | return false; /* parse_error */ |
| 357 | } |
| 358 | |
| 359 | item->valuedouble = number; |
| 360 | |
| 361 | /* use saturation in case of overflow */ |
| 362 | if (number >= INT_MAX) |
no test coverage detected