MCPcopy Create free account
hub / github.com/ByConity/ByConity / parse_number

Function parse_number

contrib/cJSON/cJSON.cpp:266–351  ·  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

264
265/* Parse the input text to generate a number, and populate the result into item. */
266static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer)
267{
268 double number = 0;
269 unsigned char *after_end = NULL;
270 unsigned char number_c_string[64];
271 unsigned char decimal_point = get_decimal_point();
272 bool isInteger = true;
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 number_c_string[i] = buffer_at_offset(input_buffer)[i];
300 break;
301 case 'e':
302 case 'E':
303 number_c_string[i] = buffer_at_offset(input_buffer)[i];
304 isInteger = false;
305 break;
306
307 case '.':
308 number_c_string[i] = decimal_point;
309 isInteger = false;
310 break;
311
312 default:
313 goto loop_end;
314 }
315 }
316loop_end:
317 number_c_string[i] = '\0';
318
319 number = strtod((const char*)number_c_string, (char**)&after_end);
320 if (number_c_string == after_end)
321 {
322 return false; /* parse_error */
323 }

Callers 1

parse_valueFunction · 0.85

Calls 2

get_decimal_pointFunction · 0.85
cJSON_strdupFunction · 0.85

Tested by

no test coverage detected