Use `table.move` to implement `common.splice` (#1324)
* Use `table.move` to implement `common.splice` * Disallow negative `remove` in `common.splice`
This commit is contained in:
parent
75b6173dc9
commit
e4c5fceaf9
|
@ -82,23 +82,12 @@ end
|
||||||
|
|
||||||
|
|
||||||
function common.splice(t, at, remove, insert)
|
function common.splice(t, at, remove, insert)
|
||||||
|
assert(remove >= 0, "bad argument #3 to 'splice' (non-negative value expected)")
|
||||||
insert = insert or {}
|
insert = insert or {}
|
||||||
local offset = #insert - remove
|
local len = #insert
|
||||||
local old_len = #t
|
if remove ~= len then table.move(t, at + remove, #t + remove, at + len) end
|
||||||
if offset < 0 then
|
table.move(insert, 1, len, at, t)
|
||||||
for i = at - offset, old_len - offset do
|
|
||||||
t[i + offset] = t[i]
|
|
||||||
end
|
end
|
||||||
elseif offset > 0 then
|
|
||||||
for i = old_len, at, -1 do
|
|
||||||
t[i + offset] = t[i]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
for i, item in ipairs(insert) do
|
|
||||||
t[at + i - 1] = item
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local function compare_score(a, b)
|
local function compare_score(a, b)
|
||||||
|
|
Loading…
Reference in New Issue