'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

12
lapi.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lapi.c,v 2.206 2014/04/29 18:14:16 roberto Exp roberto $ ** $Id: lapi.c,v 2.207 2014/04/30 16:48:44 roberto Exp roberto $
** Lua API ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -333,13 +333,11 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
} }
LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) { LUA_API size_t lua_strtonum (lua_State *L, const char *s) {
if (!luaO_str2num(s, len, L->top)) size_t sz = luaO_str2num(s, L->top);
return 0; /* conversion failed */ if (sz != 0)
else {
api_incr_top(L); api_incr_top(L);
return 1; return sz;
}
} }

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 ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) {
#define SPACECHARS " \f\n\r\t\v" #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; lua_Unsigned n = 0;
int neg = 0; int neg = 0;
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)) /* no digit? */ if (!isalnum((unsigned char)*s)) /* no digit? */
return 0; return NULL;
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) return 0; /* invalid numeral */ if (digit >= base) return NULL; /* 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) /* invalid trailing characters? */ if (*s != '\0') /* invalid trailing characters? */
return 0; return NULL;
*pn = (lua_Integer)((neg) ? (0u - n) : n); *pn = (lua_Integer)((neg) ? (0u - n) : n);
return 1; return s;
} }
static int luaB_tonumber (lua_State *L) { 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); luaL_checkany(L, 1);
if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
lua_settop(L, 1); /* yes; return it */ lua_settop(L, 1); /* yes; return it */
@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) {
else { else {
size_t l; size_t l;
const char *s = lua_tolstring(L, 1, &l); const char *s = lua_tolstring(L, 1, &l);
if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */ if (s != NULL && lua_strtonum(L, s) == l + 1)
return 1; return 1; /* successful conversion to number */
/* else not a number */ /* else not a number */
} }
} }
else { else {
size_t l; size_t l;
const char *s; const char *s;
lua_Integer n; lua_Integer n = 0; /* to avoid warnings */
int base = luaL_checkint(L, 2); int base = luaL_checkint(L, 2);
luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */
s = luaL_checklstring(L, 1, &l); s = luaL_checklstring(L, 1, &l);
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 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); lua_pushinteger(L, n);
return 1; return 1;
} /* else not a number */ } /* else not a number */

4
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.74 2014/02/14 15:23:51 roberto Exp roberto $ ** $Id: llex.c,v 2.75 2014/04/30 16:48:44 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -210,7 +210,7 @@ static void buffreplace (LexState *ls, char from, char to) {
#endif #endif
#define buff2num(b,o) luaO_str2num(luaZ_buffer(b), luaZ_bufflen(b) - 1, o) #define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
/* /*
** in case of format error, try to change decimal point separator to ** in case of format error, try to change decimal point separator to

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.82 2014/04/29 18:14:16 roberto Exp roberto $ ** $Id: lobject.c,v 2.83 2014/04/30 16:48:44 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -254,22 +254,21 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
/* }====================================================== */ /* }====================================================== */
static int l_str2d (const char *s, size_t len, lua_Number *result) { static const char *l_str2d (const char *s, lua_Number *result) {
char *endptr; char *endptr;
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
return 0; return NULL;
else if (strpbrk(s, "xX")) /* hexa? */ else if (strpbrk(s, "xX")) /* hexa? */
*result = lua_strx2number(s, &endptr); *result = lua_strx2number(s, &endptr);
else else
*result = lua_str2number(s, &endptr); *result = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* nothing recognized */ if (endptr == s) return 0; /* nothing recognized */
while (lisspace(cast_uchar(*endptr))) endptr++; while (lisspace(cast_uchar(*endptr))) endptr++;
return (endptr == s + len); /* OK if no trailing characters */ return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
} }
static int l_str2int (const char *s, size_t len, lua_Integer *result) { static const char *l_str2int (const char *s, lua_Integer *result) {
const char *ends = s + len;
lua_Unsigned a = 0; lua_Unsigned a = 0;
int empty = 1; int empty = 1;
int neg; int neg;
@ -290,25 +289,26 @@ static int l_str2int (const char *s, size_t len, lua_Integer *result) {
} }
} }
while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
if (empty || s != ends) return 0; /* something wrong in the numeral */ if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
else { else {
*result = l_castU2S((neg) ? 0u - a : a); *result = l_castU2S((neg) ? 0u - a : a);
return 1; return s;
} }
} }
int luaO_str2num (const char *s, size_t len, TValue *o) { size_t luaO_str2num (const char *s, TValue *o) {
lua_Integer i; lua_Number n; lua_Integer i; lua_Number n;
if (l_str2int(s, len, &i)) { /* try as an integer */ const char *e;
if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
setivalue(o, i); setivalue(o, i);
} }
else if (l_str2d(s, len, &n)) { /* else try as a float */ else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
setfltvalue(o, n); setfltvalue(o, n);
} }
else else
return 0; /* conversion failed */ return 0; /* conversion failed */
return 1; /* success */ return (e - s + 1); /* success; return string size */
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.h,v 2.87 2014/04/29 18:14:16 roberto Exp roberto $ ** $Id: lobject.h,v 2.88 2014/04/30 16:48:44 roberto Exp roberto $
** Type definitions for Lua objects ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -500,7 +500,7 @@ LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x);
LUAI_FUNC int luaO_ceillog2 (unsigned int x); LUAI_FUNC int luaO_ceillog2 (unsigned int x);
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
const TValue *p2, TValue *res); const TValue *p2, TValue *res);
LUAI_FUNC int luaO_str2num (const char *s, size_t len, TValue *o); LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
LUAI_FUNC int luaO_hexavalue (int c); LUAI_FUNC int luaO_hexavalue (int c);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
va_list argp); va_list argp);

4
lua.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lua.h,v 1.301 2014/03/12 20:57:40 roberto Exp roberto $ ** $Id: lua.h,v 1.302 2014/03/20 19:42:35 roberto Exp roberto $
** Lua - A Scripting Language ** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file ** See Copyright Notice at the end of this file
@ -311,7 +311,7 @@ LUA_API int (lua_next) (lua_State *L, int idx);
LUA_API void (lua_concat) (lua_State *L, int n); LUA_API void (lua_concat) (lua_State *L, int n);
LUA_API void (lua_len) (lua_State *L, int idx); LUA_API void (lua_len) (lua_State *L, int idx);
LUA_API int (lua_strtonum) (lua_State *L, const char *s, size_t len); LUA_API size_t (lua_strtonum) (lua_State *L, const char *s);
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);

8
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.203 2014/04/30 16:50:16 roberto Exp roberto $ ** $Id: lvm.c,v 2.204 2014/04/30 19:29:51 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -70,7 +70,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
return 1; return 1;
} }
else if (ttisstring(obj) && else if (ttisstring(obj) &&
luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) { luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
obj = &v; obj = &v;
goto again; /* convert result from 'luaO_str2num' to a float */ goto again; /* convert result from 'luaO_str2num' to a float */
} }
@ -109,8 +109,8 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
*p = ivalue(obj); *p = ivalue(obj);
return 1; return 1;
} }
if (ttisstring(obj) && else if (ttisstring(obj) &&
luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) { luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
obj = &v; obj = &v;
goto again; /* convert result from 'luaO_str2num' to an integer */ goto again; /* convert result from 'luaO_str2num' to an integer */
} }