Skip to main content

Module table_lib

Module table_lib 

Source
Expand description

Lua table standard library.

Provides: table.concat, table.insert, table.move, table.pack, table.remove, table.sort, table.unpack.

C source: reference/lua-5.4.7/src/ltablib.c.

Constants§

TABLE_FUNCS
The core table roster shared by 5.2-5.5. move is filtered out for 5.2 (a 5.3 addition) and move/pack/unpack for 5.1 by open_table, which also layers the version-specific extras (5.1 legacy, 5.5 create) on top.

Functions§

concat
table.concat(t, [sep, [i, [j]]]). Joins t[i..j] (defaults i=1, j=#t) with sep (default empty) into one string. Each element must be a string or number; the first that is not raises through [add_field].
create
table.create(nseq [, nrec]) — Lua 5.5 addition (specs/research/5.5-upstream-delta.md §5, ltablib.c).
insert
table.insert(t, [pos,] v). With two args, appends v at border+1. With three, inserts v at pos (1 <= pos <= border+1) after shifting the tail up by one; any other arity raises “wrong number of arguments to ‘insert’”. The bounds check uses a wrapping unsigned subtract so pos <= 0 is rejected alongside pos > border+1. Note border here is #t, which honors a __len metamethod on 5.2+ and uses the primitive length on 5.1.
open_table
Builds the table library table for the running version. The base roster is TABLE_FUNCS, from which 5.1 and 5.2 drop the functions they lack and onto which 5.1’s legacy roster and 5.5’s create are layered. The per-version deltas below are each verified against that version’s reference binary.
pack
table.pack(...). Creates a new table with the arguments at integer keys 1..n and t.n set to the literal argument count n — holes and trailing nils included, so .n recovers an arity that a #t border would lose (pinned by v52_plus_pack_n_field_*). A 5.2 addition.
remove
table.remove(t, [pos]). Removes and returns t[pos] (default: the last element, #t), shifting the tail down to close the gap.
sort
table.sort(t, [comp]): sorts t in place using comp, or the default < operator if comp is absent.
tmove
table.move(a1, f, e, t, [a2]). Copies a1[f..e] into a2[t..] (or a1[t..] if a2 is absent), reading source slots through __index and writing destinations through __newindex one element at a time. To survive an overlapping in-place range, it copies FORWARD (increasing index) when the destination is clear of the source’s tail (t > e || t <= f, or a distinct destination table) and BACKWARD (decreasing) otherwise — the order pinned by v53_plus_move_*. Returns the destination table. A 5.3 addition.
unpack
table.unpack(t, [i, [j]]). Pushes t[i], t[i+1], …, t[j] (defaults i=1, j=#t) and returns the count. An i > e range is empty (zero results); a span of INT_MAX or more — including the i64-extreme wrap where e - i overflows to a huge unsigned value — raises “too many results to unpack” rather than attempting the push (pinned by v52_plus_unpack_* / v53_plus_unpack_*). A 5.2 addition.