aboutsummaryrefslogtreecommitdiff
path: root/src/lua/ltablib.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lua/ltablib.c (renamed from src/lua-5.3/ltablib.c)52
1 files changed, 15 insertions, 37 deletions
diff --git a/src/lua-5.3/ltablib.c b/src/lua/ltablib.c
index c534957..d344a47 100644
--- a/src/lua-5.3/ltablib.c
+++ b/src/lua/ltablib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $ 2** $Id: ltablib.c $
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*/
@@ -58,24 +58,6 @@ static void checktab (lua_State *L, int arg, int what) {
58} 58}
59 59
60 60
61#if defined(LUA_COMPAT_MAXN)
62static int maxn (lua_State *L) {
63 lua_Number max = 0;
64 luaL_checktype(L, 1, LUA_TTABLE);
65 lua_pushnil(L); /* first key */
66 while (lua_next(L, 1)) {
67 lua_pop(L, 1); /* remove value */
68 if (lua_type(L, -1) == LUA_TNUMBER) {
69 lua_Number v = lua_tonumber(L, -1);
70 if (v > max) max = v;
71 }
72 }
73 lua_pushnumber(L, max);
74 return 1;
75}
76#endif
77
78
79static int tinsert (lua_State *L) { 61static int tinsert (lua_State *L) {
80 lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ 62 lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
81 lua_Integer pos; /* where to insert new element */ 63 lua_Integer pos; /* where to insert new element */
@@ -87,7 +69,9 @@ static int tinsert (lua_State *L) {
87 case 3: { 69 case 3: {
88 lua_Integer i; 70 lua_Integer i;
89 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ 71 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
90 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); 72 /* check whether 'pos' is in [1, e] */
73 luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
74 "position out of bounds");
91 for (i = e; i > pos; i--) { /* move up elements */ 75 for (i = e; i > pos; i--) { /* move up elements */
92 lua_geti(L, 1, i - 1); 76 lua_geti(L, 1, i - 1);
93 lua_seti(L, 1, i); /* t[i] = t[i - 1] */ 77 lua_seti(L, 1, i); /* t[i] = t[i - 1] */
@@ -107,14 +91,16 @@ static int tremove (lua_State *L) {
107 lua_Integer size = aux_getn(L, 1, TAB_RW); 91 lua_Integer size = aux_getn(L, 1, TAB_RW);
108 lua_Integer pos = luaL_optinteger(L, 2, size); 92 lua_Integer pos = luaL_optinteger(L, 2, size);
109 if (pos != size) /* validate 'pos' if given */ 93 if (pos != size) /* validate 'pos' if given */
110 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); 94 /* check whether 'pos' is in [1, size + 1] */
95 luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1,
96 "position out of bounds");
111 lua_geti(L, 1, pos); /* result = t[pos] */ 97 lua_geti(L, 1, pos); /* result = t[pos] */
112 for ( ; pos < size; pos++) { 98 for ( ; pos < size; pos++) {
113 lua_geti(L, 1, pos + 1); 99 lua_geti(L, 1, pos + 1);
114 lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ 100 lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
115 } 101 }
116 lua_pushnil(L); 102 lua_pushnil(L);
117 lua_seti(L, 1, pos); /* t[pos] = nil */ 103 lua_seti(L, 1, pos); /* remove entry t[pos] */
118 return 1; 104 return 1;
119} 105}
120 106
@@ -191,7 +177,7 @@ static int tconcat (lua_State *L) {
191** ======================================================= 177** =======================================================
192*/ 178*/
193 179
194static int pack (lua_State *L) { 180static int tpack (lua_State *L) {
195 int i; 181 int i;
196 int n = lua_gettop(L); /* number of elements to pack */ 182 int n = lua_gettop(L); /* number of elements to pack */
197 lua_createtable(L, n, 1); /* create result table */ 183 lua_createtable(L, n, 1); /* create result table */
@@ -204,7 +190,7 @@ static int pack (lua_State *L) {
204} 190}
205 191
206 192
207static int unpack (lua_State *L) { 193static int tunpack (lua_State *L) {
208 lua_Unsigned n; 194 lua_Unsigned n;
209 lua_Integer i = luaL_optinteger(L, 2, 1); 195 lua_Integer i = luaL_optinteger(L, 2, 1);
210 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); 196 lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
@@ -313,14 +299,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
313 /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ 299 /* loop invariant: a[lo .. i] <= P <= a[j .. up] */
314 for (;;) { 300 for (;;) {
315 /* next loop: repeat ++i while a[i] < P */ 301 /* next loop: repeat ++i while a[i] < P */
316 while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { 302 while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
317 if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ 303 if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
318 luaL_error(L, "invalid order function for sorting"); 304 luaL_error(L, "invalid order function for sorting");
319 lua_pop(L, 1); /* remove a[i] */ 305 lua_pop(L, 1); /* remove a[i] */
320 } 306 }
321 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ 307 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */
322 /* next loop: repeat --j while P < a[j] */ 308 /* next loop: repeat --j while P < a[j] */
323 while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { 309 while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
324 if (j < i) /* j < i but a[j] > P ?? */ 310 if (j < i) /* j < i but a[j] > P ?? */
325 luaL_error(L, "invalid order function for sorting"); 311 luaL_error(L, "invalid order function for sorting");
326 lua_pop(L, 1); /* remove a[j] */ 312 lua_pop(L, 1); /* remove a[j] */
@@ -352,7 +338,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
352 338
353 339
354/* 340/*
355** QuickSort algorithm (recursive function) 341** Quicksort algorithm (recursive function)
356*/ 342*/
357static void auxsort (lua_State *L, IdxT lo, IdxT up, 343static void auxsort (lua_State *L, IdxT lo, IdxT up,
358 unsigned int rnd) { 344 unsigned int rnd) {
@@ -425,12 +411,9 @@ static int sort (lua_State *L) {
425 411
426static const luaL_Reg tab_funcs[] = { 412static const luaL_Reg tab_funcs[] = {
427 {"concat", tconcat}, 413 {"concat", tconcat},
428#if defined(LUA_COMPAT_MAXN)
429 {"maxn", maxn},
430#endif
431 {"insert", tinsert}, 414 {"insert", tinsert},
432 {"pack", pack}, 415 {"pack", tpack},
433 {"unpack", unpack}, 416 {"unpack", tunpack},
434 {"remove", tremove}, 417 {"remove", tremove},
435 {"move", tmove}, 418 {"move", tmove},
436 {"sort", sort}, 419 {"sort", sort},
@@ -440,11 +423,6 @@ static const luaL_Reg tab_funcs[] = {
440 423
441LUAMOD_API int luaopen_table (lua_State *L) { 424LUAMOD_API int luaopen_table (lua_State *L) {
442 luaL_newlib(L, tab_funcs); 425 luaL_newlib(L, tab_funcs);
443#if defined(LUA_COMPAT_UNPACK)
444 /* _G.unpack = table.unpack */
445 lua_getfield(L, -1, "unpack");
446 lua_setglobal(L, "unpack");
447#endif
448 return 1; 426 return 1;
449} 427}
450 428