diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-16 15:53:25 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-05-16 15:53:25 -0300 |
| commit | 17159b491c98e7da79b413d8b0ca770a1af116f3 (patch) | |
| tree | 43039bb5e50cb877b813303bb694024164d3a4d4 | |
| parent | 0aa32fa0cbe8d9fea52e0311d09225b37697e085 (diff) | |
| download | lua-17159b491c98e7da79b413d8b0ca770a1af116f3.tar.gz lua-17159b491c98e7da79b413d8b0ca770a1af116f3.tar.bz2 lua-17159b491c98e7da79b413d8b0ca770a1af116f3.zip | |
more direct implementation of 'table.pack'
| -rw-r--r-- | ltablib.c | 16 |
1 files changed, 6 insertions, 10 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.68 2014/04/04 16:38:11 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.69 2014/04/12 14:43:50 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 | */ |
| @@ -118,18 +118,14 @@ static int tconcat (lua_State *L) { | |||
| 118 | */ | 118 | */ |
| 119 | 119 | ||
| 120 | static int pack (lua_State *L) { | 120 | static int pack (lua_State *L) { |
| 121 | int i; | ||
| 121 | int n = lua_gettop(L); /* number of elements to pack */ | 122 | int n = lua_gettop(L); /* number of elements to pack */ |
| 122 | lua_createtable(L, n, 1); /* create result table */ | 123 | lua_createtable(L, n, 1); /* create result table */ |
| 124 | lua_insert(L, 1); /* put it at index 1 */ | ||
| 125 | for (i = n; i >= 1; i--) /* assign elements */ | ||
| 126 | lua_rawseti(L, 1, i); | ||
| 123 | lua_pushinteger(L, n); | 127 | lua_pushinteger(L, n); |
| 124 | lua_setfield(L, -2, "n"); /* t.n = number of elements */ | 128 | lua_setfield(L, 1, "n"); /* t.n = number of elements */ |
| 125 | if (n > 0) { /* at least one element? */ | ||
| 126 | int i; | ||
| 127 | lua_pushvalue(L, 1); | ||
| 128 | lua_rawseti(L, -2, 1); /* insert first element */ | ||
| 129 | lua_replace(L, 1); /* move table into index 1 */ | ||
| 130 | for (i = n; i >= 2; i--) /* assign other elements */ | ||
| 131 | lua_rawseti(L, 1, i); | ||
| 132 | } | ||
| 133 | return 1; /* return table */ | 129 | return 1; /* return table */ |
| 134 | } | 130 | } |
| 135 | 131 | ||
