diff options
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 16 |
1 files changed, 11 insertions, 5 deletions
| @@ -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) { | |||
| 746 | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { | 746 | LUALIB_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: |
