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