|
|
@ -1,5 +1,5 @@ |
|
|
|
/*
|
|
|
|
** $Id: lbaselib.c,v 1.250 2010/09/07 19:38:36 roberto Exp roberto $ |
|
|
|
** $Id: lbaselib.c,v 1.251 2010/10/28 15:36:30 roberto Exp roberto $ |
|
|
|
** Basic library |
|
|
|
** See Copyright Notice in lua.h |
|
|
|
*/ |
|
|
@ -43,6 +43,8 @@ static int luaB_print (lua_State *L) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#define SPACECHARS " \f\n\r\t\v" |
|
|
|
|
|
|
|
static int luaB_tonumber (lua_State *L) { |
|
|
|
int base = luaL_optint(L, 2, 10); |
|
|
|
if (base == 10) { /* standard conversion */ |
|
|
@ -53,17 +55,19 @@ static int luaB_tonumber (lua_State *L) { |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
const char *s1 = luaL_checkstring(L, 1); |
|
|
|
size_t l1; |
|
|
|
const char *s1 = luaL_checklstring(L, 1, &l1); |
|
|
|
const char *e1 = s1 + l1; |
|
|
|
char *s2; |
|
|
|
unsigned long n; |
|
|
|
int neg = 0; |
|
|
|
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); |
|
|
|
while (isspace((unsigned char)(*s1))) s1++; /* skip initial spaces */ |
|
|
|
s1 += strspn(s1, SPACECHARS); /* skip initial spaces */ |
|
|
|
if (*s1 == '-') { s1++; neg = 1; } |
|
|
|
n = strtoul(s1, &s2, base); |
|
|
|
if (s1 != s2) { /* at least one valid digit? */ |
|
|
|
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */ |
|
|
|
if (*s2 == '\0') { /* no invalid trailing characters? */ |
|
|
|
s2 += strspn(s2, SPACECHARS); /* skip trailing spaces */ |
|
|
|
if (s2 == e1) { /* no invalid trailing characters? */ |
|
|
|
lua_pushnumber(L, (neg) ? -(lua_Number)n : (lua_Number)n); |
|
|
|
return 1; |
|
|
|
} |
|
|
|