diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-07-27 09:14:06 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2011-07-27 09:14:06 -0300 |
commit | 43c873895f5c78ca1b678e91709f36ce2023432d (patch) | |
tree | 72b819ae07ee3a4c82e2024b09557bf6fa82e803 /lbaselib.c | |
parent | 03a078493ebfb9a85aea44594ca3a90f9d297f85 (diff) | |
download | lua-43c873895f5c78ca1b678e91709f36ce2023432d.tar.gz lua-43c873895f5c78ca1b678e91709f36ce2023432d.tar.bz2 lua-43c873895f5c78ca1b678e91709f36ce2023432d.zip |
tonumber: base 10 is not special, no base is
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 18 |
1 files changed, 10 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.263 2011/07/02 15:56:43 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.264 2011/07/05 12:49:35 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 | */ |
@@ -46,20 +46,22 @@ static int luaB_print (lua_State *L) { | |||
46 | #define SPACECHARS " \f\n\r\t\v" | 46 | #define SPACECHARS " \f\n\r\t\v" |
47 | 47 | ||
48 | static int luaB_tonumber (lua_State *L) { | 48 | static int luaB_tonumber (lua_State *L) { |
49 | int base = luaL_optint(L, 2, 10); | 49 | if (lua_isnoneornil(L, 2)) { /* standard conversion */ |
50 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); | 50 | int isnum; |
51 | if (base == 10) { /* standard conversion */ | 51 | lua_Number n = lua_tonumberx(L, 1, &isnum); |
52 | luaL_checkany(L, 1); | 52 | if (isnum) { |
53 | if (lua_isnumber(L, 1)) { | 53 | lua_pushnumber(L, n); |
54 | lua_pushnumber(L, lua_tonumber(L, 1)); | ||
55 | return 1; | 54 | return 1; |
56 | } /* else not a number */ | 55 | } /* else not a number; must be something */ |
56 | luaL_checkany(L, 1); | ||
57 | } | 57 | } |
58 | else { | 58 | else { |
59 | size_t l; | 59 | size_t l; |
60 | const char *s = luaL_checklstring(L, 1, &l); | 60 | const char *s = luaL_checklstring(L, 1, &l); |
61 | const char *e = s + l; /* end point for 's' */ | 61 | const char *e = s + l; /* end point for 's' */ |
62 | int base = luaL_checkint(L, 2); | ||
62 | int neg = 0; | 63 | int neg = 0; |
64 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); | ||
63 | s += strspn(s, SPACECHARS); /* skip initial spaces */ | 65 | s += strspn(s, SPACECHARS); /* skip initial spaces */ |
64 | if (*s == '-') { s++; neg = 1; } /* handle signal */ | 66 | if (*s == '-') { s++; neg = 1; } /* handle signal */ |
65 | else if (*s == '+') s++; | 67 | else if (*s == '+') s++; |