diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-01 15:18:06 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-01 15:18:06 -0300 |
commit | c549d4fe64c48ab645740e6d12c69c91250fad3d (patch) | |
tree | 1f98183ee7518f107d5902916468d99d7fce9527 /lobject.c | |
parent | ddff6ecf305e6da4f275b473ef6b66811848a3c6 (diff) | |
download | lua-c549d4fe64c48ab645740e6d12c69c91250fad3d.tar.gz lua-c549d4fe64c48ab645740e6d12c69c91250fad3d.tar.bz2 lua-c549d4fe64c48ab645740e6d12c69c91250fad3d.zip |
'lua_strtonum' (and 'luaO_str2num') now return string size, instead of
receiving it
Diffstat (limited to 'lobject.c')
-rw-r--r-- | lobject.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.82 2014/04/29 18:14:16 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.83 2014/04/30 16:48:44 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 | */ |
@@ -254,22 +254,21 @@ static lua_Number lua_strx2number (const char *s, char **endptr) { | |||
254 | /* }====================================================== */ | 254 | /* }====================================================== */ |
255 | 255 | ||
256 | 256 | ||
257 | static int l_str2d (const char *s, size_t len, lua_Number *result) { | 257 | static const char *l_str2d (const char *s, lua_Number *result) { |
258 | char *endptr; | 258 | char *endptr; |
259 | if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ | 259 | if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ |
260 | return 0; | 260 | return NULL; |
261 | else if (strpbrk(s, "xX")) /* hexa? */ | 261 | else if (strpbrk(s, "xX")) /* hexa? */ |
262 | *result = lua_strx2number(s, &endptr); | 262 | *result = lua_strx2number(s, &endptr); |
263 | else | 263 | else |
264 | *result = lua_str2number(s, &endptr); | 264 | *result = lua_str2number(s, &endptr); |
265 | if (endptr == s) return 0; /* nothing recognized */ | 265 | if (endptr == s) return 0; /* nothing recognized */ |
266 | while (lisspace(cast_uchar(*endptr))) endptr++; | 266 | while (lisspace(cast_uchar(*endptr))) endptr++; |
267 | return (endptr == s + len); /* OK if no trailing characters */ | 267 | return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */ |
268 | } | 268 | } |
269 | 269 | ||
270 | 270 | ||
271 | static int l_str2int (const char *s, size_t len, lua_Integer *result) { | 271 | static const char *l_str2int (const char *s, lua_Integer *result) { |
272 | const char *ends = s + len; | ||
273 | lua_Unsigned a = 0; | 272 | lua_Unsigned a = 0; |
274 | int empty = 1; | 273 | int empty = 1; |
275 | int neg; | 274 | int neg; |
@@ -290,25 +289,26 @@ static int l_str2int (const char *s, size_t len, lua_Integer *result) { | |||
290 | } | 289 | } |
291 | } | 290 | } |
292 | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ | 291 | while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ |
293 | if (empty || s != ends) return 0; /* something wrong in the numeral */ | 292 | if (empty || *s != '\0') return NULL; /* something wrong in the numeral */ |
294 | else { | 293 | else { |
295 | *result = l_castU2S((neg) ? 0u - a : a); | 294 | *result = l_castU2S((neg) ? 0u - a : a); |
296 | return 1; | 295 | return s; |
297 | } | 296 | } |
298 | } | 297 | } |
299 | 298 | ||
300 | 299 | ||
301 | int luaO_str2num (const char *s, size_t len, TValue *o) { | 300 | size_t luaO_str2num (const char *s, TValue *o) { |
302 | lua_Integer i; lua_Number n; | 301 | lua_Integer i; lua_Number n; |
303 | if (l_str2int(s, len, &i)) { /* try as an integer */ | 302 | const char *e; |
303 | if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */ | ||
304 | setivalue(o, i); | 304 | setivalue(o, i); |
305 | } | 305 | } |
306 | else if (l_str2d(s, len, &n)) { /* else try as a float */ | 306 | else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */ |
307 | setfltvalue(o, n); | 307 | setfltvalue(o, n); |
308 | } | 308 | } |
309 | else | 309 | else |
310 | return 0; /* conversion failed */ | 310 | return 0; /* conversion failed */ |
311 | return 1; /* success */ | 311 | return (e - s + 1); /* success; return string size */ |
312 | } | 312 | } |
313 | 313 | ||
314 | 314 | ||