From fe5c41fb8a0e1d4f130437b22667bf699851b17a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 30 Dec 1998 11:16:50 -0200 Subject: new functions "tinsert" and "tremove" --- lbuiltin.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'lbuiltin.c') diff --git a/lbuiltin.c b/lbuiltin.c index 7fc5a647..7b67b9fc 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -387,6 +387,37 @@ static void luaB_foreachvar (void) { } +static void luaB_tinsert (void) { + Hash *a = gethash(1); + lua_Object v = lua_getparam(3); + int n = (int)getnarg(a); + int pos; + if (v != LUA_NOOBJECT) + pos = luaL_check_int(2); + else { /* called with only 2 arguments */ + v = luaL_nonnullarg(2); + pos = n+1; + } + luaV_setn(a, n+1); /* increment field "n" */ + for ( ;n>=pos; n--) + tablemove(a, n, n+1); + luaH_setint(a, pos, luaA_Address(v)); +} + + +static void luaB_tremove (void) { + Hash *a = gethash(1); + int n = (int)getnarg(a); + int pos = luaL_opt_int(2, n); + TObject v = *luaH_getint(a, pos); + if (n <= 0) return; /* table is "empty" */ + luaV_setn(a, n-1); /* decrement field "n" */ + for ( ;pos