mirror of
https://github.com/lua/lua
synced 2024-12-26 04:16:52 +03:00
string contatenation handles conversion of integers to strings +
floats always format as floats (with decimal dot or exponent)
This commit is contained in:
parent
932e7fb0e1
commit
6fb0b11350
15
lauxlib.c
15
lauxlib.c
@ -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
|
||||
** 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) {
|
||||
if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
|
||||
switch (lua_type(L, idx)) {
|
||||
case LUA_TNUMBER: {
|
||||
if (lua_isinteger(L, idx)) {
|
||||
lua_Integer n = lua_tointeger(L, idx);
|
||||
lua_pushfstring(L, "%I", n);
|
||||
}
|
||||
else {
|
||||
lua_Number n = lua_tonumber(L, idx);
|
||||
lua_pushfstring(L, "%f", n);
|
||||
}
|
||||
case LUA_TNUMBER: { /* concatenate with empty string to convert */
|
||||
lua_pushvalue(L, idx);
|
||||
lua_pushliteral(L, "");
|
||||
lua_concat(L, 2);
|
||||
break;
|
||||
}
|
||||
case LUA_TSTRING:
|
||||
|
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -282,14 +282,11 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
|
||||
break;
|
||||
}
|
||||
case 'd': {
|
||||
setivalue(L->top++, va_arg(argp, int));
|
||||
setivalue(L->top++, cast_int(va_arg(argp, int)));
|
||||
break;
|
||||
}
|
||||
case 'I': {
|
||||
char buff[LUA_MAXINTEGER2STR];
|
||||
lua_Integer i = cast(lua_Integer, va_arg(argp, lua_Integer));
|
||||
int l = lua_integer2str(buff, i);
|
||||
pushstr(L, buff, l);
|
||||
setivalue(L->top++, cast_integer(va_arg(argp, lua_Integer)));
|
||||
break;
|
||||
}
|
||||
case 'f': {
|
||||
|
23
lvm.c
23
lvm.c
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -32,6 +32,10 @@
|
||||
#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) {
|
||||
lua_assert(!ttisfloat(obj));
|
||||
if (ttisinteger(obj)) {
|
||||
@ -47,12 +51,19 @@ int luaV_tostring (lua_State *L, StkId obj) {
|
||||
if (!ttisnumber(obj))
|
||||
return 0;
|
||||
else {
|
||||
char s[LUAI_MAXNUMBER2STR];
|
||||
lua_Number n;
|
||||
char buff[MAXNUMBER2STR];
|
||||
int len;
|
||||
(void)tonumber(obj, &n);
|
||||
len = lua_number2str(s, n);
|
||||
setsvalue2s(L, obj, luaS_newlstr(L, s, len));
|
||||
if (ttisinteger(obj))
|
||||
len = lua_integer2str(buff, ivalue(obj));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user