aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-05-10 15:06:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-05-10 15:06:14 -0300
commitb072e4ea0ba72a78712f9a10f25c7266f906d5c5 (patch)
treeff11180d86e13aea3f6d7bb666a5f0623cccb110 /ltablib.c
parent7e41612eb28ff0fba282bd419781fcbc9bd94776 (diff)
downloadlua-b072e4ea0ba72a78712f9a10f25c7266f906d5c5.tar.gz
lua-b072e4ea0ba72a78712f9a10f25c7266f906d5c5.tar.bz2
lua-b072e4ea0ba72a78712f9a10f25c7266f906d5c5.zip
`tinsert' and `tremove' also use LUA_FIRSTINDEX
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c28
1 files changed, 14 insertions, 14 deletions
diff --git a/ltablib.c b/ltablib.c
index 3e35857e..f051742d 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltablib.c,v 1.23 2004/04/30 20:13:38 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.24 2004/05/10 17:50:51 roberto Exp roberto $
3** Library for Table Manipulation 3** Library for Table Manipulation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -69,19 +69,19 @@ static int luaB_setn (lua_State *L) {
69 69
70static int luaB_tinsert (lua_State *L) { 70static int luaB_tinsert (lua_State *L) {
71 int v = lua_gettop(L); /* number of arguments */ 71 int v = lua_gettop(L); /* number of arguments */
72 int n = aux_getn(L, 1) + 1; 72 int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */
73 int pos; /* where to insert new element */ 73 int pos; /* where to insert new element */
74 if (v == 2) /* called with only 2 arguments */ 74 if (v == 2) /* called with only 2 arguments */
75 pos = n; /* insert new element at the end */ 75 pos = e; /* insert new element at the end */
76 else { 76 else {
77 pos = luaL_checkint(L, 2); /* 2nd argument is the position */ 77 pos = luaL_checkint(L, 2); /* 2nd argument is the position */
78 if (pos > n) n = pos; /* `grow' array if necessary */ 78 if (pos > e) e = pos; /* `grow' array if necessary */
79 v = 3; /* function may be called with more than 3 args */ 79 v = 3; /* function may be called with more than 3 args */
80 } 80 }
81 luaL_setn(L, 1, n); /* new size */ 81 luaL_setn(L, 1, e - LUA_FIRSTINDEX + 1); /* new size */
82 while (--n >= pos) { /* move up elements */ 82 while (--e >= pos) { /* move up elements */
83 lua_rawgeti(L, 1, n); 83 lua_rawgeti(L, 1, e);
84 lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */ 84 lua_rawseti(L, 1, e+1); /* t[e+1] = t[e] */
85 } 85 }
86 lua_pushvalue(L, v); 86 lua_pushvalue(L, v);
87 lua_rawseti(L, 1, pos); /* t[pos] = v */ 87 lua_rawseti(L, 1, pos); /* t[pos] = v */
@@ -90,17 +90,17 @@ static int luaB_tinsert (lua_State *L) {
90 90
91 91
92static int luaB_tremove (lua_State *L) { 92static int luaB_tremove (lua_State *L) {
93 int n = aux_getn(L, 1); 93 int e = aux_getn(L, 1) + LUA_FIRSTINDEX - 1;
94 int pos = luaL_optint(L, 2, n); 94 int pos = luaL_optint(L, 2, e);
95 if (n <= 0) return 0; /* table is `empty' */ 95 if (e < LUA_FIRSTINDEX) return 0; /* table is `empty' */
96 luaL_setn(L, 1, n-1); /* t.n = n-1 */ 96 luaL_setn(L, 1, e - LUA_FIRSTINDEX); /* t.n = n-1 */
97 lua_rawgeti(L, 1, pos); /* result = t[pos] */ 97 lua_rawgeti(L, 1, pos); /* result = t[pos] */
98 for ( ;pos<n; pos++) { 98 for ( ;pos<e; pos++) {
99 lua_rawgeti(L, 1, pos+1); 99 lua_rawgeti(L, 1, pos+1);
100 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ 100 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
101 } 101 }
102 lua_pushnil(L); 102 lua_pushnil(L);
103 lua_rawseti(L, 1, n); /* t[n] = nil */ 103 lua_rawseti(L, 1, e); /* t[e] = nil */
104 return 1; 104 return 1;
105} 105}
106 106