More luci.util optimizations
[project/luci.git] / libs / core / luasrc / util.lua
index 5f9c609..95491cf 100644 (file)
@@ -59,6 +59,17 @@ end
 -- Class helper routines
 --
 
+-- Instantiates a class
+local function _instantiate(class, ...)
+       local inst = setmetatable({}, {__index = class})
+
+       if inst.__init__ then
+               inst:__init__(...)
+       end
+
+       return inst
+end
+
 --- Create a Class object (Python-style object model).
 -- The class object can be instantiated by calling itself.
 -- Any class functions or shared parameters can be attached to this object.
@@ -74,26 +85,10 @@ end
 -- @see                        instanceof
 -- @see                        clone
 function class(base)
-       local class = {}
-
-       local create = function(class, ...)
-               local inst = setmetatable({}, {__index = class})
-
-               if inst.__init__ then
-                       inst:__init__(...)
-               end
-
-               return inst
-       end
-
-       local classmeta = {__call = create}
-
-       if base then
-               classmeta.__index = base
-       end
-
-       setmetatable(class, classmeta)
-       return class
+       return setmetatable({}, {
+               __call  = _instantiate,
+               __index = base
+       })
 end
 
 --- Test whether the given object is an instance of the given class.
@@ -199,13 +194,13 @@ end
 -- @param value        String value containing the data to escape
 -- @return             String value containing the escaped data
 function pcdata(value)
-       if not value then return end
-       value = tostring(value)
-       value = value:gsub("&", "&")
-       value = value:gsub('"', """)
-       value = value:gsub("'", "'")
-       value = value:gsub("<", "&lt;")
-       return value:gsub(">", "&gt;")
+       return value and tostring(value):gsub("[&\"'<>]", {
+               ["&"] = "&amp;",
+               ['"'] = "&quot;",
+               ["'"] = "&apos;",
+               ["<"] = "&lt;",
+               [">"] = "&gt;"
+       })
 end
 
 --- Strip HTML tags from given string.
@@ -572,13 +567,12 @@ function _sortiter( t, f )
        end
 
        local _pos = 0
-       local _len = table.getn( keys )
 
        table.sort( keys, f )
 
        return function()
                _pos = _pos + 1
-               if _pos <= _len then
+               if _pos <= #keys then
                        return keys[_pos], t[keys[_pos]]
                end
        end