aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-29 17:06:05 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-29 17:06:05 -0300
commitb123a88673fcf37ebfcc8ea866a3d27da6cdf82e (patch)
treeb0ea9d9fe0e28ae39d5202378ed7ace21a23af0f
parentffa43df3cd0593a3fb92fe0411a81a9ae6eb3c1d (diff)
downloadlua-b123a88673fcf37ebfcc8ea866a3d27da6cdf82e.tar.gz
lua-b123a88673fcf37ebfcc8ea866a3d27da6cdf82e.tar.bz2
lua-b123a88673fcf37ebfcc8ea866a3d27da6cdf82e.zip
merge of common parts from 'limittointeger' and 'luaV_tointeger_'
-rw-r--r--lvm.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/lvm.c b/lvm.c
index 17c34ee6..7c4a0acf 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.200 2014/04/29 18:11:57 roberto Exp roberto $ 2** $Id: lvm.c,v 2.201 2014/04/29 18:14:16 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*/
@@ -97,13 +97,14 @@ int luaV_numtointeger (lua_Number n, lua_Integer *p) {
97 97
98 98
99/* 99/*
100** try to convert a non-integer value to an integer 100** try to convert a non-integer value to an integer, rounding up if
101** 'up' is true
101*/ 102*/
102int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { 103static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
103 lua_Number n; 104 lua_Number n;
104 lua_assert(!ttisinteger(obj)); 105 lua_assert(!ttisinteger(obj));
105 if (tonumber(obj, &n)) { 106 if (tonumber(obj, &n)) {
106 n = l_floor(n); 107 n = (up ? -l_floor(-n) : l_floor(n));
107 return luaV_numtointeger(n, p); 108 return luaV_numtointeger(n, p);
108 } 109 }
109 else return 0; 110 else return 0;
@@ -111,6 +112,14 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
111 112
112 113
113/* 114/*
115** try to convert a non-integer value to an integer, rounding down
116*/
117int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
118 return tointeger_aux(obj, p, 0);
119}
120
121
122/*
114** Check whether the limit of a 'for' loop can be safely converted 123** Check whether the limit of a 'for' loop can be safely converted
115** to an integer (rounding down or up depending on the signal of 'step') 124** to an integer (rounding down or up depending on the signal of 'step')
116*/ 125*/
@@ -119,12 +128,8 @@ static int limittointeger (const TValue *n, lua_Integer *p, lua_Integer step) {
119 *p = ivalue(n); 128 *p = ivalue(n);
120 return 1; 129 return 1;
121 } 130 }
122 else if (!ttisfloat(n)) return 0; 131 else
123 else { 132 return tointeger_aux(n, p, (step < 0));
124 lua_Number f = fltvalue(n);
125 f = (step >= 0) ? l_floor(f) : -l_floor(-f);
126 return luaV_numtointeger(f, p);
127 }
128} 133}
129 134
130 135