diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-03-16 13:58:41 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-03-16 13:58:41 -0300 |
| commit | 9ffae705ee8894d68bbc07a1a5f77a112c40efad (patch) | |
| tree | e162bb7ecfd1a3e6776cf09e62736ec8eb0fca41 /ltablib.c | |
| parent | 6bfef60e77f5eec1057470010b4179ee6f830fe6 (diff) | |
| download | lua-9ffae705ee8894d68bbc07a1a5f77a112c40efad.tar.gz lua-9ffae705ee8894d68bbc07a1a5f77a112c40efad.tar.bz2 lua-9ffae705ee8894d68bbc07a1a5f77a112c40efad.zip | |
new "primitive" getn
Diffstat (limited to '')
| -rw-r--r-- | ltablib.c | 21 |
1 files changed, 12 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.26 2004/06/15 13:37:21 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.27 2004/12/07 18:28:47 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 | */ |
| @@ -62,29 +62,32 @@ static int getn (lua_State *L) { | |||
| 62 | 62 | ||
| 63 | static int setn (lua_State *L) { | 63 | static int setn (lua_State *L) { |
| 64 | luaL_checktype(L, 1, LUA_TTABLE); | 64 | luaL_checktype(L, 1, LUA_TTABLE); |
| 65 | #ifndef luaL_setn | ||
| 65 | luaL_setn(L, 1, luaL_checkint(L, 2)); | 66 | luaL_setn(L, 1, luaL_checkint(L, 2)); |
| 67 | #else | ||
| 68 | luaL_error(L, "`setn' is obsolete"); | ||
| 69 | #endif | ||
| 66 | lua_pushvalue(L, 1); | 70 | lua_pushvalue(L, 1); |
| 67 | return 1; | 71 | return 1; |
| 68 | } | 72 | } |
| 69 | 73 | ||
| 70 | 74 | ||
| 71 | static int tinsert (lua_State *L) { | 75 | static int tinsert (lua_State *L) { |
| 72 | int v = lua_gettop(L); /* number of arguments */ | ||
| 73 | int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */ | 76 | int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */ |
| 74 | int pos; /* where to insert new element */ | 77 | int pos; /* where to insert new element */ |
| 75 | if (v == 2) /* called with only 2 arguments */ | 78 | if (lua_isnone(L, 3)) /* called with only 2 arguments */ |
| 76 | pos = e; /* insert new element at the end */ | 79 | pos = e; /* insert new element at the end */ |
| 77 | else { | 80 | else { |
| 81 | int i; | ||
| 78 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ | 82 | pos = luaL_checkint(L, 2); /* 2nd argument is the position */ |
| 79 | if (pos > e) e = pos; /* `grow' array if necessary */ | 83 | if (pos > e) e = pos; /* `grow' array if necessary */ |
| 80 | v = 3; /* function may be called with more than 3 args */ | 84 | lua_settop(L, 3); /* function may be called with more than 3 args */ |
| 85 | for (i = e; i > pos; i--) { /* move up elements */ | ||
| 86 | lua_rawgeti(L, 1, i-1); | ||
| 87 | lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ | ||
| 88 | } | ||
| 81 | } | 89 | } |
| 82 | luaL_setn(L, 1, e - LUA_FIRSTINDEX + 1); /* new size */ | 90 | luaL_setn(L, 1, e - LUA_FIRSTINDEX + 1); /* new size */ |
| 83 | while (--e >= pos) { /* move up elements */ | ||
| 84 | lua_rawgeti(L, 1, e); | ||
| 85 | lua_rawseti(L, 1, e+1); /* t[e+1] = t[e] */ | ||
| 86 | } | ||
| 87 | lua_pushvalue(L, v); | ||
| 88 | lua_rawseti(L, 1, pos); /* t[pos] = v */ | 91 | lua_rawseti(L, 1, pos); /* t[pos] = v */ |
| 89 | return 0; | 92 | return 0; |
| 90 | } | 93 | } |
