forked from Wiladams/LAPHLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathltin.lua
More file actions
54 lines (47 loc) · 1.48 KB
/
Copy pathltin.lua
File metadata and controls
54 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local table = require('table')
local ltin = {}
local sandbox = {}
-- Parse ltin code by reusing the lua parser in a sandbox
function ltin.decode(string)
local fn, message = loadstring("return " .. string)
if not fn then
error(message)
end
setfenv(fn, sandbox)
return fn()
end
-- stringify data to ltin using a pretty-printer
function ltin.encode(value)
local t = type(value)
if t == "number" or t == "boolean" or t == "nil" then
return tostring(value)
end
if t == "string" then
return '"' .. value:gsub("\\", "\\\\")
:gsub("%z", "\\0"):gsub("\a", "\\a"):gsub("\b", "\\b")
:gsub("\f", "\\f"):gsub("\n", "\\n"):gsub("\r", "\\r")
:gsub("\t", "\\t"):gsub("\v", "\\v"):gsub('"', '\\"') .. '"'
end
if t == 'table' then
local parts = {}
local index = 1
for key, item in pairs(value) do
local keyString
if key == index then
keyString = ""
elseif type(key) == "string" and key:match("^[_%a][_%w]*$") then
keyString = key .. "="
else
keyString = "[" .. ltin.stringify(key) .. "]="
end
parts[index] = keyString .. ltin.stringify(item)
index = index + 1
end
return "{" .. table.concat(parts, ",") .. "}"
end
return "'" .. tostring(value):gsub("\\", "\\\\")
:gsub("%z", "\\0"):gsub("\a", "\\a"):gsub("\b", "\\b")
:gsub("\f", "\\f"):gsub("\n", "\\n"):gsub("\r", "\\r")
:gsub("\t", "\\t"):gsub("\v", "\\v"):gsub("'", "\\'") .. "'"
end
return ltin