diff options
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 31 |
1 files changed, 12 insertions, 19 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.183 2013/06/20 15:02:49 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.184 2013/06/20 15:12:43 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -332,25 +332,18 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) { | |||
332 | } | 332 | } |
333 | 333 | ||
334 | 334 | ||
335 | LUA_API int lua_cvtonum (lua_State *L, int idx) { | 335 | LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) { |
336 | TValue *o = index2addr(L, idx); | 336 | lua_Integer i; lua_Number n; |
337 | if (ttisnumber(o)) return 1; /* already a number? */ | 337 | if (luaO_str2int(s, len, &i)) { /* try as an integer */ |
338 | else if (!ttisstring(o)) return 0; /* only strings can be converted */ | 338 | setivalue(L->top, i); |
339 | else { | 339 | } |
340 | lua_Integer i; lua_Number n; | 340 | else if (luaO_str2d(s, len, &n)) { /* else try as a float */ |
341 | const char *s = svalue(o); | 341 | setnvalue(L->top, n); |
342 | size_t len = tsvalue(o)->len; | ||
343 | if (luaO_str2int(s, len, &i)) { | ||
344 | setivalue(o, i); | ||
345 | return 1; | ||
346 | } | ||
347 | else if (luaO_str2d(s, len, &n)) { | ||
348 | setivalue(o, i); | ||
349 | setnvalue(o, n); | ||
350 | return 1; | ||
351 | } | ||
352 | else return 0; | ||
353 | } | 342 | } |
343 | else | ||
344 | return 0; /* conversion failed */ | ||
345 | api_incr_top(L); | ||
346 | return 1; | ||
354 | } | 347 | } |
355 | 348 | ||
356 | 349 | ||