Change the lua length operator to do the obvious thing.

This commit is contained in:
2026-02-11 14:51:03 -05:00
parent 24075cd356
commit 30e53c3054
4 changed files with 56 additions and 53 deletions

View File

@@ -163,27 +163,6 @@ int luaH_nkeys (Table *t) {
return t->nnkeys;
}
int luaH_nthkey (lua_State *L, Table *t, int n, StkId pair) {
n -= 1; /* convert to C indexing */
if ((n < 0) || (n >= t->nnkeys)) {
setnilvalue(pair+0);
setnilvalue(pair+1);
return 0;
}
int index = t->sequence[n];
if (index < t->sizearray) {
setnvalue(pair + 0, index + 1);
setobj2s(L, pair + 1, &t->array[index].i_val);
return 1;
} else {
index -= t->sizearray;
Node *n = t->node + index;
setobj2s(L, pair + 0, gkey(n));
setobj2s(L, pair + 1, gval(n));
return 1;
}
}
int successorindex (lua_State *L, Table *t, StkId key) {
int i, seqno;
if (ttisnil(key)) {
@@ -706,34 +685,13 @@ void luaH_setint (lua_State *L, Table *t, int key, TValue *value) {
}
/*
** Try to find a boundary in table `t'. A `boundary' is an integer index
** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).
** Return the number of keys in the table.
*/
int luaH_getn (Table *t) {
unsigned int j = 1;
unsigned int i = 0;
/* find `i' and `j' such that i is present and j is not */
while (!ttisnil(luaH_getint(t, j))) {
i = j;
j *= 2;
if (j > cast(unsigned int, MAX_INT)) { /* overflow? */
/* table was built with bad purposes: resort to linear search */
i = 1;
while (!ttisnil(luaH_getint(t, i))) i++;
return i - 1;
}
}
/* now do a binary search between them */
while (j - i > 1) {
unsigned int m = (i+j)/2;
if (ttisnil(luaH_getint(t, m))) j = m;
else i = m;
}
return i;
return t->nnkeys;
}
#if defined(LUA_DEBUG)
Node *luaH_mainposition (const Table *t, const TValue *key) {

View File

@@ -37,7 +37,6 @@ LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize);
LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize);
LUAI_FUNC void luaH_free (lua_State *L, Table *t);
LUAI_FUNC int luaH_nkeys (Table *t);
LUAI_FUNC int luaH_nthkey (lua_State *L, Table *t, int n, StkId pair);
LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key);
LUAI_FUNC int luaH_getn (Table *t);