Browse Source

insertion of ".0" in floats with integer values done by "luaL_tolstring",
not by the core

Roberto Ierusalimschy 11 năm trước cách đây
mục cha
commit
d438e1379d
2 tập tin đã thay đổi với 15 bổ sung17 xóa
  1. 11 5
      lauxlib.c
  2. 4 12
      lvm.c

+ 11 - 5
lauxlib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lauxlib.c,v 1.255 2013/06/27 18:32:33 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.256 2014/01/05 14:04:46 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -746,10 +746,16 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
 LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
 LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
   if (!luaL_callmeta(L, idx, "__tostring")) {  /* no metafield? */
   if (!luaL_callmeta(L, idx, "__tostring")) {  /* no metafield? */
     switch (lua_type(L, idx)) {
     switch (lua_type(L, idx)) {
-      case LUA_TNUMBER: {  /* concatenate with empty string to convert */
-        lua_pushvalue(L, idx);
-        lua_pushliteral(L, "");
-        lua_concat(L, 2);
+      case LUA_TNUMBER: {
+        if (lua_isinteger(L, idx))
+          lua_pushfstring(L, "%I", lua_tointeger(L, idx));
+        else {
+          const char *s = lua_pushfstring(L, "%f", lua_tonumber(L, idx));
+          if (s[strspn(s, "-0123456789")] == '\0') {  /* looks like an int? */
+            lua_pushliteral(L, ".0");  /* add a '.0' to result */
+            lua_concat(L, 2);
+          }
+        }
         break;
         break;
       }
       }
       case LUA_TSTRING:
       case LUA_TSTRING:

+ 4 - 12
lvm.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lvm.c,v 2.184 2014/01/22 20:02:04 roberto Exp roberto $
+** $Id: lvm.c,v 2.185 2014/01/27 13:34:32 roberto Exp roberto $
 ** Lua virtual machine
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -53,17 +53,9 @@ int luaV_tostring (lua_State *L, StkId obj) {
     return 0;
     return 0;
   else {
   else {
     char buff[MAXNUMBER2STR];
     char buff[MAXNUMBER2STR];
-    size_t len;
-    if (ttisinteger(obj))
-      len = lua_integer2str(buff, ivalue(obj));
-    else {
-      len = lua_number2str(buff, fltvalue(obj));
-      if (strspn(buff, "-0123456789") == len) {  /* look like an integer? */
-        buff[len++] = '.';  /* add a '.0' */
-        buff[len++] = '0';
-        buff[len] = '\0';
-      }
-    }
+    size_t len = (ttisinteger(obj))
+                 ? lua_integer2str(buff, ivalue(obj))
+                 : lua_number2str(buff, fltvalue(obj));
     setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
     setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
     return 1;
     return 1;
   }
   }