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