string contatenation handles conversion of integers to strings +

floats always format as floats (with decimal dot or exponent)
This commit is contained in:
Roberto Ierusalimschy 2013-06-04 16:36:42 -03:00
parent 932e7fb0e1
commit 6fb0b11350
3 changed files with 25 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.248 2013/03/21 13:54:57 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.249 2013/04/25 13:53:13 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -737,15 +737,10 @@ LUALIB_API int luaL_len (lua_State *L, int idx) {
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
switch (lua_type(L, idx)) { switch (lua_type(L, idx)) {
case LUA_TNUMBER: { case LUA_TNUMBER: { /* concatenate with empty string to convert */
if (lua_isinteger(L, idx)) { lua_pushvalue(L, idx);
lua_Integer n = lua_tointeger(L, idx); lua_pushliteral(L, "");
lua_pushfstring(L, "%I", n); lua_concat(L, 2);
}
else {
lua_Number n = lua_tonumber(L, idx);
lua_pushfstring(L, "%f", n);
}
break; break;
} }
case LUA_TSTRING: case LUA_TSTRING:

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.64 2013/05/26 14:43:35 roberto Exp roberto $ ** $Id: lobject.c,v 2.65 2013/05/27 17:42:38 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -282,14 +282,11 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
break; break;
} }
case 'd': { case 'd': {
setivalue(L->top++, va_arg(argp, int)); setivalue(L->top++, cast_int(va_arg(argp, int)));
break; break;
} }
case 'I': { case 'I': {
char buff[LUA_MAXINTEGER2STR]; setivalue(L->top++, cast_integer(va_arg(argp, lua_Integer)));
lua_Integer i = cast(lua_Integer, va_arg(argp, lua_Integer));
int l = lua_integer2str(buff, i);
pushstr(L, buff, l);
break; break;
} }
case 'f': { case 'f': {

23
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.170 2013/05/26 14:47:51 roberto Exp roberto $ ** $Id: lvm.c,v 2.171 2013/05/27 12:43:37 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -32,6 +32,10 @@
#define MAXTAGLOOP 100 #define MAXTAGLOOP 100
/* maximum length of the conversion of a number to a string */
#define MAXNUMBER2STR 50
int luaV_tonumber_ (const TValue *obj, lua_Number *n) { int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
lua_assert(!ttisfloat(obj)); lua_assert(!ttisfloat(obj));
if (ttisinteger(obj)) { if (ttisinteger(obj)) {
@ -47,12 +51,19 @@ int luaV_tostring (lua_State *L, StkId obj) {
if (!ttisnumber(obj)) if (!ttisnumber(obj))
return 0; return 0;
else { else {
char s[LUAI_MAXNUMBER2STR]; char buff[MAXNUMBER2STR];
lua_Number n;
int len; int len;
(void)tonumber(obj, &n); if (ttisinteger(obj))
len = lua_number2str(s, n); len = lua_integer2str(buff, ivalue(obj));
setsvalue2s(L, obj, luaS_newlstr(L, s, len)); else {
len = lua_number2str(buff, fltvalue(obj));
if (strpbrk(buff, ".eE") == NULL) { /* no marks that it is a float? */
buff[len++] = '.'; /* add a '.0' */
buff[len++] = '0';
buff[len] = '\0';
}
}
setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
return 1; return 1;
} }
} }