From 237d0436d8505bb4263a477022d42c8a291f5b36 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 29 Mar 2015 11:49:58 +0200 Subject: [fontloader] sync with Context as of 2015-03-29 --- src/fontloader/misc/fontloader-l-lpeg.lua | 270 ++++++++---------------------- 1 file changed, 72 insertions(+), 198 deletions(-) (limited to 'src/fontloader/misc/fontloader-l-lpeg.lua') diff --git a/src/fontloader/misc/fontloader-l-lpeg.lua b/src/fontloader/misc/fontloader-l-lpeg.lua index 0c89706..9225b57 100644 --- a/src/fontloader/misc/fontloader-l-lpeg.lua +++ b/src/fontloader/misc/fontloader-l-lpeg.lua @@ -834,209 +834,45 @@ end -- experiment: --- local function make(t) --- local p --- local keys = sortedkeys(t) --- for i=1,#keys do --- local k = keys[i] --- local v = t[k] --- if not p then --- if next(v) then --- p = P(k) * make(v) --- else --- p = P(k) --- end --- else --- if next(v) then --- p = p + P(k) * make(v) --- else --- p = p + P(k) --- end --- end --- end --- return p --- end - --- local function make(t) --- local p = P(false) --- local keys = sortedkeys(t) --- for i=1,#keys do --- local k = keys[i] --- local v = t[k] --- if next(v) then --- p = p + P(k) * make(v) --- else --- p = p + P(k) --- end --- end --- return p --- end - --- function lpeg.utfchartabletopattern(list) -- goes to util-lpg --- local tree = { } --- for i=1,#list do --- local t = tree --- for c in gmatch(list[i],".") do --- local tc = t[c] --- if not tc then --- tc = { } --- t[c] = tc --- end --- t = tc --- end --- end --- return make(tree) --- end - local p_false = P(false) local p_true = P(true) --- local function make(t,hash) --- local p = p_false --- local keys = sortedkeys(t) --- for i=1,#keys do --- local k = keys[i] --- local v = t[k] --- local h = hash[v] --- if h then --- if next(v) then --- p = p + P(k) * (make(v,hash) + p_true) --- else --- p = p + P(k) * p_true --- end --- else --- if next(v) then --- p = p + P(k) * make(v,hash) --- else --- p = p + P(k) --- end --- end --- end --- return p --- end - --- local function make(t,hash) --- local p = p_false --- local keys = sortedkeys(t) --- local function making(t,w) --- local p = p_false --- local keys = sortedkeys(t) --- for i=1,#keys do --- local k = keys[i] --- local v = t[k] --- if w then --- if next(v) then --- p = p + P(k) * (making(v,w) + p_true) --- else --- p = p + P(k) * p_true --- end --- else --- if next(v) then --- p = p + P(k) * making(v,w) --- else --- p = p + P(k) --- end --- end --- end --- return p --- end --- for i=1,#keys do --- local k = keys[i] --- local v = t[k] --- local h = hash[v] --- if h then --- if next(v) then --- p = p + P(k) * (making(v,true) + p_true) --- else --- p = p + P(k) * p_true --- end --- else --- if next(v) then --- p = p + P(k) * making(v,false) --- else --- p = p + P(k) --- end --- end --- end --- return p --- end --- --- function lpeg.utfchartabletopattern(list) -- goes to util-lpg --- local tree = { } --- local hash = { } --- local n = #list --- if n == 0 then --- for s in next, list do --- local t = tree --- for c in gmatch(s,".") do --- local tc = t[c] --- if not tc then --- tc = { } --- t[c] = tc --- end --- t = tc --- end --- hash[t] = s --- end --- else --- for i=1,n do --- local t = tree --- local s = list[i] --- for c in gmatch(s,".") do --- local tc = t[c] --- if not tc then --- tc = { } --- t[c] = tc --- end --- t = tc --- end --- hash[t] = s --- end --- end --- return make(tree,hash) --- end - - -local function make(t,hash) - local p = p_false - local keys = sortedkeys(t) - local function making(t,w) +local function make(t) + local function making(t) local p = p_false local keys = sortedkeys(t) + local okay = t[""] for i=1,#keys do local k = keys[i] - local v = t[k] - if w then + if k ~= "" then + local v = t[k] if v == true then p = p + P(k) * p_true + elseif v == false then + -- can't happen + elseif okay then + p = p + P(k) * (making(v) + p_true) else - p = p + P(k) * (making(v,w) + p_true) - end - else - if v == true then - p = p + P(k) - else - p = p + P(k) * making(v,w) + p = p + P(k) * making(v) end end end return p end + local p = p_false + local keys = sortedkeys(t) for i=1,#keys do local k = keys[i] - local v = t[k] - local h = hash[v] - if h then + if k ~= "" then + local v = t[k] if v == true then p = p + P(k) * p_true + elseif v == false then + -- can't happen + elseif v[""] then + p = p + P(k) * (making(v) + p_true) else - p = p + P(k) * (making(v,true) + p_true) - end - else - if v == true then - p = p + P(k) - else - p = p + P(k) * making(v,false) + p = p + P(k) * making(v) end end end @@ -1045,61 +881,99 @@ end function lpeg.utfchartabletopattern(list) -- goes to util-lpg local tree = { } --- local hash = { } - local hash local n = #list if n == 0 then - hash = list for s in next, list do local t = tree local p, pk for c in gmatch(s,".") do if t == true then - t = { [c] = true } + t = { [c] = true, [""] = true } + p[pk] = t + p = t + t = false + elseif t == false then + t = { [c] = false } p[pk] = t p = t - t = true + t = false else local tc = t[c] if not tc then - tc = true - t[c] = tc + tc = false + t[c] = false end p = t t = tc end pk = c end + if t == false then + p[pk] = true + elseif t == true then + -- okay + else + t[""] = true + end end else - hash = { } for i=1,n do - local t = tree local s = list[i] + local t = tree local p, pk for c in gmatch(s,".") do if t == true then - t = { [c] = true } + t = { [c] = true, [""] = true } p[pk] = t p = t - t = true + t = false + elseif t == false then + t = { [c] = false } + p[pk] = t + p = t + t = false else local tc = t[c] if not tc then - tc = true - t[c] = true + tc = false + t[c] = false end p = t t = tc end pk = c end - hash[s] = true + if t == false then + p[pk] = true + elseif t == true then + -- okay + else + t[""] = true + end end end - return make(tree,hash) +-- inspect(tree) + return make(tree) end +-- local t = { "a", "abc", "ac", "abe", "abxyz", "xy", "bef" } +-- local p = lpeg.Cs((lpeg.utfchartabletopattern(t)/string.upper + 1)^1) +-- +-- inspect(lpegmatch(p,"a")) +-- inspect(lpegmatch(p,"aaaa")) +-- inspect(lpegmatch(p,"ac")) +-- inspect(lpegmatch(p,"bc")) +-- inspect(lpegmatch(p,"zzbczz")) +-- inspect(lpegmatch(p,"zzabezz")) +-- inspect(lpegmatch(p,"ab")) +-- inspect(lpegmatch(p,"abc")) +-- inspect(lpegmatch(p,"abe")) +-- inspect(lpegmatch(p,"xa")) +-- inspect(lpegmatch(p,"bx")) +-- inspect(lpegmatch(p,"bax")) +-- inspect(lpegmatch(p,"abxyz")) +-- inspect(lpegmatch(p,"foobarbefcrap")) + -- lpeg.utfchartabletopattern { -- utfchar(0x00A0), -- nbsp -- utfchar(0x2000), -- enquad -- cgit v1.2.3