lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Well, I mentioned some time ago
> <http://lua-users.org/lists/lua-l/2001-10/msg00179.html> an issue with
> tinsert. I checked and it is still in the updated distribution. Now, I
> can understand you didn't included it in case it brokes some scripts.
> And I gave a workaround ;-)

I've just added a fix for this to my standard library, including the dist
on my web page. I reproduce my version at the bottom of the message. It
calls the built-in tinsert, both for speed and in order to get the same
error messages. It fixes the other bug in tinsert, where tinsert(t, n, v1,
..., vn) would insert vn rather than v1; I believe this is also fixed in
5.0.

  -- Make tinsert (t, p, v1 ... vn) insert v1 rather than vn, and when
  -- p > getn (t), set t.n = p
  local _tinsert = tinsert
  function tinsert (t, ...)
    local pos
    local n = getn (t)
    if arg.n == 1 then
      pos = getn (t) + 1
    else
      pos = arg[1]
    end
    if pos > n then
      t.n = pos - 1
    end
    if arg.n == 1 then
      %_tinsert (t, arg[1])
    elseif arg.n >= 2 then
      %_tinsert (t, arg[1], arg[2])
    else
      %_tinsert (t)
    end
  end