diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-28 13:36:30 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-28 13:36:30 -0200 |
commit | e642cc42061beea0290398877d3ee6f48c3a55be (patch) | |
tree | 65e7cbf3ff1ec5623142162a3d7d1e78e9a97387 | |
parent | da57477c3dd51f6eb9afc10546cb125e8ae4d57c (diff) | |
download | lua-e642cc42061beea0290398877d3ee6f48c3a55be.tar.gz lua-e642cc42061beea0290398877d3ee6f48c3a55be.tar.bz2 lua-e642cc42061beea0290398877d3ee6f48c3a55be.zip |
correct handling of negative numbers in non-10 bases by 'tonumber'
(e.g., tonumber(-34, 8))
-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 | } |