Browse Source

more robust implementation for 'luaO_str2d'

pull/9/head
Roberto Ierusalimschy 14 years ago
parent
commit
94043a3a1a
  1. 17
      lobject.c

17
lobject.c

@ -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);
}

Loading…
Cancel
Save