addition of '.0' to float representation done by the kernel

This commit is contained in:
Roberto Ierusalimschy 2014-05-12 18:44:17 -03:00
parent 27d9219cf3
commit 45c430eac0
3 changed files with 18 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lauxlib.c,v 1.261 2014/04/01 18:55:06 roberto Exp roberto $
** $Id: lauxlib.c,v 1.262 2014/04/15 18:25:49 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@ -757,13 +757,8 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
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);
}
}
else
lua_pushfstring(L, "%f", lua_tonumber(L, idx));
break;
}
case LUA_TSTRING:

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.84 2014/05/01 18:18:06 roberto Exp roberto $
** $Id: lobject.c,v 2.85 2014/05/12 21:22:05 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -356,14 +356,17 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
}
case 'd': {
setivalue(L->top++, cast_int(va_arg(argp, int)));
luaV_tostring(L, L->top - 1);
break;
}
case 'I': {
setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
luaV_tostring(L, L->top - 1);
break;
}
case 'f': {
setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
luaV_tostring(L, L->top - 1);
break;
}
case 'p': {

15
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.207 2014/05/12 19:13:32 roberto Exp roberto $
** $Id: lvm.c,v 2.208 2014/05/12 21:22:05 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -135,9 +135,16 @@ int luaV_tostring (lua_State *L, StkId obj) {
return 0;
else {
char buff[MAXNUMBER2STR];
size_t len = (ttisinteger(obj))
? lua_integer2str(buff, ivalue(obj))
: lua_number2str(buff, fltvalue(obj));
size_t len;
if (ttisinteger(obj))
len = lua_integer2str(buff, ivalue(obj));
else {
len = lua_number2str(buff, fltvalue(obj));
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
buff[len++] = '.';
buff[len++] = '0'; /* adds '.0' to result */
}
}
setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
return 1;
}