diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.249 2010/09/07 19:21:39 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.250 2010/09/07 19:38:36 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 | */ |
@@ -56,12 +56,15 @@ static int luaB_tonumber (lua_State *L) { | |||
56 | const char *s1 = luaL_checkstring(L, 1); | 56 | const char *s1 = luaL_checkstring(L, 1); |
57 | char *s2; | 57 | char *s2; |
58 | unsigned long n; | 58 | unsigned long n; |
59 | int neg = 0; | ||
59 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); | 60 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); |
61 | while (isspace((unsigned char)(*s1))) s1++; /* skip initial spaces */ | ||
62 | if (*s1 == '-') { s1++; neg = 1; } | ||
60 | n = strtoul(s1, &s2, base); | 63 | n = strtoul(s1, &s2, base); |
61 | if (s1 != s2) { /* at least one valid digit? */ | 64 | if (s1 != s2) { /* at least one valid digit? */ |
62 | while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ | 65 | while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ |
63 | if (*s2 == '\0') { /* no invalid trailing characters? */ | 66 | if (*s2 == '\0') { /* no invalid trailing characters? */ |
64 | lua_pushnumber(L, (lua_Number)n); | 67 | lua_pushnumber(L, (neg) ? -(lua_Number)n : (lua_Number)n); |
65 | return 1; | 68 | return 1; |
66 | } | 69 | } |
67 | } | 70 | } |