forked from redo/BlockLua
improve readme, improve std lib
This commit is contained in:
@@ -5,8 +5,7 @@
|
||||
-- Table / List
|
||||
-- Whether a table contains no keys
|
||||
function table.empty(t)
|
||||
for _,_ in pairs(t) do return false end
|
||||
return true
|
||||
return next(t)~=nil
|
||||
end
|
||||
-- Apply a function to each key in a table
|
||||
function table.map(f, ...)
|
||||
@@ -41,18 +40,6 @@ function table.reverse(l)
|
||||
for i=1,#l do m[#l-i+1] = l[i] end
|
||||
return m
|
||||
end
|
||||
-- Convert i->v to v->true
|
||||
function table.values(l)
|
||||
local u = {}
|
||||
for _,v in ipairs(l) do u[v] = true end
|
||||
return u
|
||||
end
|
||||
-- Make a list of keys
|
||||
function table.keys(t)
|
||||
local u = {}
|
||||
for k,_ in pairs(t) do table.insert(u, k) end
|
||||
return u
|
||||
end
|
||||
-- Whether a table is a list/array (has only monotonic integer keys)
|
||||
function table.islist(t)
|
||||
local n = 0
|
||||
@@ -244,8 +231,8 @@ function string.bytes(s)
|
||||
end
|
||||
-- Trim leading and trailing whitespace
|
||||
function string.trim(s, ws)
|
||||
ws = ws or '[ \t\r\t]'
|
||||
return s:gsub('^'..ws..'+', ''):gsub(ws..'+$', '')..''
|
||||
ws = ws or ' \t\r\n'
|
||||
return s:gsub('^['..ws..']+', ''):gsub('['..ws..']+$', '')..''
|
||||
end
|
||||
-- String slicing and searching using [] operator
|
||||
local str_meta = getmetatable('')
|
||||
@@ -325,7 +312,7 @@ end
|
||||
|
||||
io = io or {}
|
||||
-- Read entire file at once, return nil,err if access failed
|
||||
function io.readfile(filename)
|
||||
function io.readall(filename)
|
||||
local fi,err = io.open(filename, 'rb')
|
||||
if not fi then return nil,err end
|
||||
local s = fi:read("*a")
|
||||
@@ -333,7 +320,7 @@ function io.readfile(filename)
|
||||
return s
|
||||
end
|
||||
-- Write data to file all at once, return true if success / false,err if failure
|
||||
function io.writefile(filename, data)
|
||||
function io.writeall(filename, data)
|
||||
local fi,err = io.open(filename, 'wb')
|
||||
if not fi then return false,err end
|
||||
fi:write(data)
|
||||
|
||||
Reference in New Issue
Block a user