From d438e1379d24f06f027f8fc0e8fc1ff6673b322f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 5 Feb 2014 17:14:53 -0200 Subject: insertion of ".0" in floats with integer values done by "luaL_tolstring", not by the core --- lauxlib.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'lauxlib.c') 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: -- cgit v1.2.3-55-g6feb