Skip to content

Commit

Permalink
Use table.move to implement common.splice (lite-xl#1324)
Browse files Browse the repository at this point in the history
* Use `table.move` to implement `common.splice`

* Disallow negative `remove` in `common.splice`
  • Loading branch information
xcb-xwii authored Jan 13, 2023
1 parent 673c564 commit 6e04a4f
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions data/core/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,14 @@ end


function common.splice(t, at, remove, insert)
assert(remove >= 0, "bad argument #3 to 'splice' (non-negative value expected)")
insert = insert or {}
local offset = #insert - remove
local old_len = #t
if offset < 0 then
for i = at - offset, old_len - offset do
t[i + offset] = t[i]
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
local len = #insert
if remove ~= len then table.move(t, at + remove, #t + remove, at + len) end
table.move(insert, 1, len, at, t)
end



local function compare_score(a, b)
return a.score > b.score
end
Expand Down

0 comments on commit 6e04a4f

Please sign in to comment.