diff options
author | Karl Berry <karl@freefriends.org> | 2017-02-05 23:22:36 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2017-02-05 23:22:36 +0000 |
commit | 7ef0c5b362e4df3e85449f3c397ca3b61edf15a0 (patch) | |
tree | ca33364f8dca69a80a40985834e5726448ba6746 /Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua | |
parent | 17efbb93c3c280ebec43438b10c53601758ef11e (diff) |
lualibs (4feb17)
git-svn-id: svn://tug.org/texlive/trunk@43153 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua')
-rw-r--r-- | Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua b/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua index a6239adf409..0521a2a4f14 100644 --- a/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua +++ b/Master/texmf-dist/tex/luatex/lualibs/lualibs-util-tab.lua @@ -12,7 +12,7 @@ local tables = utilities.tables local format, gmatch, gsub, sub = string.format, string.gmatch, string.gsub, string.sub local concat, insert, remove, sort = table.concat, table.insert, table.remove, table.sort -local setmetatable, getmetatable, tonumber, tostring = setmetatable, getmetatable, tonumber, tostring +local setmetatable, getmetatable, tonumber, tostring, rawget = setmetatable, getmetatable, tonumber, tostring, rawget local type, next, rawset, tonumber, tostring, load, select = type, next, rawset, tonumber, tostring, load, select local lpegmatch, P, Cs, Cc = lpeg.match, lpeg.P, lpeg.Cs, lpeg.Cc local sortedkeys, sortedpairs = table.sortedkeys, table.sortedpairs @@ -169,7 +169,8 @@ function table.tocsv(t,specification) r[f] = tostring(field) end end - result[#result+1] = concat(r,separator) + -- result[#result+1] = concat(r,separator) + result[i+1] = concat(r,separator) end return concat(result,"\n") else @@ -485,11 +486,12 @@ end local selfmapper = { __index = function(t,k) t[k] = k return k end } -function table.twowaymapper(t) - if not t then - t = { } - else - for i=0,#t do +function table.twowaymapper(t) -- takes a 0/1 .. n indexed table and returns + if not t then -- it with string-numbers as indices + reverse + t = { } -- mapping (all strings) .. used in cvs etc but + else -- typically a helper that one forgets about + local zero = rawget(t,0) -- so it might move someplace else + for i=zero and 0 or 1,#t do local ti = t[i] -- t[1] = "one" if ti then local i = tostring(i) @@ -497,7 +499,6 @@ function table.twowaymapper(t) t[ti] = i -- t["one"] = "1" end end - t[""] = t[0] or "" end -- setmetatableindex(t,"key") setmetatable(t,selfmapper) @@ -616,7 +617,8 @@ local function serialize(root,name,specification) return nil end end - local haszero = t[0] + -- local haszero = t[0] + local haszero = rawget(t,0) -- don't trigger meta if n == nt then local tt = { } for i=1,nt do @@ -680,7 +682,8 @@ local function serialize(root,name,specification) local last = 0 last = #root for k=1,last do - if root[k] == nil then + if rawget(root,k) == nil then + -- if root[k] == nil then last = k - 1 break end @@ -810,9 +813,10 @@ local function serialize(root,name,specification) if root then -- The dummy access will initialize a table that has a delayed initialization - -- using a metatable. (maybe explicitly test for metatable) + -- using a metatable. (maybe explicitly test for metatable). This can crash on + -- metatables that check the index against a number. if getmetatable(root) then -- todo: make this an option, maybe even per subtable - local dummy = root._w_h_a_t_e_v_e_r_ + local dummy = root._w_h_a_t_e_v_e_r_ -- needed root._w_h_a_t_e_v_e_r_ = nil end -- Let's forget about empty tables. @@ -833,5 +837,10 @@ end table.serialize = serialize if setinspector then - setinspector("table",function(v) if type(v) == "table" then print(serialize(v,"table",{})) return true end end) + setinspector("table",function(v) + if type(v) == "table" then + print(serialize(v,"table",{ metacheck = false })) + return true + end + end) end |