Browse Source

'lua_cvtonum' -> 'lua_strtonum'; converts only strings to numbers

Roberto Ierusalimschy 12 years ago
parent
commit
5519c98655
2 changed files with 14 additions and 21 deletions
  1. 12 19
      lapi.c
  2. 2 2
      lua.h

+ 12 - 19
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.183 2013/06/20 15:02:49 roberto Exp roberto $
+** $Id: lapi.c,v 2.184 2013/06/20 15:12:43 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -332,25 +332,18 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
 }
 
 
-LUA_API int lua_cvtonum (lua_State *L, int idx) {
-  TValue *o = index2addr(L, idx);
-  if (ttisnumber(o)) return 1;  /* already a number? */
-  else if (!ttisstring(o)) return 0;  /* only strings can be converted */
-  else {
-    lua_Integer i; lua_Number n;
-    const char *s = svalue(o);
-    size_t len = tsvalue(o)->len;
-    if (luaO_str2int(s, len, &i)) {
-      setivalue(o, i);
-      return 1;
-    }
-    else if (luaO_str2d(s, len, &n)) {
-      setivalue(o, i);
-      setnvalue(o, n);
-      return 1;
-    }
-    else return 0;
+LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) {
+  lua_Integer i; lua_Number n;
+  if (luaO_str2int(s, len, &i)) {  /* try as an integer */
+    setivalue(L->top, i);
+  }
+  else if (luaO_str2d(s, len, &n)) {  /* else try as a float */
+    setnvalue(L->top, n);
   }
+  else
+    return 0;  /* conversion failed */
+  api_incr_top(L);
+  return 1;
 }
 
 

+ 2 - 2
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.290 2013/06/07 14:51:10 roberto Exp roberto $
+** $Id: lua.h,v 1.291 2013/06/07 19:01:50 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
@@ -307,7 +307,7 @@ LUA_API int   (lua_next) (lua_State *L, int idx);
 LUA_API void  (lua_concat) (lua_State *L, int n);
 LUA_API void  (lua_len)    (lua_State *L, int idx);
 
-LUA_API int   (lua_cvtonum) (lua_State *L, int idx);
+LUA_API int   (lua_strtonum) (lua_State *L, const char *s, size_t len);
 
 LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
 LUA_API void      (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);