** 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 */
| 438 | ** correct and to convert it to a Lua number |
| 439 | */ |
| 440 | static int read_number (lua_State *L, FILE *f) { |
| 441 | RN rn; |
| 442 | int count = 0; |
| 443 | int hex = 0; |
| 444 | char decp[2]; |
| 445 | rn.f = f; rn.n = 0; |
| 446 | decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ |
| 447 | decp[1] = '.'; /* always accept a dot */ |
| 448 | l_lockfile(rn.f); |
| 449 | do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ |
| 450 | test2(&rn, "-+"); /* optional signal */ |
| 451 | if (test2(&rn, "00")) { |
| 452 | if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ |
| 453 | else count = 1; /* count initial '0' as a valid digit */ |
| 454 | } |
| 455 | count += readdigits(&rn, hex); /* integral part */ |
| 456 | if (test2(&rn, decp)) /* decimal point? */ |
| 457 | count += readdigits(&rn, hex); /* fractional part */ |
| 458 | if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ |
| 459 | test2(&rn, "-+"); /* exponent signal */ |
| 460 | readdigits(&rn, 0); /* exponent digits */ |
| 461 | } |
| 462 | ungetc(rn.c, rn.f); /* unread look-ahead char */ |
| 463 | l_unlockfile(rn.f); |
| 464 | rn.buff[rn.n] = '\0'; /* finish string */ |
| 465 | if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ |
| 466 | return 1; /* ok */ |
| 467 | else { /* invalid format */ |
| 468 | lua_pushnil(L); /* "result" to be removed */ |
| 469 | return 0; /* read fails */ |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | |
| 474 | static int test_eof (lua_State *L, FILE *f) { |
no test coverage detected