diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-04 16:36:42 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-06-04 16:36:42 -0300 |
commit | 6fb0b1135090b1e4543438c56ae3e444abb5477d (patch) | |
tree | 95493cdd6da2474b448f31becc4434a72e354eae | |
parent | 932e7fb0e1b35758bd96361479ca3ddab3fc43da (diff) | |
download | lua-6fb0b1135090b1e4543438c56ae3e444abb5477d.tar.gz lua-6fb0b1135090b1e4543438c56ae3e444abb5477d.tar.bz2 lua-6fb0b1135090b1e4543438c56ae3e444abb5477d.zip |
string contatenation handles conversion of integers to strings +
floats always format as floats (with decimal dot or exponent)
-rw-r--r-- | lauxlib.c | 15 | ||||
-rw-r--r-- | lobject.c | 9 | ||||
-rw-r--r-- | lvm.c | 23 |
3 files changed, 25 insertions, 22 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.248 2013/03/21 13:54:57 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.249 2013/04/25 13:53:13 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 | */ |
@@ -737,15 +737,10 @@ LUALIB_API int luaL_len (lua_State *L, int idx) { | |||
737 | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { | 737 | LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { |
738 | if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ | 738 | if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ |
739 | switch (lua_type(L, idx)) { | 739 | switch (lua_type(L, idx)) { |
740 | case LUA_TNUMBER: { | 740 | case LUA_TNUMBER: { /* concatenate with empty string to convert */ |
741 | if (lua_isinteger(L, idx)) { | 741 | lua_pushvalue(L, idx); |
742 | lua_Integer n = lua_tointeger(L, idx); | 742 | lua_pushliteral(L, ""); |
743 | lua_pushfstring(L, "%I", n); | 743 | lua_concat(L, 2); |
744 | } | ||
745 | else { | ||
746 | lua_Number n = lua_tonumber(L, idx); | ||
747 | lua_pushfstring(L, "%f", n); | ||
748 | } | ||
749 | break; | 744 | break; |
750 | } | 745 | } |
751 | case LUA_TSTRING: | 746 | case LUA_TSTRING: |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.64 2013/05/26 14:43:35 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.65 2013/05/27 17:42:38 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -282,14 +282,11 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
282 | break; | 282 | break; |
283 | } | 283 | } |
284 | case 'd': { | 284 | case 'd': { |
285 | setivalue(L->top++, va_arg(argp, int)); | 285 | setivalue(L->top++, cast_int(va_arg(argp, int))); |
286 | break; | 286 | break; |
287 | } | 287 | } |
288 | case 'I': { | 288 | case 'I': { |
289 | char buff[LUA_MAXINTEGER2STR]; | 289 | setivalue(L->top++, cast_integer(va_arg(argp, lua_Integer))); |
290 | lua_Integer i = cast(lua_Integer, va_arg(argp, lua_Integer)); | ||
291 | int l = lua_integer2str(buff, i); | ||
292 | pushstr(L, buff, l); | ||
293 | break; | 290 | break; |
294 | } | 291 | } |
295 | case 'f': { | 292 | case 'f': { |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.170 2013/05/26 14:47:51 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.171 2013/05/27 12:43:37 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -32,6 +32,10 @@ | |||
32 | #define MAXTAGLOOP 100 | 32 | #define MAXTAGLOOP 100 |
33 | 33 | ||
34 | 34 | ||
35 | /* maximum length of the conversion of a number to a string */ | ||
36 | #define MAXNUMBER2STR 50 | ||
37 | |||
38 | |||
35 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | 39 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { |
36 | lua_assert(!ttisfloat(obj)); | 40 | lua_assert(!ttisfloat(obj)); |
37 | if (ttisinteger(obj)) { | 41 | if (ttisinteger(obj)) { |
@@ -47,12 +51,19 @@ int luaV_tostring (lua_State *L, StkId obj) { | |||
47 | if (!ttisnumber(obj)) | 51 | if (!ttisnumber(obj)) |
48 | return 0; | 52 | return 0; |
49 | else { | 53 | else { |
50 | char s[LUAI_MAXNUMBER2STR]; | 54 | char buff[MAXNUMBER2STR]; |
51 | lua_Number n; | ||
52 | int len; | 55 | int len; |
53 | (void)tonumber(obj, &n); | 56 | if (ttisinteger(obj)) |
54 | len = lua_number2str(s, n); | 57 | len = lua_integer2str(buff, ivalue(obj)); |
55 | setsvalue2s(L, obj, luaS_newlstr(L, s, len)); | 58 | else { |
59 | len = lua_number2str(buff, fltvalue(obj)); | ||
60 | if (strpbrk(buff, ".eE") == NULL) { /* no marks that it is a float? */ | ||
61 | buff[len++] = '.'; /* add a '.0' */ | ||
62 | buff[len++] = '0'; | ||
63 | buff[len] = '\0'; | ||
64 | } | ||
65 | } | ||
66 | setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); | ||
56 | return 1; | 67 | return 1; |
57 | } | 68 | } |
58 | } | 69 | } |