MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / read_number

Function read_number

lib/lua/src/liolib.c:480–511  ·  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

478** correct and to convert it to a Lua number.
479*/
480static int read_number (lua_State *L, FILE *f) {
481 RN rn;
482 int count = 0;
483 int hex = 0;
484 char decp[2];
485 rn.f = f; rn.n = 0;
486 decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */
487 decp[1] = '.'; /* always accept a dot */
488 l_lockfile(rn.f);
489 do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */
490 test2(&rn, "-+"); /* optional sign */
491 if (test2(&rn, "00")) {
492 if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */
493 else count = 1; /* count initial '0' as a valid digit */
494 }
495 count += readdigits(&rn, hex); /* integral part */
496 if (test2(&rn, decp)) /* decimal point? */
497 count += readdigits(&rn, hex); /* fractional part */
498 if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */
499 test2(&rn, "-+"); /* exponent sign */
500 readdigits(&rn, 0); /* exponent digits */
501 }
502 ungetc(rn.c, rn.f); /* unread look-ahead char */
503 l_unlockfile(rn.f);
504 rn.buff[rn.n] = '\0'; /* finish string */
505 if (l_likely(lua_stringtonumber(L, rn.buff)))
506 return 1; /* ok, it is a valid number */
507 else { /* invalid format */
508 lua_pushnil(L); /* "result" to be removed */
509 return 0; /* read fails */
510 }
511}
512
513
514static int test_eof (lua_State *L, FILE *f) {

Callers 1

g_readFunction · 0.85

Calls 4

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

Tested by

no test coverage detected