'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: 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
** 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;
if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
return 0;
return NULL;
else if (strpbrk(s, "xX")) /* hexa? */
*result = lua_strx2number(s, &endptr);
else
*result = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* nothing recognized */
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) {
const char *ends = s + len;
static const char *l_str2int (const char *s, lua_Integer *result) {
lua_Unsigned a = 0;
int empty = 1;
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 */
if (empty || s != ends) return 0; /* something wrong in the numeral */
if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
else {
*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;
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);
}
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);
}
else
return 0; /* conversion failed */
return 1; /* success */
return (e - s + 1); /* success; return string size */
}