You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.9 KiB
122 lines
2.9 KiB
#!/usr/bin/env lua5.1
|
|
-- Lua utils
|
|
|
|
-- copy a table
|
|
function table.deepcopy(object)
|
|
local lookup_table = {}
|
|
local function _copy(object)
|
|
if type(object) ~= "table" then
|
|
return object
|
|
elseif lookup_table[object] then
|
|
return lookup_table[object]
|
|
end
|
|
local new_table = {}
|
|
lookup_table[object] = new_table
|
|
for index, value in pairs(object) do
|
|
new_table[_copy(index)] = _copy(value)
|
|
end
|
|
return setmetatable(new_table, getmetatable(object))
|
|
end
|
|
return _copy(object)
|
|
end
|
|
|
|
function table.contains(table, element)
|
|
if table then
|
|
for _, value in pairs(table) do
|
|
if value == element then
|
|
return true
|
|
end
|
|
end --for
|
|
end --if
|
|
return false
|
|
end
|
|
|
|
function table.val_to_str ( v )
|
|
if "string" == type( v ) then
|
|
v = string.gsub( v, "\n", "\\n" )
|
|
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
|
|
return "'" .. v .. "'"
|
|
end
|
|
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
|
|
else
|
|
return "table" == type( v ) and table.tostring( v ) or
|
|
tostring( v )
|
|
end
|
|
end
|
|
|
|
function table.key_to_str ( k )
|
|
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
|
|
return k
|
|
else
|
|
return "[" .. table.val_to_str( k ) .. "]"
|
|
end
|
|
end
|
|
|
|
function table.tostring( tbl )
|
|
local result, done = {}, {}
|
|
for k, v in ipairs( tbl ) do
|
|
table.insert( result, table.val_to_str( v ) )
|
|
done[ k ] = true
|
|
end
|
|
for k, v in pairs( tbl ) do
|
|
if not done[ k ] then
|
|
table.insert( result,
|
|
table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
|
|
end
|
|
end
|
|
return "{" .. table.concat( result, "," ) .. "}"
|
|
end
|
|
|
|
-- from table to string
|
|
-- t = {'a','b'}
|
|
-- implode(",",t,"'")
|
|
-- "'a','b'"
|
|
-- implode("#",t)
|
|
-- "a#b"
|
|
function implode(delimiter, list, quoter)
|
|
local len = #list
|
|
if not delimiter then
|
|
error("delimiter is nil")
|
|
end
|
|
if len == 0 then
|
|
return nil
|
|
end
|
|
if not quoter then
|
|
quoter = ""
|
|
end
|
|
local string = quoter .. list[1] .. quoter
|
|
for i = 2, len do
|
|
string = string .. delimiter .. quoter .. list[i] .. quoter
|
|
end
|
|
return string
|
|
end
|
|
|
|
-- from string to table
|
|
function explode(delimiter, text)
|
|
local list = {}
|
|
local pos = 1
|
|
|
|
if not delimiter then
|
|
error("delimiter is nil")
|
|
end
|
|
if not text then
|
|
error("text is nil")
|
|
end
|
|
if string.find("", delimiter, 1) then
|
|
-- We'll look at error handling later!
|
|
error("delimiter matches empty string!")
|
|
end
|
|
while 1 do
|
|
local first, last = string.find(text, delimiter, pos)
|
|
-- print (first, last)
|
|
if first then
|
|
table.insert(list, string.sub(text, pos, first-1))
|
|
pos = last+1
|
|
else
|
|
table.insert(list, string.sub(text, pos))
|
|
break
|
|
end
|
|
end
|
|
return list
|
|
end
|
|
--EOF |