1348 1 126134 13273 1225 101 82895 28772 0 36940 48495 0 0 0 0 602 6449 7)ˆl–ßi~”Jj"T%ýÆÛ¸&TÅ£�þ^mÆÖl£dr:Ë üÿÒ „e ´ÿÑÀÎ?÷)if not modules then modules = { } end modules ['util-tab'] = { version = 1.001, comment = "companion to luat-lib.mkiv", author = "Hans Hagen, PRAGMA-ADE, Hasselt NL", copyright = "PRAGMA ADE / ConTeXt Development Team", license = "see context related readme files" } utilities = utilities or {} utilities.tables = utilities.tables or { } 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, 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 local formatters = string.formatters local utftoeight = utf.toeight local splitter = lpeg.tsplitat(".") function utilities.tables.definetable(target,nofirst,nolast) -- defines undefined tables local composed = nil local t = { } local snippets = lpegmatch(splitter,target) for i=1,#snippets - (nolast and 1 or 0) do local name = snippets[i] if composed then composed = composed .. "." .. name t[#t+1] = formatters["if not %s then %s = { } end"](composed,composed) else composed = name if not nofirst then t[#t+1] = formatters["%s = %s or { }"](composed,composed) end end end if composed then if nolast then composed = composed .. "." .. snippets[#snippets] end return concat(t,"\n"), composed -- could be shortcut else return "", target end end -- local t = tables.definedtable("a","b","c","d") function tables.definedtable(...) local t = _G for i=1,select("#",...) do local li = select(i,...) local tl = t[li] if not tl then tl = { } t[li] = tl end t = tl end return t end function tables.accesstable(target,root) local t = root or _G for name in gmatch(target,"([^%.]+)") do t = t[name] if not t then return end end return t end function tables.migratetable(target,v,root) local t = root or _G local names = lpegmatch(splitter,target) for i=1,#names-1 do local name = names[i] t[name] = t[name] or { } t = t[name] if not t then return end end t[names[#names]] = v end function tables.removevalue(t,value) -- todo: n if value then for i=1,#t do if t[i] == value then remove(t,i) -- remove all, so no: return end end end end function tables.replacevalue(t,oldvalue,newvalue) if oldvalue and newvalue then for i=1,#t do if t[i] == oldvalue then t[i] = newvalue -- replace all, so no: return end end end end function tables.insertbeforevalue(t,value,extra) for i=1,#t do if t[i] == extra then remove(t,i) end end for i=1,#t do if t[i] == value then insert(t,i,extra) return end end insert(t,1,extra) end function tables.insertaftervalue(t,value,extra) for i=1,#t do if t[i] == extra then remove(t,i) end end for i=1,#t do if t[i] == value then insert(t,i+1,extra) return end end insert(t,#t+1,extra) end -- experimental local escape = Cs(Cc('"') * ((P('"')/'""' + P(1))^