tonumber: base 10 is not special, no base is

This commit is contained in:
Roberto Ierusalimschy 2011-07-27 09:14:06 -03:00
parent 03a078493e
commit 43c873895f
1 changed files with 10 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.263 2011/07/02 15:56:43 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.264 2011/07/05 12:49:35 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -46,20 +46,22 @@ static int luaB_print (lua_State *L) {
#define SPACECHARS " \f\n\r\t\v" #define SPACECHARS " \f\n\r\t\v"
static int luaB_tonumber (lua_State *L) { static int luaB_tonumber (lua_State *L) {
int base = luaL_optint(L, 2, 10); if (lua_isnoneornil(L, 2)) { /* standard conversion */
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); int isnum;
if (base == 10) { /* standard conversion */ lua_Number n = lua_tonumberx(L, 1, &isnum);
luaL_checkany(L, 1); if (isnum) {
if (lua_isnumber(L, 1)) { lua_pushnumber(L, n);
lua_pushnumber(L, lua_tonumber(L, 1));
return 1; return 1;
} /* else not a number */ } /* else not a number; must be something */
luaL_checkany(L, 1);
} }
else { else {
size_t l; size_t l;
const char *s = luaL_checklstring(L, 1, &l); const char *s = luaL_checklstring(L, 1, &l);
const char *e = s + l; /* end point for 's' */ const char *e = s + l; /* end point for 's' */
int base = luaL_checkint(L, 2);
int neg = 0; int neg = 0;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
s += strspn(s, SPACECHARS); /* skip initial spaces */ s += strspn(s, SPACECHARS); /* skip initial spaces */
if (*s == '-') { s++; neg = 1; } /* handle signal */ if (*s == '-') { s++; neg = 1; } /* handle signal */
else if (*s == '+') s++; else if (*s == '+') s++;