MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / table_is_an_array

Function table_is_an_array

deps/lua/src/lua_cmsgpack.c:425–463  ·  view source on GitHub ↗

Returns true if the Lua table on top of the stack is exclusively composed * of keys from numerical keys from 1 up to N, with N being the total number * of elements, without any hole in the middle. */

Source from the content-addressed store, hash-verified

423 * of keys from numerical keys from 1 up to N, with N being the total number
424 * of elements, without any hole in the middle. */
425int table_is_an_array(lua_State *L) {
426 int count = 0, max = 0;
427#if LUA_VERSION_NUM < 503
428 lua_Number n;
429#else
430 lua_Integer n;
431#endif
432
433 /* Stack top on function entry */
434 int stacktop;
435
436 stacktop = lua_gettop(L);
437
438 lua_pushnil(L);
439 while(lua_next(L,-2)) {
440 /* Stack: ... key value */
441 lua_pop(L,1); /* Stack: ... key */
442 /* The <= 0 check is valid here because we're comparing indexes. */
443#if LUA_VERSION_NUM < 503
444 if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||
445 !IS_INT_EQUIVALENT(n))
446#else
447 if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)
448#endif
449 {
450 lua_settop(L, stacktop);
451 return 0;
452 }
453 max = (n > max ? n : max);
454 count++;
455 }
456 /* We have the total number of elements in "count". Also we have
457 * the max index encountered in "max". We can't reach this code
458 * if there are indexes <= 0. If you also note that there can not be
459 * repeated keys into a table, you have that if max==count you are sure
460 * that there are all the keys form 1 to count (both included). */
461 lua_settop(L, stacktop);
462 return max == count;
463}
464
465/* If the length operator returns non-zero, that is, there is at least
466 * an object at key '1', we serialize to message pack list. Otherwise

Callers 1

mp_encode_lua_tableFunction · 0.85

Calls 7

lua_gettopFunction · 0.85
lua_pushnilFunction · 0.85
lua_nextFunction · 0.85
lua_typeFunction · 0.85
lua_tonumberFunction · 0.85
lua_tointegerFunction · 0.85
lua_settopFunction · 0.85

Tested by

no test coverage detected