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 /lvm.c | |
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)
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 23 |
1 files changed, 17 insertions, 6 deletions
@@ -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 | } |