diff options
-rw-r--r-- | lbaselib.c | 14 |
1 files changed, 9 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.250 2010/09/07 19:38:36 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.251 2010/10/28 15:36:30 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -43,6 +43,8 @@ static int luaB_print (lua_State *L) { | |||
43 | } | 43 | } |
44 | 44 | ||
45 | 45 | ||
46 | #define SPACECHARS " \f\n\r\t\v" | ||
47 | |||
46 | static int luaB_tonumber (lua_State *L) { | 48 | static int luaB_tonumber (lua_State *L) { |
47 | int base = luaL_optint(L, 2, 10); | 49 | int base = luaL_optint(L, 2, 10); |
48 | if (base == 10) { /* standard conversion */ | 50 | if (base == 10) { /* standard conversion */ |
@@ -53,17 +55,19 @@ static int luaB_tonumber (lua_State *L) { | |||
53 | } | 55 | } |
54 | } | 56 | } |
55 | else { | 57 | else { |
56 | const char *s1 = luaL_checkstring(L, 1); | 58 | size_t l1; |
59 | const char *s1 = luaL_checklstring(L, 1, &l1); | ||
60 | const char *e1 = s1 + l1; | ||
57 | char *s2; | 61 | char *s2; |
58 | unsigned long n; | 62 | unsigned long n; |
59 | int neg = 0; | 63 | int neg = 0; |
60 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); | 64 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); |
61 | while (isspace((unsigned char)(*s1))) s1++; /* skip initial spaces */ | 65 | s1 += strspn(s1, SPACECHARS); /* skip initial spaces */ |
62 | if (*s1 == '-') { s1++; neg = 1; } | 66 | if (*s1 == '-') { s1++; neg = 1; } |
63 | n = strtoul(s1, &s2, base); | 67 | n = strtoul(s1, &s2, base); |
64 | if (s1 != s2) { /* at least one valid digit? */ | 68 | if (s1 != s2) { /* at least one valid digit? */ |
65 | while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ | 69 | s2 += strspn(s2, SPACECHARS); /* skip trailing spaces */ |
66 | if (*s2 == '\0') { /* no invalid trailing characters? */ | 70 | if (s2 == e1) { /* no invalid trailing characters? */ |
67 | lua_pushnumber(L, (neg) ? -(lua_Number)n : (lua_Number)n); | 71 | lua_pushnumber(L, (neg) ? -(lua_Number)n : (lua_Number)n); |
68 | return 1; | 72 | return 1; |
69 | } | 73 | } |