diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-14 12:59:04 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-14 12:59:04 -0300 |
commit | 36e8771076705d33261a88a3c144967b1840671a (patch) | |
tree | b2c2c7b5fd8487227d9629e7507d91cd1d15a834 /lobject.c | |
parent | 27f09415e38b00f16013649d3d91c430a6cc33ff (diff) | |
download | lua-36e8771076705d33261a88a3c144967b1840671a.tar.gz lua-36e8771076705d33261a88a3c144967b1840671a.tar.bz2 lua-36e8771076705d33261a88a3c144967b1840671a.zip |
'luaO_str2int' more generic: accepts white spaces around the numeral
and handles signal
Diffstat (limited to 'lobject.c')
-rw-r--r-- | lobject.c | 33 |
1 files changed, 22 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.61 2013/04/29 16:57:28 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.62 2013/05/02 12:37:24 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 | */ |
@@ -133,11 +133,6 @@ int luaO_hexavalue (int c) { | |||
133 | } | 133 | } |
134 | 134 | ||
135 | 135 | ||
136 | #if !defined(lua_strx2number) | ||
137 | |||
138 | #include <math.h> | ||
139 | |||
140 | |||
141 | static int isneg (const char **s) { | 136 | static int isneg (const char **s) { |
142 | if (**s == '-') { (*s)++; return 1; } | 137 | if (**s == '-') { (*s)++; return 1; } |
143 | else if (**s == '+') (*s)++; | 138 | else if (**s == '+') (*s)++; |
@@ -145,6 +140,11 @@ static int isneg (const char **s) { | |||
145 | } | 140 | } |
146 | 141 | ||
147 | 142 | ||
143 | #if !defined(lua_strx2number) | ||
144 | |||
145 | #include <math.h> | ||
146 | |||
147 | |||
148 | static lua_Number readhexa (const char **s, lua_Number r, int *count) { | 148 | static lua_Number readhexa (const char **s, lua_Number r, int *count) { |
149 | for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ | 149 | for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */ |
150 | r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); | 150 | r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s))); |
@@ -212,21 +212,32 @@ int luaO_str2d (const char *s, size_t len, lua_Number *result) { | |||
212 | } | 212 | } |
213 | 213 | ||
214 | 214 | ||
215 | int luaO_str2int (const char *s, lua_Integer *result) { | 215 | int luaO_str2int (const char *s, size_t len, lua_Integer *result) { |
216 | const char *ends = s + len; | ||
216 | lua_Unsigned a = 0; | 217 | lua_Unsigned a = 0; |
218 | int empty = 1; | ||
219 | int neg; | ||
220 | while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ | ||
221 | neg = isneg(&s); | ||
217 | if (s[0] == '0' && | 222 | if (s[0] == '0' && |
218 | (s[1] == 'x' || s[1] == 'X')) { /* hexa? */ | 223 | (s[1] == 'x' || s[1] == 'X')) { /* hexa? */ |
219 | s += 2; /* skip '0x' */ | 224 | s += 2; /* skip '0x' */ |
220 | for (; lisxdigit(cast_uchar(*s)); s++) | 225 | for (; lisxdigit(cast_uchar(*s)); s++) { |
221 | a = a * 16 + luaO_hexavalue(cast_uchar(*s)); | 226 | a = a * 16 + luaO_hexavalue(cast_uchar(*s)); |
227 | empty = 0; | ||
228 | } | ||
222 | } | 229 | } |
223 | else { /* decimal */ | 230 | else { /* decimal */ |
224 | for (; lisdigit(cast_uchar(*s)); s++) | 231 | for (; lisdigit(cast_uchar(*s)); s++) { |
225 | a = a * 10 + luaO_hexavalue(cast_uchar(*s)); | 232 | a = a * 10 + luaO_hexavalue(cast_uchar(*s)); |
233 | empty = 0; | ||
234 | } | ||
226 | } | 235 | } |
227 | if (*s != '\0') return 0; /* something wrong in the numeral */ | 236 | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ |
237 | if (empty || s != ends) return 0; /* something wrong in the numeral */ | ||
228 | else { | 238 | else { |
229 | *result = cast(lua_Integer, a); | 239 | if (neg) *result = -cast(lua_Integer, a); |
240 | else *result = cast(lua_Integer, a); | ||
230 | return 1; | 241 | return 1; |
231 | } | 242 | } |
232 | } | 243 | } |