diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-04-30 13:50:16 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-04-30 13:50:16 -0300 |
commit | 1a6ef098b0edbb495afb8cfe4e4cdb434eed50a6 (patch) | |
tree | 20980dea5be8a7cb16ba6694087f13fc730b7b22 /lvm.c | |
parent | 5388aa9fc0185b5d7a852e679484e26ba53e5d4d (diff) | |
download | lua-1a6ef098b0edbb495afb8cfe4e4cdb434eed50a6.tar.gz lua-1a6ef098b0edbb495afb8cfe4e4cdb434eed50a6.tar.bz2 lua-1a6ef098b0edbb495afb8cfe4e4cdb434eed50a6.zip |
'luaO_str2d' + 'luaO_str2int' replaced by 'luaO_str2num' plus proper
conversion from strings to integers
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 68 |
1 files changed, 46 insertions, 22 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.201 2014/04/29 18:14:16 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.202 2014/04/29 20:06:05 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 | */ |
@@ -55,28 +55,26 @@ static int tofloat (const TValue *obj, lua_Number *n) { | |||
55 | } | 55 | } |
56 | 56 | ||
57 | 57 | ||
58 | /* | ||
59 | ** Try to convert a value to a float | ||
60 | */ | ||
58 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | 61 | int luaV_tonumber_ (const TValue *obj, lua_Number *n) { |
59 | lua_assert(!ttisfloat(obj)); | 62 | TValue v; |
63 | again: | ||
60 | if (ttisinteger(obj)) { | 64 | if (ttisinteger(obj)) { |
61 | *n = cast_num(ivalue(obj)); | 65 | *n = cast_num(ivalue(obj)); |
62 | return 1; | 66 | return 1; |
63 | } | 67 | } |
64 | else | 68 | else if (ttisfloat(obj)) { |
65 | return (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, n)); | 69 | *n = fltvalue(obj); |
66 | } | ||
67 | |||
68 | |||
69 | int luaV_tostring (lua_State *L, StkId obj) { | ||
70 | if (!ttisnumber(obj)) | ||
71 | return 0; | ||
72 | else { | ||
73 | char buff[MAXNUMBER2STR]; | ||
74 | size_t len = (ttisinteger(obj)) | ||
75 | ? lua_integer2str(buff, ivalue(obj)) | ||
76 | : lua_number2str(buff, fltvalue(obj)); | ||
77 | setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); | ||
78 | return 1; | 70 | return 1; |
79 | } | 71 | } |
72 | else if (ttisstring(obj) && | ||
73 | luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) { | ||
74 | obj = &v; | ||
75 | goto again; /* convert result from 'luaO_str2num' to a float */ | ||
76 | } | ||
77 | return 0; /* conversion failed */ | ||
80 | } | 78 | } |
81 | 79 | ||
82 | 80 | ||
@@ -97,17 +95,26 @@ int luaV_numtointeger (lua_Number n, lua_Integer *p) { | |||
97 | 95 | ||
98 | 96 | ||
99 | /* | 97 | /* |
100 | ** try to convert a non-integer value to an integer, rounding up if | 98 | ** try to convert a value to an integer, rounding up if 'up' is true |
101 | ** 'up' is true | ||
102 | */ | 99 | */ |
103 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { | 100 | static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { |
104 | lua_Number n; | 101 | TValue v; |
105 | lua_assert(!ttisinteger(obj)); | 102 | again: |
106 | if (tonumber(obj, &n)) { | 103 | if (ttisfloat(obj)) { |
104 | lua_Number n = fltvalue(obj); | ||
107 | n = (up ? -l_floor(-n) : l_floor(n)); | 105 | n = (up ? -l_floor(-n) : l_floor(n)); |
108 | return luaV_numtointeger(n, p); | 106 | return luaV_numtointeger(n, p); |
109 | } | 107 | } |
110 | else return 0; | 108 | else if (ttisinteger(obj)) { |
109 | *p = ivalue(obj); | ||
110 | return 1; | ||
111 | } | ||
112 | if (ttisstring(obj) && | ||
113 | luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) { | ||
114 | obj = &v; | ||
115 | goto again; /* convert result from 'luaO_str2num' to an integer */ | ||
116 | } | ||
117 | return 0; /* conversion failed */ | ||
111 | } | 118 | } |
112 | 119 | ||
113 | 120 | ||
@@ -133,6 +140,23 @@ static int limittointeger (const TValue *n, lua_Integer *p, lua_Integer step) { | |||
133 | } | 140 | } |
134 | 141 | ||
135 | 142 | ||
143 | /* | ||
144 | ** Convert a number object to a string | ||
145 | */ | ||
146 | int luaV_tostring (lua_State *L, StkId obj) { | ||
147 | if (!ttisnumber(obj)) | ||
148 | return 0; | ||
149 | else { | ||
150 | char buff[MAXNUMBER2STR]; | ||
151 | size_t len = (ttisinteger(obj)) | ||
152 | ? lua_integer2str(buff, ivalue(obj)) | ||
153 | : lua_number2str(buff, fltvalue(obj)); | ||
154 | setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); | ||
155 | return 1; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | |||
136 | void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { | 160 | void luaV_gettable (lua_State *L, const TValue *t, TValue *key, StkId val) { |
137 | int loop; | 161 | int loop; |
138 | for (loop = 0; loop < MAXTAGLOOP; loop++) { | 162 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |