aboutsummaryrefslogtreecommitdiff
path: root/src/ltablib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltablib.c')
-rw-r--r--src/ltablib.c52
1 files changed, 15 insertions, 37 deletions
diff --git a/src/ltablib.c b/src/ltablib.c
index c534957..d344a47 100644
--- a/src/ltablib.c
+++ b/src/ltablib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltablib.c,v 1.93.1.1 2017/04/19 17:20:42 roberto Exp $
+** $Id: ltablib.c $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@@ -58,24 +58,6 @@ static void checktab (lua_State *L, int arg, int what) {
}
-#if defined(LUA_COMPAT_MAXN)
-static int maxn (lua_State *L) {
- lua_Number max = 0;
- luaL_checktype(L, 1, LUA_TTABLE);
- lua_pushnil(L); /* first key */
- while (lua_next(L, 1)) {
- lua_pop(L, 1); /* remove value */
- if (lua_type(L, -1) == LUA_TNUMBER) {
- lua_Number v = lua_tonumber(L, -1);
- if (v > max) max = v;
- }
- }
- lua_pushnumber(L, max);
- return 1;
-}
-#endif
-
-
static int tinsert (lua_State *L) {
lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
lua_Integer pos; /* where to insert new element */
@@ -87,7 +69,9 @@ static int tinsert (lua_State *L) {
case 3: {
lua_Integer i;
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
- luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
+ /* check whether 'pos' is in [1, e] */
+ luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
+ "position out of bounds");
for (i = e; i > pos; i--) { /* move up elements */
lua_geti(L, 1, i - 1);
lua_seti(L, 1, i); /* t[i] = t[i - 1] */
@@ -107,14 +91,16 @@ static int tremove (lua_State *L) {
lua_Integer size = aux_getn(L, 1, TAB_RW);
lua_Integer pos = luaL_optinteger(L, 2, size);
if (pos != size) /* validate 'pos' if given */
- luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
+ /* check whether 'pos' is in [1, size + 1] */
+ luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1,
+ "position out of bounds");
lua_geti(L, 1, pos); /* result = t[pos] */
for ( ; pos < size; pos++) {
lua_geti(L, 1, pos + 1);
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
}
lua_pushnil(L);
- lua_seti(L, 1, pos); /* t[pos] = nil */
+ lua_seti(L, 1, pos); /* remove entry t[pos] */
return 1;
}
@@ -191,7 +177,7 @@ static int tconcat (lua_State *L) {
** =======================================================
*/
-static int pack (lua_State *L) {
+static int tpack (lua_State *L) {
int i;
int n = lua_gettop(L); /* number of elements to pack */
lua_createtable(L, n, 1); /* create result table */
@@ -204,7 +190,7 @@ static int pack (lua_State *L) {
}
-static int unpack (lua_State *L) {
+static int tunpack (lua_State *L) {
lua_Unsigned n;
lua_Integer i = luaL_optinteger(L, 2, 1);
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) {
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
for (;;) {
/* next loop: repeat ++i while a[i] < P */
- while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
+ while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[i] */
}
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
/* next loop: repeat --j while P < a[j] */
- while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
+ while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
if (j < i) /* j < i but a[j] > P ?? */
luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
@@ -352,7 +338,7 @@ static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
/*
-** QuickSort algorithm (recursive function)
+** Quicksort algorithm (recursive function)
*/
static void auxsort (lua_State *L, IdxT lo, IdxT up,
unsigned int rnd) {
@@ -425,12 +411,9 @@ static int sort (lua_State *L) {
static const luaL_Reg tab_funcs[] = {
{"concat", tconcat},
-#if defined(LUA_COMPAT_MAXN)
- {"maxn", maxn},
-#endif
{"insert", tinsert},
- {"pack", pack},
- {"unpack", unpack},
+ {"pack", tpack},
+ {"unpack", tunpack},
{"remove", tremove},
{"move", tmove},
{"sort", sort},
@@ -440,11 +423,6 @@ static const luaL_Reg tab_funcs[] = {
LUAMOD_API int luaopen_table (lua_State *L) {
luaL_newlib(L, tab_funcs);
-#if defined(LUA_COMPAT_UNPACK)
- /* _G.unpack = table.unpack */
- lua_getfield(L, -1, "unpack");
- lua_setglobal(L, "unpack");
-#endif
return 1;
}