Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- common.lua: functions from my LUA_INIT file. -- This file: -- http://angg.twu.net/blogme4/common.lua.html -- http://angg.twu.net/blogme4/common.lua -- (find-blogme4 "common.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- Version: 2011aug04 -- License: GPL3 -- -- «.pathto» (to "pathto") -- (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" "printf") printf = function (...) io.write(string.format(...)) end -- (find-angg "LUA/lua50init.lua" "pack-and-unpack") pack = table.pack or function (...) return {n=select("#", ...), ...} end unpack = function (T) return table.unpack(T, 1, T.n) 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-angg "LUA/lua50init.lua" "gformat") -- Examples: gformat "<%1_%1>" "foo" --> "<foo_foo>" -- mapconcat(gformat "<%1_%1>", split "foo ba", ", ") --> "<foo_foo>, <ba_ba>" gformat = function (fmt, pat) return function (str) return (str:gsub((pat or "^.*$"), fmt)) end end -- (find-angg "LUA/lua50init.lua" "split") split = function (str, pat) local T = {} string.gsub(str, pat or "([^%s]+)", function (word) T[#T+1] = word end) return T end -- (find-angg "LUA/lua50init.lua" "splitlines") splitlines = function (bigstr) local lines = split(bigstr, "([^\n]*)\n?") table.remove(lines) return lines end maplines = function (f, bigstr) return mapconcat(f, splitlines(bigstr), "\n") 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 -- (find-angg "LUA/lua50init.lua" "PP") PP = function (...) local arg = pack(...) for i=1,arg.n do printf(" %s", tos(arg[i])) end printf("\n") return ... end -- (find-angg "LUA/lua50init.lua" "translatechars") translatechars = function (str, re, tbl) return (str:gsub(re, function (c) return tbl[c] or c end)) end -- (find-angg "LUA/lua50init.lua" "each2") each2 = function (T) local i = 1 return function () if i <= #T then i = i+2; return T[i-2], T[i-1] end end end -- (find-blogme3 "blogme3.lua" "blogmedir") fnamedirectory = function (fname) return fname:match"^(.*/)[^/]*$" end fnamenondirectory = function (fname) return fname:match "([^/]*)$" end -- «pathto» (to ".pathto") -- (find-blogme4 "options.lua" "dooption_o") pathtoroot = "" pathtoroot_ = function (path) local _, nslashes = path:gsub("/", "/") return ("../"):rep(nslashes) end pathto = function (path) return pathtoroot..path end -- dump-to: tests --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) require "common" PP(maplines(gformat "<%1>", "foo\nbar\nplic")) --]==] -- Local Variables: -- coding: raw-text-unix -- ee-anchor-format: "«%s»" -- End: