MCPcopy Create free account
hub / github.com/crownengine/crown / parse_number

Function parse_number

src/core/json/sjson.cpp:287–340  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

285 }
286
287 static f64 parse_number(const char *json)
288 {
289 if (json == NULL) {
290 fatal("json is NULL");
291 return 0.0f;
292 }
293
294 TempAllocator512 alloc;
295 Array<char> number(alloc);
296
297 if (*json == '-') {
298 array::push_back(number, '-');
299 ++json;
300 }
301 while (isdigit(*json)) {
302 array::push_back(number, *json);
303 ++json;
304 }
305
306 if (*json == '.') {
307 array::push_back(number, '.');
308 while (*++json && isdigit(*json)) {
309 array::push_back(number, *json);
310 }
311 }
312
313 if (*json == 'e' || *json == 'E') {
314 array::push_back(number, *json);
315 ++json;
316
317 if (*json == '-' || *json == '+') {
318 array::push_back(number, *json);
319 ++json;
320 }
321 while (isdigit(*json)) {
322 array::push_back(number, *json);
323 ++json;
324 }
325 }
326
327 if (array::size(number) == 0) {
328 fatal("Invalid number");
329 return 0.0f;
330 }
331 array::push_back(number, '\0');
332
333 errno = 0;
334 f64 val = strtod(array::begin(number), NULL);
335 if (errno == ERANGE || errno == EINVAL) {
336 fatal("Bad number '%s'", array::begin(number));
337 return 0.0f;
338 }
339 return val;
340 }
341
342 s32 parse_int(const char *json)
343 {

Callers 2

parse_intFunction · 0.70
parse_floatFunction · 0.70

Calls 3

fatalFunction · 0.70
sizeFunction · 0.50
beginFunction · 0.50

Tested by

no test coverage detected