summaryrefslogtreecommitdiff
path: root/tex/context/base/util-tab.lua
diff options
context:
space:
mode:
authorMarius <mariausol@gmail.com>2012-09-10 02:00:22 +0300
committerMarius <mariausol@gmail.com>2012-09-10 02:00:22 +0300
commit40012d63f567ccc7ce33e0307069f35926fc5d6a (patch)
treeefdaaf9e16d0f3b562974629bf0ea3559258e72d /tex/context/base/util-tab.lua
parent5b4c6dd791dfcb02838191ccee764cbb215b7cc4 (diff)
downloadcontext-40012d63f567ccc7ce33e0307069f35926fc5d6a.tar.gz
beta 2012.09.10 00:57
Diffstat (limited to 'tex/context/base/util-tab.lua')
-rw-r--r--tex/context/base/util-tab.lua61
1 files changed, 52 insertions, 9 deletions
diff --git a/tex/context/base/util-tab.lua b/tex/context/base/util-tab.lua
index d45b58f6f..f0977743b 100644
--- a/tex/context/base/util-tab.lua
+++ b/tex/context/base/util-tab.lua
@@ -168,14 +168,6 @@ function tables.encapsulate(core,capsule,protect)
end
end
-local escaped = Cs ( (
- P('\\' ) +
- P('"' )/'\\"' +
- P('\n')/'\\n' +
- P('\r')/'\\r' +
- 1
-)^0 )
-
local function serialize(t,r,outer) -- no mixes
r[#r+1] = "{"
local n = #t
@@ -217,7 +209,19 @@ local function serialize(t,r,outer) -- no mixes
end
function table.fastserialize(t,prefix)
- return concat(serialize(t,{ prefix },true))
+ return concat(serialize(t,{ prefix or "return" },true))
+end
+
+function table.deserialize(str)
+ local code = loadstring(str)
+ if not code then
+ return
+ end
+ code = code()
+ if not code then
+ return
+ end
+ return code
end
-- inspect(table.fastserialize { a = 1, b = { 4, { 5, 6 } }, c = { d = 7, e = 'f"g\nh' } })
@@ -236,3 +240,42 @@ function table.load(filename)
end
end
end
+
+local function slowdrop(t)
+ local r = { }
+ local l = { }
+ for i=1,#t do
+ local ti = t[i]
+ local j = 0
+ for k, v in next, ti do
+ j = j + 1
+ l[j] = format("%s=%q",k,v)
+ end
+ r[i] = format(" {%s},\n",concat(l))
+ end
+ return format("return {\n%s}",concat(r))
+end
+
+local function fastdrop(t)
+ local r = { "return {\n" }
+ for i=1,#t do
+ local ti = t[i]
+ r[#r+1] = " {"
+ for k, v in next, ti do
+ r[#r+1] = format("%s=%q",k,v)
+ end
+ r[#r+1] = "},\n"
+ end
+ r[#r+1] = "}"
+ return concat(r)
+end
+
+function table.drop(t,slow)
+ if #t == 0 then
+ return "return { }"
+ elseif slow == true then
+ return slowdrop(t) -- less memory
+ else
+ return fastdrop(t) -- some 15% faster
+ end
+end