aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-07-05 11:29:51 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-07-05 11:29:51 -0300
commit5519c986550677e616e8c22b659e55986a539dd1 (patch)
tree7b13927e4c2eefab849e9fd487b896350f05576e
parentcbe164191c6c3510e225a43c935c6cc3e2da2cd4 (diff)
downloadlua-5519c986550677e616e8c22b659e55986a539dd1.tar.gz
lua-5519c986550677e616e8c22b659e55986a539dd1.tar.bz2
lua-5519c986550677e616e8c22b659e55986a539dd1.zip
'lua_cvtonum' -> 'lua_strtonum'; converts only strings to numbers
-rw-r--r--lapi.c31
-rw-r--r--lua.h4
2 files changed, 14 insertions, 21 deletions
diff --git a/lapi.c b/lapi.c
index e8e7104f..4ed815c7 100644
--- a/lapi.c
+++ b/lapi.c
@@ -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
335LUA_API int lua_cvtonum (lua_State *L, int idx) { 335LUA_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
diff --git a/lua.h b/lua.h
index 8e5531ff..ef623831 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.290 2013/06/07 14:51:10 roberto Exp roberto $ 2** $Id: lua.h,v 1.291 2013/06/07 19:01:50 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -307,7 +307,7 @@ LUA_API int (lua_next) (lua_State *L, int idx);
307LUA_API void (lua_concat) (lua_State *L, int n); 307LUA_API void (lua_concat) (lua_State *L, int n);
308LUA_API void (lua_len) (lua_State *L, int idx); 308LUA_API void (lua_len) (lua_State *L, int idx);
309 309
310LUA_API int (lua_cvtonum) (lua_State *L, int idx); 310LUA_API int (lua_strtonum) (lua_State *L, const char *s, size_t len);
311 311
312LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 312LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
313LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); 313LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);