mirror of https://github.com/rusefi/lua.git
'tonumber': does not accept numbers when given a base + convertion from
strings to numbers with a base moved to a separated function (more modular)
This commit is contained in:
parent
5519c98655
commit
beff5b078a
59
lbaselib.c
59
lbaselib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lbaselib.c,v 1.276 2013/02/21 13:44:53 roberto Exp roberto $
|
** $Id: lbaselib.c,v 1.277 2013/05/16 18:35:57 roberto Exp roberto $
|
||||||
** Basic library
|
** Basic library
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -45,40 +45,57 @@ 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 b_str2int (const char *s, const char *e, int base, lua_Integer *pn) {
|
||||||
if (lua_isnoneornil(L, 2)) { /* standard conversion */
|
lua_Unsigned n = 0;
|
||||||
luaL_checkany(L, 1);
|
|
||||||
if (lua_cvtonum(L, 1)) { /* can convert to a number? */
|
|
||||||
lua_settop(L, 1); /* yes; return converted value */
|
|
||||||
return 1;
|
|
||||||
} /* else not a number */
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
size_t l;
|
|
||||||
const char *s = luaL_checklstring(L, 1, &l);
|
|
||||||
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++;
|
||||||
if (isalnum((unsigned char)*s)) {
|
if (!isalnum((unsigned char)*s)) /* no digit? */
|
||||||
lua_Integer n = 0;
|
return 0;
|
||||||
do {
|
do {
|
||||||
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
|
||||||
: toupper((unsigned char)*s) - 'A' + 10;
|
: toupper((unsigned char)*s) - 'A' + 10;
|
||||||
if (digit >= base) break; /* invalid numeral; force a fail */
|
if (digit >= base) return 0; /* invalid numeral */
|
||||||
n = n * base + digit;
|
n = n * base + digit;
|
||||||
s++;
|
s++;
|
||||||
} while (isalnum((unsigned char)*s));
|
} while (isalnum((unsigned char)*s));
|
||||||
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
s += strspn(s, SPACECHARS); /* skip trailing spaces */
|
||||||
if (s == e) { /* no invalid trailing characters? */
|
if (s != e) /* invalid trailing characters? */
|
||||||
lua_pushinteger(L, (neg) ? -n : n);
|
return 0;
|
||||||
|
*pn = (neg) ? -(lua_Integer)n : (lua_Integer)n;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_tonumber (lua_State *L) {
|
||||||
|
if (lua_isnoneornil(L, 2)) { /* standard conversion */
|
||||||
|
luaL_checkany(L, 1);
|
||||||
|
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
|
||||||
|
lua_settop(L, 1); /* yes; return it */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size_t l;
|
||||||
|
const char *s = lua_tolstring(L, 1, &l);
|
||||||
|
if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */
|
||||||
|
return 1;
|
||||||
|
/* else not a number */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size_t l;
|
||||||
|
const char *s;
|
||||||
|
lua_Integer n;
|
||||||
|
int base = lua_tointeger(L, 2);
|
||||||
|
luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
|
||||||
|
s = luaL_checklstring(L, 1, &l);
|
||||||
|
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||||
|
if (b_str2int(s, s + l, base, &n)) {
|
||||||
|
lua_pushinteger(L, n);
|
||||||
return 1;
|
return 1;
|
||||||
} /* else not a number */
|
} /* else not a number */
|
||||||
} /* else not a number */
|
} /* else not a number */
|
||||||
}
|
|
||||||
lua_pushnil(L); /* not a number */
|
lua_pushnil(L); /* not a number */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue