MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / read_number

Function read_number

extlibs/lua/src/liolib.c:463–494  ·  view source on GitHub ↗

** Read a number: first reads a valid prefix of a numeral into a buffer. ** Then it calls 'lua_stringtonumber' to check whether the format is ** correct and to convert it to a Lua number. */

Source from the content-addressed store, hash-verified

461** correct and to convert it to a Lua number.
462*/
463static int read_number (lua_State *L, FILE *f) {
464 RN rn;
465 int count = 0;
466 int hex = 0;
467 char decp[2];
468 rn.f = f; rn.n = 0;
469 decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
470 decp[1] = '.'; /* always accept a dot */
471 l_lockfile(rn.f);
472 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
473 test2(&rn, "-+"); /* optional sign */
474 if (test2(&rn, "00")) {
475 if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
476 else count = 1; /* count initial '0' as a valid digit */
477 }
478 count += readdigits(&rn, hex); /* integral part */
479 if (test2(&rn, decp)) /* decimal point? */
480 count += readdigits(&rn, hex); /* fractional part */
481 if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
482 test2(&rn, "-+"); /* exponent sign */
483 readdigits(&rn, 0); /* exponent digits */
484 }
485 ungetc(rn.c, rn.f); /* unread look-ahead char */
486 l_unlockfile(rn.f);
487 rn.buff[rn.n] = '\0'; /* finish string */
488 if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */
489 return 1; /* ok */
490 else { /* invalid format */
491 lua_pushnil(L); /* "result" to be removed */
492 return 0; /* read fails */
493 }
494}
495
496
497static int test_eof (lua_State *L, FILE *f) {

Callers 1

g_readFunction · 0.85

Calls 4

test2Function · 0.85
readdigitsFunction · 0.85
lua_pushnilFunction · 0.85
lua_stringtonumberFunction · 0.70

Tested by

no test coverage detected