* Last API changes before 0.4 API softfreeze
[project/luci.git] / core / src / ffluci / util.lua
index dfc88e3..9e3c7f2 100644 (file)
@@ -139,45 +139,40 @@ function resfenv(f)
 end 
 
 
--- Returns the Haserl unique sessionid
-function sessionid()
-       return ENV.SESSIONID
-end
-
-
--- Splits a string into an array (Adapted from lua-users.org)
-function split(str, pat, max)
+-- Splits a string into an array
+function split(str, pat, max, regex)
        pat = pat or "\n"
-       max = max or -1
+       max = max or #str
        
        local t = {}
-       local fpat = "(.-)" .. pat
-       local last_end = 1
-       local s, e, cap = str:find(fpat, 1)
+       local c = 1
        
-       while s do
-               max = max - 1
-               if s ~= 1 or cap ~= "" then
-                       table.insert(t,cap)
-               end
-               last_end = e+1
-               if max == 0 then
-                       break
-               end
-               s, e, cap = str:find(fpat, last_end)
+       if #str == 0 then
+               return {""}
+       end
+       
+       if #pat == 0 then
+               return nil
        end
        
-       if last_end <= #str then
-               cap = str:sub(last_end)
-               table.insert(t, cap)
+       if max == 0 then
+               return str
        end
        
+       repeat
+               local s, e = str:find(pat, c, not regex)
+               table.insert(t, str:sub(c, s and s - 1))
+               max = max - 1
+               c = e and e + 1 or #str + 1
+       until not s or max < 0
+       
        return t
 end
 
 -- Removes whitespace from beginning and end of a string
-function trim (string)
-       return string:gsub("^%s*(.-)%s*$", "%1")
+function trim(str)
+       local s = str:gsub("^%s*(.-)%s*$", "%1")
+       return s
 end
 
 -- Updates given table with new values