|
Warning: this is an htmlized version!
The original is across this link, and the conversion rules are here. |
-- -*- coding: raw-text -*-
-- Based on:
-- (find-angg "LUA/lua50init.lua" "mytostring")
-- tos_compare_keyvals({key="a", val="aa"}, {key=2, val=22})
-- --> true
-- tos_sorted_keyvals({a="aa", [2]=22})
-- --> {{key=2, val=22}, {key="a", val="aa"}}
-- tos_keyval({key="a", val="aa"})
-- --> '"a"="aa"'
tos_compare_keyvals = function (keyval1, keyval2)
local key1, key2 = keyval1.key, keyval2.key
local type1, type2 = type(key1), type(key2)
if type1 == type2 then
if type1 == "number" then return key1 < key2 end
if type1 == "string" then return key1 < key2 end
return tos(key1) < tos(key2)
else
return type1 < type2
end
end
tos_sorted_keyvals = function (tbl)
local keyvaltbl = {}
for key,val in pairs(tbl) do
table.insert(keyvaltbl, {key=key, val=val})
end
table.sort(keyvaltbl, tos_compare_keyvals)
return keyvaltbl
end
tos_keyval = function (keyval)
return tos(keyval.key).."="..tos(keyval.val)
end
tos_table = function (tbl, sep)
local keyvaltbl = tos_sorted_keyvals(tbl)
local images = map(tos_keyval, keyvaltbl)
return "{"..table.concat(images, sep or ", ").."}"
end
tos_number = function (n) return tostring(n) end
tos_string = function (s) return string.format("%q", s) end
tos_other = function (o) return "<"..tostring(o)..">" end
tos = function (o)
local t = type(o)
if t == "number" then return tos_number(o) end
if t == "string" then return tos_string(o) end
if t == "table" then return tos_table(o) end
return tos_other(o)
end
tos_arg = function (arg, sep)
local images = map(tos, arg, arg.n)
return table.concat(images, sep or " ")
end
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "~/LUA/tos.lua"
= tos{}
= tos{a="aa", 22}
= tos({{"a", b="bb", 22}, {}, print})
--]]