From 924a46e15e5296f930159534d8a4e1385114e0e0 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 6 Apr 2016 23:45:24 +0200 Subject: sync with Context as of 2016-04-06 --- lualibs-lpeg.lua | 2 +- lualibs-lua.lua | 14 ++++- lualibs-trac-inf.lua | 6 +- lualibs-unicode.lua | 9 +-- lualibs-util-fil.lua | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++ lualibs-util-prs.lua | 11 +++- lualibs-util-tab.lua | 2 +- 7 files changed, 198 insertions(+), 13 deletions(-) create mode 100644 lualibs-util-fil.lua diff --git a/lualibs-lpeg.lua b/lualibs-lpeg.lua index 5be1246..959ca55 100644 --- a/lualibs-lpeg.lua +++ b/lualibs-lpeg.lua @@ -110,7 +110,7 @@ local underscore = P("_") local hexdigit = digit + lowercase + uppercase local cr, lf, crlf = P("\r"), P("\n"), P("\r\n") ----- newline = crlf + S("\r\n") -- cr + lf -local newline = P("\r") * (P("\n") + P(true)) + P("\n") +local newline = P("\r") * (P("\n") + P(true)) + P("\n") -- P("\r")^-1 * P("\n")^-1 local escaped = P("\\") * anything local squote = P("'") local dquote = P('"') diff --git a/lualibs-lua.lua b/lualibs-lua.lua index cb61829..b90f37e 100644 --- a/lualibs-lua.lua +++ b/lualibs-lua.lua @@ -19,12 +19,20 @@ if not modules then modules = { } end modules ['l-lua'] = { -- compatibility hacksand helpers -local major, minor = string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$") +_MAJORVERSION, _MINORVERSION = string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$") -_MAJORVERSION = tonumber(major) or 5 -_MINORVERSION = tonumber(minor) or 1 +_MAJORVERSION = tonumber(_MAJORVERSION) or 5 +_MINORVERSION = tonumber(_MINORVERSION) or 1 _LUAVERSION = _MAJORVERSION + _MINORVERSION/10 +if _LUAVERSION < 5.2 and jit then + -- + -- we want loadstring cum suis to behave like 5.2 + -- + _MINORVERSION = 2 + _LUAVERSION = 5.2 +end + -- lpeg if not lpeg then diff --git a/lualibs-trac-inf.lua b/lualibs-trac-inf.lua index 5497e54..f4c4e55 100644 --- a/lualibs-trac-inf.lua +++ b/lualibs-trac-inf.lua @@ -145,13 +145,13 @@ function statistics.show() -- so far -- collectgarbage("collect") register("lua properties",function() - local list = status.list() - local hashchar = tonumber(list.luatex_hashchars) + local hashchar = tonumber(status.luatex_hashchars) + local hashtype = status.luatex_hashtype local mask = lua.mask or "ascii" return format("engine: %s, used memory: %s, hash type: %s, hash chars: min(%s,40), symbol mask: %s (%s)", jit and "luajit" or "lua", statistics.memused(), - list.luatex_hashtype or "default", + hashtype or "default", hashchar and 2^hashchar or "unknown", mask, mask == "utf" and "τεχ" or "tex") diff --git a/lualibs-unicode.lua b/lualibs-unicode.lua index 70b6032..3dec800 100644 --- a/lualibs-unicode.lua +++ b/lualibs-unicode.lua @@ -528,9 +528,10 @@ end -- end, pattern -- end -function utf.remapper(mapping,option) -- static also returns a pattern +function utf.remapper(mapping,option,action) -- static also returns a pattern local variant = type(mapping) if variant == "table" then + action = action or mapping if option == "dynamic" then local pattern = false table.setmetatablenewindex(mapping,function(t,k,v) rawset(t,k,v) pattern = false end) @@ -539,16 +540,16 @@ function utf.remapper(mapping,option) -- static also returns a pattern return "" else if not pattern then - pattern = Cs((tabletopattern(mapping)/mapping + p_utf8char)^0) + pattern = Cs((tabletopattern(mapping)/action + p_utf8char)^0) end return lpegmatch(pattern,str) end end elseif option == "pattern" then - return Cs((tabletopattern(mapping)/mapping + p_utf8char)^0) + return Cs((tabletopattern(mapping)/action + p_utf8char)^0) -- elseif option == "static" then else - local pattern = Cs((tabletopattern(mapping)/mapping + p_utf8char)^0) + local pattern = Cs((tabletopattern(mapping)/action + p_utf8char)^0) return function(str) if not str or str == "" then return "" diff --git a/lualibs-util-fil.lua b/lualibs-util-fil.lua new file mode 100644 index 0000000..28c92c7 --- /dev/null +++ b/lualibs-util-fil.lua @@ -0,0 +1,167 @@ +if not modules then modules = { } end modules ['util-fil'] = { + 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" +} + +local byte = string.byte +local extract = bit32.extract + +-- Here are a few helpers (the starting point were old ones I used for parsing +-- flac files). In Lua 5.3 we can probably do this better. Some code will move +-- here. + +utilities = utilities or { } +local files = { } +utilities.files = files + +local zerobased = { } + +function files.open(filename,zb) + local f = io.open(filename,"rb") + if f then + zerobased[f] = zb or false + end + return f +end + +function files.close(f) + zerobased[f] = nil + f:close() +end + +function files.size(f) + return f:seek("end") +end + +function files.setposition(f,n) + if zerobased[f] then + f:seek("set",n) + else + f:seek("set",n - 1) + end +end + +function files.getposition(f) + if zerobased[f] then + return f:seek() + else + return f:seek() + 1 + end +end + +function files.look(f,n,chars) + local p = f:seek() + local s = f:read(n) + f:seek("set",p) + if chars then + return s + else + return byte(s,1,#s) + end +end + +function files.skip(f,n) + if n == 1 then + f:read(n) + else + f:seek("set",f:seek()+n) + end +end + +function files.readbyte(f) + return byte(f:read(1)) +end + +function files.readbytes(f,n) + return byte(f:read(n),1,n) +end + +function files.readchar(f) + return f:read(1) +end + +function files.readstring(f,n) + return f:read(n or 1) +end + +function files.readinteger1(f) -- one byte + local n = byte(f:read(1)) + if n >= 0x80 then + return n - 0xFF - 1 + else + return n + end +end + +files.readcardinal1 = files.readbyte -- one byte +files.readcardinal = files.readcardinal1 +files.readinteger = files.readinteger1 + +function files.readcardinal2(f) + local a, b = byte(f:read(2),1,2) + return 0x100 * a + b +end + +function files.readinteger2(f) + local a, b = byte(f:read(2),1,2) + local n = 0x100 * a + b + if n >= 0x8000 then + return n - 0xFFFF - 1 + else + return n + end +end + +function files.readcardinal3(f) + local a, b, c = byte(f:read(3),1,3) + return 0x10000 * a + 0x100 * b + c +end + +function files.readcardinal4(f) + local a, b, c, d = byte(f:read(4),1,4) + return 0x1000000 * a + 0x10000 * b + 0x100 * c + d +end + +function files.readinteger4(f) + local a, b, c, d = byte(f:read(4),1,4) + local n = 0x1000000 * a + 0x10000 * b + 0x100 * c + d + if n >= 0x8000000 then + return n - 0xFFFFFFFF - 1 + else + return n + end +end + +function files.readfixed4(f) + local a, b, c, d = byte(f:read(4),1,4) + local n = 0x100 * a + b + if n >= 0x8000 then + return n - 0xFFFF - 1 + (0x100 * c + d)/0xFFFF + else + return n + (0x100 * c + d)/0xFFFF + end +end + +function files.read2dot14(f) + local a, b = byte(f:read(2),1,2) + local n = 0x100 * a + b + local m = extract(n,0,30) + if n > 0x7FFF then + n = extract(n,30,2) + return m/0x4000 - 4 + else + n = extract(n,30,2) + return n + m/0x4000 + end +end + +function files.skipshort(f,n) + f:read(2*(n or 1)) +end + +function files.skiplong(f,n) + f:read(4*(n or 1)) +end diff --git a/lualibs-util-prs.lua b/lualibs-util-prs.lua index a3c1c6f..01138f1 100644 --- a/lualibs-util-prs.lua +++ b/lualibs-util-prs.lua @@ -24,6 +24,8 @@ local sortedhash = table.sortedhash local sortedkeys = table.sortedkeys local tohash = table.tohash +local hashes = { } +utilities.parsers.hashes = hashes -- we share some patterns local digit = R("09") @@ -315,10 +317,17 @@ end local pattern = Cf(Ct("") * Cg(C((1-S(", "))^1) * S(", ")^0 * Cc(true))^1,rawset) -function utilities.parsers.settings_to_set(str,t) +function utilities.parsers.settings_to_set(str) return str and lpegmatch(pattern,str) or { } end +hashes.settings_to_set = table.setmetatableindex(function(t,k) -- experiment, not public + local v = k and lpegmatch(pattern,k) or { } + t[k] = v + return v +end) + +getmetatable(hashes.settings_to_set).__mode = "kv" -- could be an option (maybe sharing makes sense) function parsers.simple_hash_to_string(h, separator) local t, tn = { }, 0 diff --git a/lualibs-util-tab.lua b/lualibs-util-tab.lua index d6f3d67..a6239ad 100644 --- a/lualibs-util-tab.lua +++ b/lualibs-util-tab.lua @@ -833,5 +833,5 @@ 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",{})) return true end end) end -- cgit v1.2.3