diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-21 09:54:26 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-21 09:54:26 -0300 |
commit | 465b47489910c8660f7d8f9869ffacdbb2d5d292 (patch) | |
tree | 3da1e80d05496bf48515dd71c55f14cdcb252ed1 /lvm.c | |
parent | 6353d619a594b5dfbef5dad10af9d4c051878466 (diff) | |
download | lua-465b47489910c8660f7d8f9869ffacdbb2d5d292.tar.gz lua-465b47489910c8660f7d8f9869ffacdbb2d5d292.tar.bz2 lua-465b47489910c8660f7d8f9869ffacdbb2d5d292.zip |
small reorganization of 'luaV_flttointeger'/'luaV_tointeger'
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 48 |
1 files changed, 27 insertions, 21 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.341 2018/02/17 19:20:00 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.342 2018/02/19 20:06:56 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 | */ |
@@ -90,36 +90,42 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |||
90 | ** mode == 1: takes the floor of the number | 90 | ** mode == 1: takes the floor of the number |
91 | ** mode == 2: takes the ceil of the number | 91 | ** mode == 2: takes the ceil of the number |
92 | */ | 92 | */ |
93 | int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) { | 93 | int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { |
94 | if (!ttisfloat(obj)) | 94 | lua_Number f = l_floor(n); |
95 | return 0; | 95 | if (n != f) { /* not an integral value? */ |
96 | else { | 96 | if (mode == 0) return 0; /* fails if mode demands integral value */ |
97 | lua_Number n = fltvalue(obj); | 97 | else if (mode > 1) /* needs ceil? */ |
98 | lua_Number f = l_floor(n); | 98 | f += 1; /* convert floor to ceil (remember: n != f) */ |
99 | if (n != f) { /* not an integral value? */ | ||
100 | if (mode == 0) return 0; /* fails if mode demands integral value */ | ||
101 | else if (mode > 1) /* needs ceil? */ | ||
102 | f += 1; /* convert floor to ceil (remember: n != f) */ | ||
103 | } | ||
104 | return lua_numbertointeger(f, p); | ||
105 | } | 99 | } |
100 | return lua_numbertointeger(f, p); | ||
106 | } | 101 | } |
107 | 102 | ||
108 | 103 | ||
109 | /* | 104 | /* |
110 | ** try to convert a value to an integer. ("Fast track" is handled | 105 | ** try to convert a value to an integer, rounding according to 'mode', |
111 | ** by macro 'tointeger'.) | 106 | ** without string coercion. |
107 | ** ("Fast track" handled by macro 'tointegerns'.) | ||
112 | */ | 108 | */ |
113 | int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { | 109 | int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { |
114 | TValue v; | 110 | if (ttisfloat(obj)) |
115 | if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) | 111 | return luaV_flttointeger(fltvalue(obj), p, mode); |
116 | obj = &v; /* change string to its corresponding number */ | 112 | else if (ttisinteger(obj)) { |
117 | if (ttisinteger(obj)) { | ||
118 | *p = ivalue(obj); | 113 | *p = ivalue(obj); |
119 | return 1; | 114 | return 1; |
120 | } | 115 | } |
121 | else | 116 | else |
122 | return luaV_flttointeger(obj, p, mode); | 117 | return 0; |
118 | } | ||
119 | |||
120 | |||
121 | /* | ||
122 | ** try to convert a value to an integer. | ||
123 | */ | ||
124 | int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { | ||
125 | TValue v; | ||
126 | if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) | ||
127 | obj = &v; /* change string to its corresponding number */ | ||
128 | return luaV_tointegerns(obj, p, mode); | ||
123 | } | 129 | } |
124 | 130 | ||
125 | 131 | ||