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