MCPcopy Create free account
hub / github.com/Netis/cloud-probe / parse_number

Function parse_number

cpworker/src/cJSON/cJSON.c:307–381  ·  view source on GitHub ↗

Parse the input text to generate a number, and populate the result into item. */

Source from the content-addressed store, hash-verified

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

Callers 1

parse_valueFunction · 0.85

Calls 1

get_decimal_pointFunction · 0.85

Tested by

no test coverage detected