* Entering Version 0.2
[project/luci.git] / src / ffluci / util.lua
index caa8e41..082310f 100644 (file)
@@ -56,6 +56,23 @@ function class(base)
 end
 
 
+-- Clones an object (deep on-demand)
+function clone(object, deep)
+       local copy = {}
+       
+       for k, v in pairs(object) do
+               if deep and type(v) == "table" then
+                       v = clone(v, deep)
+               end
+               copy[k] = v
+       end
+       
+       setmetatable(copy, getmetatable(object))
+       
+       return copy
+end
+
+
 -- Checks whether a table has an object "value" in it
 function contains(table, value)
        for k,v in pairs(table) do
@@ -146,6 +163,28 @@ function sessionid()
        return ENV.SESSIONID
 end
 
+
+-- Splits a string into an array (Taken from lua-users.org)
+function split(str, pat)
+   local t = {}
+   local fpat = "(.-)" .. pat
+   local last_end = 1
+   local s, e, cap = str:find(fpat, 1)
+   while s do
+      if s ~= 1 or cap ~= "" then
+        table.insert(t,cap)
+      end
+      last_end = e+1
+      s, e, cap = str:find(fpat, last_end)
+   end
+   if last_end <= #str then
+      cap = str:sub(last_end)
+      table.insert(t, cap)
+   end
+   return t
+end
+
+
 -- Updates the scope of f with "extscope"
 function updfenv(f, extscope)
        local scope = getfenv(f)