Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- tos.lua: a "tostring" class for varios kinds of pretty-printers. -- This file: -- http://angg.twu.net/dednat5/tos.lua.html -- http://angg.twu.net/dednat5/tos.lua -- (find-dn5 "tos.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- Version: 2011dec20 / 2012mar16 -- License: GPL3 -- -- Ancestors: -- (find-angg "LUA/lua50init.lua" "tos") -- (find-angg "LUA/tos.lua") -- (find-angg "LUA/tos2.lua") require "eoo" -- (find-dn5 "eoo.lua") -- ee_dofile "~/dednat5/eoo.lua" Tos = Class { type = "Tos", __index = { -- (find-angg "LUA/lua50init.lua" "tos") compare_pairs = function (pair1, pair2) -- not a method local key1, key2 = pair1.key, pair2.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 tostring(key1) < tostring(key2) -- fast else return type1 < type2 -- numbers before strings before tables, etc end end, sorted_pairs = function (tos, T) -- sort a list of {key, val} pairs local P = {} for key,val in pairs(T) do table.insert(P, {key=key, val=val}) end return sorted(P, tos.compare_pairs) end, pair = function (tos, pair) -- {key, val} pair to string return tos:o(pair.key).."="..tos:o(pair.val) end, table = function (tos, T, sep) -- table to string local tos_pair = function (p) return tos:pair(p) end return "{"..mapconcat(tos_pair, tos:sorted_pairs(T), sep or ", ").."}" end, o = function (tos, o) -- (arbitrary) object to string local t = type(o) if t=="number" then return tostring(o) end if t=="string" then return format("%q", o) end if t=="table" then return tos:table(o) end return "<"..tostring(o)..">" end }, } -- (find-angg "LUA/lua50init.lua" "PP") -- dump-to: tests --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) require "tos" tos = Tos {} p = function (o) print(tos:o(o)) end p(22) p "234" p {10, 20, 30} p {10, 20, 30, [{2, 3}]=print} --]==] -- Local Variables: -- coding: raw-text-unix -- ee-anchor-format: "«%s»" -- End: