'lua_strtonum' (and 'luaO_str2num') now return string size, instead of

receiving it
This commit is contained in:
Roberto Ierusalimschy
2014-05-01 15:18:06 -03:00
parent ddff6ecf30
commit c549d4fe64
7 changed files with 39 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
** $Id: lbaselib.c,v 1.284 2014/02/14 16:45:38 roberto Exp roberto $
** $Id: lbaselib.c,v 1.285 2014/03/12 20:57:40 roberto Exp roberto $
** Basic library
** See Copyright Notice in lua.h
*/
@@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) {
#define SPACECHARS " \f\n\r\t\v"
static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) {
static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
lua_Unsigned n = 0;
int neg = 0;
s += strspn(s, SPACECHARS); /* skip initial spaces */
if (*s == '-') { s++; neg = 1; } /* handle signal */
else if (*s == '+') s++;
if (!isalnum((unsigned char)*s)) /* no digit? */
return 0;
return NULL;
do {
int digit = (isdigit((unsigned char)*s)) ? *s - '0'
: toupper((unsigned char)*s) - 'A' + 10;
if (digit >= base) return 0; /* invalid numeral */
if (digit >= base) return NULL; /* invalid numeral */
n = n * base + digit;
s++;
} while (isalnum((unsigned char)*s));
s += strspn(s, SPACECHARS); /* skip trailing spaces */
if (s != e) /* invalid trailing characters? */
return 0;
if (*s != '\0') /* invalid trailing characters? */
return NULL;
*pn = (lua_Integer)((neg) ? (0u - n) : n);
return 1;
return s;
}
static int luaB_tonumber (lua_State *L) {
if (lua_isnoneornil(L, 2)) { /* standard conversion */
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 */
@@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) {
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;
if (s != NULL && lua_strtonum(L, s) == l + 1)
return 1; /* successful conversion to number */
/* else not a number */
}
}
else {
size_t l;
const char *s;
lua_Integer n;
lua_Integer n = 0; /* to avoid warnings */
int base = luaL_checkint(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)) {
if (b_str2int(s, base, &n) == s + l) {
lua_pushinteger(L, n);
return 1;
} /* else not a number */