Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- common.lua: functions from my LUA_INIT file.
-- This file:
--   http://angg.twu.net/dednat5/common.lua.html
--   http://angg.twu.net/dednat5/common.lua
--                    (find-dn5 "common.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2011dec03
-- License: GPL3

-- (find-blogme4 "common.lua")
-- (find-angg "LUA/lua50init.lua" "PP")


-- «.readfile»		(to "readfile")
-- «.writefile»		(to "writefile")
-- «.split»		(to "split")
-- «.untabify»		(to "untabify")
-- «.printf»		(to "printf")
-- «.PP»		(to "PP")
-- «.pack-and-unpack»	(to "pack-and-unpack")
-- «.guill»		(to "guill")
-- «.gformat»		(to "gformat")
-- «.ee_template»	(to "ee_template")
-- «.errors»		(to "errors")



-- «readfile»  (to ".readfile")
-- «writefile»  (to ".writefile")
-- (find-angg "LUA/lua50init.lua" "readfile")
-- (find-angg "LUA/lua50init.lua" "writefile")
readfile = function (fname)
    local f = assert(io.open(fname, "r"))
    local bigstr = f:read("*a")
    f:close()
    return bigstr
  end
writefile = function (fname, bigstr)
    local f = assert(io.open(fname, "w+"))
    f:write(bigstr)
    f:close()
  end

-- (find-angg "LUA/lua50init.lua" "mapconcat")
map = function (f, arr, n)
    local brr = {}
    for i=1,(n or #arr) do table.insert(brr, f(arr[i])) end
    return brr
  end
mapconcat = function (f, tbl, sep) return table.concat(map(f, tbl), sep) end
nop = function () end
id  = function (...) return ... end

-- (find-luamanualw3m "#pdf-table.sort")
sorted = function (tbl, lt) table.sort(tbl, lt); return tbl end

-- «split»  (to ".split")
-- (find-angg "LUA/lua50init.lua" "split")
split = function (str, pat)
    local arr = {}
    string.gsub(str, pat or "([^%s]+)", function (word)
        table.insert(arr, word)
      end)
    return arr
  end

-- «untabify»  (to ".untabify")
-- (find-angg "LUA/lua50init.lua" "untabify")
-- Note: to untabify strings in encodings where chars can be more than
-- 1-byte long, change the "#" below... (I never had to do that,
-- though).
untabify_table =
  {"        ", "       ", "      ", "     ", "    ", "   ", "  ", " "}
--{"--------", "-------", "------", "-----", "----", "---", "--", "-"}
untabify_strtab = function (strbeforetab)
    return strbeforetab ..
      untabify_table[math.fmod(#strbeforetab, 8) + 1]
  end
untabify = function (str)
    return (gsub(str, "([^\t\r\n]*)\t", untabify_strtab))
  end

-- (find-angg "LUA/lua50init.lua" "mytostring")
tos_compare_pairs = function (pair1, pair2)
    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
tos_sorted_pairs = function (T)
    local Tpairs = {}
    for key,val in pairs(T) do
      table.insert(Tpairs, {key=key, val=val})
    end
    return sorted(Tpairs, tos_compare_pairs)
  end
tos_table_orig = function (T, sep)
    return "{"..mapconcat(tos_pair, tos_sorted_pairs(T), sep or ", ").."}"
  end
tos_table = tos_table_orig
tos = function (o)
    local t = type(o)
    if t=="number" then return tostring(o) end
    if t=="string" then return string.format("%q", o) end
    if t=="table"  then return tos_table(o) end
    return "<"..tostring(o)..">"
  end
tos_key = tos              -- change this to print string keys differently
tos_pair = function (pair)
    return tos_key(pair.key).."="..tos(pair.val)
  end

mysort = tos_sorted_pairs   -- compatibility
mytostring = tos            -- compatibility
mytostring_arg = function (T, sep)
    return mapconcat(tos, T, sep or " ", T.n)
  end

-- Tools for building extensions
tos_good_string_key = function (key)
    return type(key) == "string" and key:match("^[A-Za-z_][A-Za-z_0-9]*$")
  end
tos_has_tostring = function (o)
    return getmetatable(T) and getmetatable(T).__tostring
  end
tos_has_eootype = function (o)
    return type(o) == "table" and getmetatable(o) and getmetatable(o).type
  end


-- «printf»  (to ".printf")
-- (find-angg "LUA/lua50init.lua" "printf")
printf = function (...) io.write(string.format(...)) end


-- «PP»  (to ".PP")
-- (find-angg "LUA/lua50init.lua" "PP")
-- Examples:
--  PP(nil, true, false, 22, "22", "a\nb", print, nil)
-->   <nil> <true> <false> 22 "22" "a\
--    b" <function: 0x806b388> <nil>
--
--  PP({44, 55, nil, 77, [{a=11}]={[22]="b"}, [{}]={}, [{}]={}})
-->    {1=44, 2=55, 4=77, {"a"=11}={22="b"}, {}={}, {}={}}
--
PP = function (...)
    -- local arg = arg or pack(...)   -- for Lua 5.2
    local arg = pack(...)   -- for Lua 5.2
    for i=1,arg.n do printf(" %s", mytostring(arg[i])) end
    printf("\n")
    return myunpack(arg)    -- todo: change to "..." (a 5.1-ism)
  end


-- «pack-and-unpack»  (to ".pack-and-unpack")
-- (find-angg "LUA/lua50init.lua" "pack-and-unpack")
-- (find-luamanualw3m "#pdf-unpack")
-- pack  = table.pack or function (...) return arg end
pack     = table.pack or function (...) return {n=select("#", ...), ...} end
unpack   = unpack or table.unpack
myunpack = function (arg) return unpack(arg, 1, arg.n) end


-- «guill»  (to ".guill")
-- «gformat»  (to ".gformat")
-- «ee_template»  (to ".ee_template")
-- (find-angg "LUA/lua50init.lua" "gformat")
-- (find-angg "LUA/lua50init.lua" "ee_template")
-- These are mostly for build.lua...
guill = function (str) return (str:gsub("<<", "\171"):gsub(">>", "\187")) end
gformat = function (fmt, pat)
    return function (str)
        return (str:gsub((pat or "^.*$"), fmt))
      end
  end
ee_template = function (pairs, templatestr)
    return (string.gsub(templatestr, "{([^{}]+)}", pairs))
  end


-- «errors»  (to ".errors")
-- (find-dn5 "errors.lua")







-- dump-to: tests
--[==[
--]==]

-- Local Variables:
-- coding:             raw-text-unix
-- ee-anchor-format:   "«%s»"
-- End: