diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-03-26 10:25:17 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-03-26 10:25:17 -0300 |
| commit | d7cb62286642b0f5c16057373ff129109e1a3e8a (patch) | |
| tree | c13039cd6b27ed5110c8e08e19c996d7f69870a3 | |
| parent | 26a9b249db37d713d7a74e24036fd93666695236 (diff) | |
| download | lua-d7cb62286642b0f5c16057373ff129109e1a3e8a.tar.gz lua-d7cb62286642b0f5c16057373ff129109e1a3e8a.tar.bz2 lua-d7cb62286642b0f5c16057373ff129109e1a3e8a.zip | |
`unpack' has two optional arguments (to give a range)
| -rw-r--r-- | lbaselib.c | 12 |
1 files changed, 8 insertions, 4 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.139 2003/12/09 16:55:43 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.140 2004/03/09 17:34:35 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -345,11 +345,15 @@ static int luaB_assert (lua_State *L) { | |||
| 345 | 345 | ||
| 346 | 346 | ||
| 347 | static int luaB_unpack (lua_State *L) { | 347 | static int luaB_unpack (lua_State *L) { |
| 348 | int n, i; | 348 | int i = luaL_optint(L, 2, 1); |
| 349 | int e = luaL_optint(L, 3, -1); | ||
| 350 | int n; | ||
| 349 | luaL_checktype(L, 1, LUA_TTABLE); | 351 | luaL_checktype(L, 1, LUA_TTABLE); |
| 350 | n = luaL_getn(L, 1); | 352 | if (e == -1) e = luaL_getn(L, 1); |
| 353 | n = e - i + 1; /* number of elements */ | ||
| 354 | if (n <= 0) return 0; /* empty range */ | ||
| 351 | luaL_checkstack(L, n, "table too big to unpack"); | 355 | luaL_checkstack(L, n, "table too big to unpack"); |
| 352 | for (i=1; i<=n; i++) /* push arg[1...n] */ | 356 | for (; i<=e; i++) /* push arg[i...e] */ |
| 353 | lua_rawgeti(L, 1, i); | 357 | lua_rawgeti(L, 1, i); |
| 354 | return n; | 358 | return n; |
| 355 | } | 359 | } |
