summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-02-05 17:14:53 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-02-05 17:14:53 -0200
commitd438e1379d24f06f027f8fc0e8fc1ff6673b322f (patch)
treeb8ba7c184768198d6b7ade4c65968f644633baa5
parent1721d09ac8e55bb4b675cea26cbe6b707fd21a2c (diff)
downloadlua-d438e1379d24f06f027f8fc0e8fc1ff6673b322f.tar.gz
lua-d438e1379d24f06f027f8fc0e8fc1ff6673b322f.tar.bz2
lua-d438e1379d24f06f027f8fc0e8fc1ff6673b322f.zip
insertion of ".0" in floats with integer values done by "luaL_tolstring",
not by the core
-rw-r--r--lauxlib.c16
-rw-r--r--lvm.c16
2 files changed, 15 insertions, 17 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 035cb06c..afed0fe1 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.255 2013/06/27 18:32:33 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.256 2014/01/05 14:04:46 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -746,10 +746,16 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
746LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { 746LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
747 if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ 747 if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
748 switch (lua_type(L, idx)) { 748 switch (lua_type(L, idx)) {
749 case LUA_TNUMBER: { /* concatenate with empty string to convert */ 749 case LUA_TNUMBER: {
750 lua_pushvalue(L, idx); 750 if (lua_isinteger(L, idx))
751 lua_pushliteral(L, ""); 751 lua_pushfstring(L, "%I", lua_tointeger(L, idx));
752 lua_concat(L, 2); 752 else {
753 const char *s = lua_pushfstring(L, "%f", lua_tonumber(L, idx));
754 if (s[strspn(s, "-0123456789")] == '\0') { /* looks like an int? */
755 lua_pushliteral(L, ".0"); /* add a '.0' to result */
756 lua_concat(L, 2);
757 }
758 }
753 break; 759 break;
754 } 760 }
755 case LUA_TSTRING: 761 case LUA_TSTRING:
diff --git a/lvm.c b/lvm.c
index 998cea9f..835e8e0f 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.184 2014/01/22 20:02:04 roberto Exp roberto $ 2** $Id: lvm.c,v 2.185 2014/01/27 13:34:32 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -53,17 +53,9 @@ int luaV_tostring (lua_State *L, StkId obj) {
53 return 0; 53 return 0;
54 else { 54 else {
55 char buff[MAXNUMBER2STR]; 55 char buff[MAXNUMBER2STR];
56 size_t len; 56 size_t len = (ttisinteger(obj))
57 if (ttisinteger(obj)) 57 ? lua_integer2str(buff, ivalue(obj))
58 len = lua_integer2str(buff, ivalue(obj)); 58 : lua_number2str(buff, fltvalue(obj));
59 else {
60 len = lua_number2str(buff, fltvalue(obj));
61 if (strspn(buff, "-0123456789") == len) { /* look like an integer? */
62 buff[len++] = '.'; /* add a '.0' */
63 buff[len++] = '0';
64 buff[len] = '\0';
65 }
66 }
67 setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); 59 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
68 return 1; 60 return 1;
69 } 61 }