diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-05-02 11:03:19 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-05-02 11:03:19 -0300 |
| commit | f3b52a606169b09b32ed172f95276c6735f5ce54 (patch) | |
| tree | 9dc0fea59b1416efa6458db3367b109497268b59 | |
| parent | 0232fbffbe43d84bef1bdb379b2b9f10eb11f750 (diff) | |
| download | lua-f3b52a606169b09b32ed172f95276c6735f5ce54.tar.gz lua-f3b52a606169b09b32ed172f95276c6735f5ce54.tar.bz2 lua-f3b52a606169b09b32ed172f95276c6735f5ce54.zip | |
'io.read("n")' accepts both a dot and the locale point as its
radix character + 'MAXRN' -> 'L_MAXLENNUM' + small detail in
'test2'
| -rw-r--r-- | liolib.c | 19 |
1 files changed, 11 insertions, 8 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: liolib.c,v 2.147 2015/07/15 14:40:28 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.148 2015/11/23 11:36:11 roberto Exp roberto $ |
| 3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -375,14 +375,17 @@ static int io_lines (lua_State *L) { | |||
| 375 | 375 | ||
| 376 | 376 | ||
| 377 | /* maximum length of a numeral */ | 377 | /* maximum length of a numeral */ |
| 378 | #define MAXRN 200 | 378 | #if !defined (L_MAXLENNUM) |
| 379 | #define L_MAXLENNUM 200 | ||
| 380 | #endif | ||
| 381 | |||
| 379 | 382 | ||
| 380 | /* auxiliary structure used by 'read_number' */ | 383 | /* auxiliary structure used by 'read_number' */ |
| 381 | typedef struct { | 384 | typedef struct { |
| 382 | FILE *f; /* file being read */ | 385 | FILE *f; /* file being read */ |
| 383 | int c; /* current character (look ahead) */ | 386 | int c; /* current character (look ahead) */ |
| 384 | int n; /* number of elements in buffer 'buff' */ | 387 | int n; /* number of elements in buffer 'buff' */ |
| 385 | char buff[MAXRN + 1]; /* +1 for ending '\0' */ | 388 | char buff[L_MAXLENNUM + 1]; /* +1 for ending '\0' */ |
| 386 | } RN; | 389 | } RN; |
| 387 | 390 | ||
| 388 | 391 | ||
| @@ -390,7 +393,7 @@ typedef struct { | |||
| 390 | ** Add current char to buffer (if not out of space) and read next one | 393 | ** Add current char to buffer (if not out of space) and read next one |
| 391 | */ | 394 | */ |
| 392 | static int nextc (RN *rn) { | 395 | static int nextc (RN *rn) { |
| 393 | if (rn->n >= MAXRN) { /* buffer overflow? */ | 396 | if (rn->n >= L_MAXLENNUM) { /* buffer overflow? */ |
| 394 | rn->buff[0] = '\0'; /* invalidate result */ | 397 | rn->buff[0] = '\0'; /* invalidate result */ |
| 395 | return 0; /* fail */ | 398 | return 0; /* fail */ |
| 396 | } | 399 | } |
| @@ -403,10 +406,10 @@ static int nextc (RN *rn) { | |||
| 403 | 406 | ||
| 404 | 407 | ||
| 405 | /* | 408 | /* |
| 406 | ** Accept current char if it is in 'set' (of size 1 or 2) | 409 | ** Accept current char if it is in 'set' (of size 2) |
| 407 | */ | 410 | */ |
| 408 | static int test2 (RN *rn, const char *set) { | 411 | static int test2 (RN *rn, const char *set) { |
| 409 | if (rn->c == set[0] || (rn->c == set[1] && rn->c != '\0')) | 412 | if (rn->c == set[0] || rn->c == set[1]) |
| 410 | return nextc(rn); | 413 | return nextc(rn); |
| 411 | else return 0; | 414 | else return 0; |
| 412 | } | 415 | } |
| @@ -435,11 +438,11 @@ static int read_number (lua_State *L, FILE *f) { | |||
| 435 | char decp[2]; | 438 | char decp[2]; |
| 436 | rn.f = f; rn.n = 0; | 439 | rn.f = f; rn.n = 0; |
| 437 | decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ | 440 | decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ |
| 438 | decp[1] = '\0'; | 441 | decp[1] = '.'; /* always accept a dot */ |
| 439 | l_lockfile(rn.f); | 442 | l_lockfile(rn.f); |
| 440 | do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ | 443 | do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ |
| 441 | test2(&rn, "-+"); /* optional signal */ | 444 | test2(&rn, "-+"); /* optional signal */ |
| 442 | if (test2(&rn, "0")) { | 445 | if (test2(&rn, "00")) { |
| 443 | if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ | 446 | if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ |
| 444 | else count = 1; /* count initial '0' as a valid digit */ | 447 | else count = 1; /* count initial '0' as a valid digit */ |
| 445 | } | 448 | } |
