diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-05-10 14:50:51 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-05-10 14:50:51 -0300 |
| commit | 7e41612eb28ff0fba282bd419781fcbc9bd94776 (patch) | |
| tree | e63123d15589435c08c844b0d471b142c924818b /ltablib.c | |
| parent | b0f341ee52ee7d3d22261a68caf06eab5b0c8822 (diff) | |
| download | lua-7e41612eb28ff0fba282bd419781fcbc9bd94776.tar.gz lua-7e41612eb28ff0fba282bd419781fcbc9bd94776.tar.bz2 lua-7e41612eb28ff0fba282bd419781fcbc9bd94776.zip | |
code parameterized by LUA_FIRSTINDEX (first index of an array)
Diffstat (limited to 'ltablib.c')
| -rw-r--r-- | ltablib.c | 17 |
1 files changed, 9 insertions, 8 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.22 2003/10/07 20:13:41 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.23 2004/04/30 20:13:38 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 | */ |
| @@ -23,7 +23,7 @@ static int luaB_foreachi (lua_State *L) { | |||
| 23 | int i; | 23 | int i; |
| 24 | int n = aux_getn(L, 1); | 24 | int n = aux_getn(L, 1); |
| 25 | luaL_checktype(L, 2, LUA_TFUNCTION); | 25 | luaL_checktype(L, 2, LUA_TFUNCTION); |
| 26 | for (i=1; i<=n; i++) { | 26 | for (i=LUA_FIRSTINDEX; i < n+LUA_FIRSTINDEX; i++) { |
| 27 | lua_pushvalue(L, 2); /* function */ | 27 | lua_pushvalue(L, 2); /* function */ |
| 28 | lua_pushinteger(L, i); /* 1st argument */ | 28 | lua_pushinteger(L, i); /* 1st argument */ |
| 29 | lua_rawgeti(L, 1, i); /* 2nd argument */ | 29 | lua_rawgeti(L, 1, i); /* 2nd argument */ |
| @@ -109,16 +109,17 @@ static int str_concat (lua_State *L) { | |||
| 109 | luaL_Buffer b; | 109 | luaL_Buffer b; |
| 110 | size_t lsep; | 110 | size_t lsep; |
| 111 | const char *sep = luaL_optlstring(L, 2, "", &lsep); | 111 | const char *sep = luaL_optlstring(L, 2, "", &lsep); |
| 112 | int i = luaL_optint(L, 3, 1); | 112 | int i = luaL_optint(L, 3, LUA_FIRSTINDEX); |
| 113 | int n = luaL_optint(L, 4, 0); | 113 | int last = luaL_optint(L, 4, -2); |
| 114 | luaL_checktype(L, 1, LUA_TTABLE); | 114 | luaL_checktype(L, 1, LUA_TTABLE); |
| 115 | if (n == 0) n = luaL_getn(L, 1); | 115 | if (last == -2) |
| 116 | last = luaL_getn(L, 1) + LUA_FIRSTINDEX - 1; | ||
| 116 | luaL_buffinit(L, &b); | 117 | luaL_buffinit(L, &b); |
| 117 | for (; i <= n; i++) { | 118 | for (; i <= last; i++) { |
| 118 | lua_rawgeti(L, 1, i); | 119 | lua_rawgeti(L, 1, i); |
| 119 | luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings"); | 120 | luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings"); |
| 120 | luaL_addvalue(&b); | 121 | luaL_addvalue(&b); |
| 121 | if (i != n) | 122 | if (i != last) |
| 122 | luaL_addlstring(&b, sep, lsep); | 123 | luaL_addlstring(&b, sep, lsep); |
| 123 | } | 124 | } |
| 124 | luaL_pushresult(&b); | 125 | luaL_pushresult(&b); |
| @@ -224,7 +225,7 @@ static int luaB_sort (lua_State *L) { | |||
| 224 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ | 225 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ |
| 225 | luaL_checktype(L, 2, LUA_TFUNCTION); | 226 | luaL_checktype(L, 2, LUA_TFUNCTION); |
| 226 | lua_settop(L, 2); /* make sure there is two arguments */ | 227 | lua_settop(L, 2); /* make sure there is two arguments */ |
| 227 | auxsort(L, 1, n); | 228 | auxsort(L, LUA_FIRSTINDEX, n + LUA_FIRSTINDEX - 1); |
| 228 | return 0; | 229 | return 0; |
| 229 | } | 230 | } |
| 230 | 231 | ||
