diff --git a/lauxlib.c b/lauxlib.c index 035cb06c..afed0fe1 100644 --- a/lauxlib.c +++ b/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 ** 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) { if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ 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; } 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 @@ /* -** $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 ** See Copyright Notice in lua.h */ @@ -53,17 +53,9 @@ int luaV_tostring (lua_State *L, StkId obj) { return 0; else { 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)); return 1; }