diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-28 13:39:03 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-28 13:39:03 -0200 |
commit | 94043a3a1a3108a9ce52dd135b7847f5e72bd51d (patch) | |
tree | 8679ba4ef9ef12b5a0977291b66a5880fec6dd39 | |
parent | e642cc42061beea0290398877d3ee6f48c3a55be (diff) | |
download | lua-94043a3a1a3108a9ce52dd135b7847f5e72bd51d.tar.gz lua-94043a3a1a3108a9ce52dd135b7847f5e72bd51d.tar.bz2 lua-94043a3a1a3108a9ce52dd135b7847f5e72bd51d.zip |
more robust implementation for 'luaO_str2d'
-rw-r--r-- | lobject.c | 17 |
1 files changed, 11 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.39 2010/04/15 19:44:43 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.40 2010/04/18 13:22:48 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -106,14 +106,19 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { | |||
106 | } | 106 | } |
107 | 107 | ||
108 | 108 | ||
109 | static int checkend (const char *s, const char *endptr) { | ||
110 | if (endptr == s) return 0; /* no characters converted */ | ||
111 | while (lisspace(cast(unsigned char, *endptr))) endptr++; | ||
112 | return (*endptr == '\0'); /* OK if no trailing characters */ | ||
113 | } | ||
114 | |||
115 | |||
109 | int luaO_str2d (const char *s, lua_Number *result) { | 116 | int luaO_str2d (const char *s, lua_Number *result) { |
110 | char *endptr; | 117 | char *endptr; |
111 | *result = lua_str2number(s, &endptr); | 118 | *result = lua_str2number(s, &endptr); |
112 | if (endptr == s) return 0; /* conversion failed */ | 119 | if (checkend(s, endptr)) return 1; /* convertion OK? */ |
113 | if (*endptr == 'x' || *endptr == 'X') /* maybe an hexadecimal constant? */ | 120 | *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */ |
114 | *result = cast_num(strtoul(s, &endptr, 16)); | 121 | return checkend(s, endptr); |
115 | while (lisspace(cast(unsigned char, *endptr))) endptr++; | ||
116 | return (*endptr == '\0'); /* OK if no trailing characters */ | ||
117 | } | 122 | } |
118 | 123 | ||
119 | 124 | ||