General optimizations, simplifications and improvements
[project/luci.git] / libs / core / luasrc / util.lua
index 2ede71a..6cefb8e 100644 (file)
@@ -24,8 +24,21 @@ limitations under the License.
 
 ]]--
 
+local io = require "io"
+local table = require "table"
+local debug = require "debug"
+local string = require "string"
+local coroutine = require "coroutine"
+
+local getmetatable, setmetatable = getmetatable, setmetatable
+local getfenv, setfenv = getfenv, setfenv
+local rawget, rawset, unpack = rawget, rawset, unpack
+local tostring, type, assert = tostring, type, assert 
+local ipairs, pairs, loadstring = ipairs, pairs, loadstring
+local require, pcall, xpcall = require, pcall, xpcall
+
 --- LuCI utility functions.
-module("luci.util", package.seeall)
+module "luci.util"
 
 --
 -- Pythonic string formatting extension
@@ -63,14 +76,10 @@ function class(base)
        local class = {}
 
        local create = function(class, ...)
-               local inst = {}
-               setmetatable(inst, {__index = class})
+               local inst = setmetatable({}, {__index = class})
 
                if inst.__init__ then
-                       local stat, err = copcall(inst.__init__, inst, ...)
-                       if not stat then
-                               error(err)
-                       end
+                       inst:__init__(...)
                end
 
                return inst
@@ -183,18 +192,18 @@ end
 
 --- Recursively dumps a table to stdout, useful for testing and debugging.
 -- @param t    Table value to dump
--- @param i    Number of tabs to prepend to each line
+-- @param maxdepth     Maximum depth
 -- @return     Always nil
-function dumptable(t, i, seen)
+function dumptable(t, maxdepth, i, seen)
        i = i or 0
        seen = seen or setmetatable({}, {__mode="k"})
        
        for k,v in pairs(t) do
                perror(string.rep("\t", i) .. tostring(k) .. "\t" .. tostring(v))
-               if type(v) == "table" then
+               if type(v) == "table" and (not maxdepth or i < maxdepth) then
                        if not seen[v] then
                                seen[v] = true
-                               dumptable(v, i+1, seen)
+                               dumptable(v, maxdepth, i+1, seen)
                        else
                                perror(string.rep("\t", i) .. "*** RECURSION ***")
                        end
@@ -284,8 +293,7 @@ end
 -- @param str  String value containing whitespace padded data
 -- @return             String value with leading and trailing space removed
 function trim(str)
-       local s = str:gsub("^%s*(.-)%s*$", "%1")
-       return s
+       return (str:gsub("^%s*(.-)%s*$", "%1"))
 end
 
 --- Parse certain units from the given string and return the canonical integer
@@ -413,9 +421,7 @@ function clone(object, deep)
                copy[k] = v
        end
 
-       setmetatable(copy, getmetatable(object))
-
-       return copy
+       return setmetatable(copy, getmetatable(object))
 end
 
 
@@ -436,14 +442,27 @@ function _serialize_table(t, seen)
        assert(not seen[t], "Recursion detected.")
        seen[t] = true
        
-       local data = ""
+       local data  = ""
+       local idata = ""
+       local ilen  = 0
+
        for k, v in pairs(t) do
-               k = serialize_data(k, seen)
-               v = serialize_data(v, seen)
-               data = data .. ( #data > 0 and ", " or "" ) ..
-                       '[' .. k .. '] = ' .. v
+               if type(k) ~= "number" or k < 1 or math.floor(k) ~= k or ( k - #t ) > 3 then
+                       k = serialize_data(k, seen)
+                       v = serialize_data(v, seen)
+                       data = data .. ( #data > 0 and ", " or "" ) ..
+                               '[' .. k .. '] = ' .. v
+               elseif k > ilen then
+                       ilen = k
+               end
        end
-       return data
+
+       for i = 1, ilen do
+               local v = serialize_data(t[i], seen)
+               idata = idata .. ( #idata > 0 and ", " or "" ) .. v
+       end             
+
+       return idata .. ( #data > 0 and #idata > 0 and ", " or "" ) .. data
 end
 
 --- Recursively serialize given data to lua code, suitable for restoring
@@ -460,11 +479,11 @@ function serialize_data(val, seen)
        elseif type(val) == "number" then
                return val
        elseif type(val) == "string" then
-               return string.format("%q", val)
+               return "%q" % val
        elseif type(val) == "boolean" then
                return val and "true" or "false"
        elseif type(val) == "function" then
-               return string.format("loadstring(%q)", get_bytecode(val))
+               return "loadstring(%q)" % get_bytecode(val)
        elseif type(val) == "table" then
                return "{ " .. _serialize_table(val, seen) .. " }"
        else
@@ -678,7 +697,7 @@ end
 --- Returns the absolute path to LuCI base directory.
 -- @return             String containing the directory path
 function libpath()
-       return luci.fs.dirname(require("luci.debug").__file__)
+       return require "luci.fs".dirname(require "luci.debug".__file__)
 end