more robust implementation for 'luaO_str2d'

This commit is contained in:
Roberto Ierusalimschy 2010-10-28 13:39:03 -02:00
parent e642cc4206
commit 94043a3a1a
1 changed files with 11 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.39 2010/04/15 19:44:43 roberto Exp roberto $
** $Id: lobject.c,v 2.40 2010/04/18 13:22:48 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -106,14 +106,19 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
}
static int checkend (const char *s, const char *endptr) {
if (endptr == s) return 0; /* no characters converted */
while (lisspace(cast(unsigned char, *endptr))) endptr++;
return (*endptr == '\0'); /* OK if no trailing characters */
}
int luaO_str2d (const char *s, lua_Number *result) {
char *endptr;
*result = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* conversion failed */
if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */
*result = cast_num(strtoul(s, &endptr, 16));
while (lisspace(cast(unsigned char, *endptr))) endptr++;
return (*endptr == '\0'); /* OK if no trailing characters */
if (checkend(s, endptr)) return 1; /* convertion OK? */
*result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */
return checkend(s, endptr);
}