libs/table.lua

Thu, 23 Mar 2023 18:56:32 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 23 Mar 2023 18:56:32 +0000
changeset 485
c9a144591649
parent 483
33d566513eb0
permissions
-rw-r--r--

component: Avoid adding to the global stream metatable

This allows component and client connections to be made side-by-side.
Previous to this change, loading this connection module would break the
ability to make client connections, due to overriding stream methods such as
:reopen() and :reset().

A next step would be to share the methods that the two connection modules have
in common.

483
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
1 return {
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
2 pack = function(...) return {n = select("#", ...); ...} end;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
3 create = function() return {} end;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
4 move = function (a1, f, e, t, a2)
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
5 a2 = a2 or a1;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
6 if e >= f then
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
7 local n = (e - f) + 1;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
8 if t > e or t <= f or a1 ~= a2 then
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
9 for i = 0, n-1 do
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
10 a2[t+i] = a1[f+i];
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
11 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
12 else
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
13 for i = n-1, 0, -1 do
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
14 a2[t+i] = a1[f+i];
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
15 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
16 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
17 end
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
18 return a2;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
19 end;
33d566513eb0 libs/table: Add table.move
Matthew Wild <mwild1@gmail.com>
parents: 439
diff changeset
20 }

mercurial