aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c40
1 files changed, 39 insertions, 1 deletions
diff --git a/ltablib.c b/ltablib.c
index dca2bc8e..5d27cb2f 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltablib.c,v 1.70 2014/05/16 18:53:25 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.71 2014/07/16 12:44:52 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*/
@@ -132,6 +132,43 @@ static int tremove (lua_State *L) {
132} 132}
133 133
134 134
135static int tcopy (lua_State *L) {
136 TabA ta;
137 lua_Integer f = luaL_checkinteger(L, 2);
138 lua_Integer e = luaL_checkinteger(L, 3);
139 lua_Integer t;
140 int tt = 4; /* destination table */
141 /* the following restriction avoids several problems with overflows */
142 luaL_argcheck(L, f > 0, 2, "initial position must be positive");
143 if (lua_istable(L, tt))
144 t = luaL_checkinteger(L, 5);
145 else {
146 tt = 1; /* destination table is equal to source */
147 t = luaL_checkinteger(L, 4);
148 }
149 if (e >= f) { /* otherwise, nothing to move */
150 lua_Integer n, i;
151 ta.geti = (!luaL_getmetafield(L, 1, "__index")) ? lua_rawgeti : geti;
152 ta.seti = (!luaL_getmetafield(L, tt, "__newindex")) ? lua_rawseti : seti;
153 n = e - f + 1; /* number of elements to move */
154 if (t > f) {
155 for (i = n - 1; i >= 0; i--) {
156 (*ta.geti)(L, 1, f + i);
157 (*ta.seti)(L, tt, t + i);
158 }
159 }
160 else {
161 for (i = 0; i < n; i++) {
162 (*ta.geti)(L, 1, f + i);
163 (*ta.seti)(L, tt, t + i);
164 }
165 }
166 }
167 lua_pushvalue(L, tt); /* return "to table" */
168 return 1;
169}
170
171
135static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { 172static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) {
136 (*ta->geti)(L, 1, i); 173 (*ta->geti)(L, 1, i);
137 if (!lua_isstring(L, -1)) 174 if (!lua_isstring(L, -1))
@@ -318,6 +355,7 @@ static const luaL_Reg tab_funcs[] = {
318 {"pack", pack}, 355 {"pack", pack},
319 {"unpack", unpack}, 356 {"unpack", unpack},
320 {"remove", tremove}, 357 {"remove", tremove},
358 {"copy", tcopy},
321 {"sort", sort}, 359 {"sort", sort},
322 {NULL, NULL} 360 {NULL, NULL}
323}; 361};