function writeLine(fp, s)
if not s then s, fp = fp, nil end
if fp then write(fp, s .. "\n")
else write(s .. "\n")
end
end
function chomp(s)
return gsub(s, "\n$", "")
end
function escapePattern(s)
s = gsub(s, "(%W)", "%%%1")
return s
end
function escapeShell(s)
s = gsub(s, "([ %(%)])", "\\%1")
return s
end
function join(sep, l)
local len = getn(l)
if len == 0 then return "" end
local s = l[1]
for i = 2, len do s = s .. sep .. l[i] end
return s
end
function pad(s, width, padder)
padder = strrep(padder or " ", abs(width))
if width < 0 then return strsub(padder .. s, width) end
return strsub(s .. padder, 1, width)
end
function wrap(s, w, i1, i2)
w = w or 78
i1 = i1 or 0
i2 = i2 or 0
affirm(i1 < w and i2 < w,
"wrap: the indents must be less than the line width")
s = strrep(" ", i1) .. s
local lstart, len = 1, strlen(s)
while len - lstart > w do
local i = lstart + w
while i > lstart and strsub(s, i, i) ~= " " do i = i - 1 end
local j = i
while j > lstart and strsub(s, j, j) == " " do j = j - 1 end
s = strsub(s, 1, j) .. "\n" .. strrep(" ", i2) ..
strsub(s, i + 1, -1)
local change = i2 + 1 - (i - j)
lstart = j + change
len = len + change
end
return s
end
function readLines()
local line = {}
repeat
local l = read("*l")
if not l then break end
tinsert(line, l)
until nil
return line
end