libs/core: Backported code optimizations

This commit is contained in:
Steven Barth 2008-09-12 07:48:36 +00:00
parent 78a3ecd136
commit 8f8da32d7e
4 changed files with 36 additions and 42 deletions

View file

@ -190,7 +190,7 @@ function source.rewind(src)
if not chunk then return src() if not chunk then return src()
else return chunk end else return chunk end
else else
table.insert(t, chunk) t[#t+1] = chunk
end end
end end
end end
@ -277,7 +277,7 @@ end
function sink.table(t) function sink.table(t)
t = t or {} t = t or {}
local f = function(chunk, err) local f = function(chunk, err)
if chunk then table.insert(t, chunk) end if chunk then t[#t+1] = chunk end
return 1 return 1
end end
return f, t return f, t

View file

@ -60,6 +60,17 @@ end
-- Class helper routines -- 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). --- Create a Class object (Python-style object model).
-- The class object can be instantiated by calling itself. -- The class object can be instantiated by calling itself.
-- Any class functions or shared parameters can be attached to this object. -- Any class functions or shared parameters can be attached to this object.
@ -75,26 +86,10 @@ end
-- @see instanceof -- @see instanceof
-- @see clone -- @see clone
function class(base) function class(base)
local class = {} return setmetatable({}, {
__call = _instantiate,
local create = function(class, ...) __index = base
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
end end
--- Test whether the given object is an instance of the given class. --- Test whether the given object is an instance of the given class.
@ -200,13 +195,13 @@ end
-- @param value String value containing the data to escape -- @param value String value containing the data to escape
-- @return String value containing the escaped data -- @return String value containing the escaped data
function pcdata(value) function pcdata(value)
if not value then return end return value and tostring(value):gsub("[&\"'<>]", {
value = tostring(value) ["&"] = "&amp;",
value = value:gsub("&", "&amp;") ['"'] = "&quot;",
value = value:gsub('"', "&quot;") ["'"] = "&apos;",
value = value:gsub("'", "&apos;") ["<"] = "&lt;",
value = value:gsub("<", "&lt;") [">"] = "&gt;"
return value:gsub(">", "&gt;") })
end end
--- Strip HTML tags from given string. --- Strip HTML tags from given string.
@ -250,9 +245,9 @@ function split(str, pat, max, regex)
local s, e = str:find(pat, c, not regex) local s, e = str:find(pat, c, not regex)
max = max - 1 max = max - 1
if s and max < 0 then if s and max < 0 then
table.insert(t, str:sub(c)) t[#t+1] = str:sub(c)
else else
table.insert(t, str:sub(c, s and s - 1)) t[#t+1] = str:sub(c, s and s - 1)
end end
c = e and e + 1 or #str + 1 c = e and e + 1 or #str + 1
until not s or max < 0 until not s or max < 0
@ -335,7 +330,7 @@ function combine(...)
local result = {} local result = {}
for i, a in ipairs(arg) do for i, a in ipairs(arg) do
for j, v in ipairs(a) do for j, v in ipairs(a) do
table.insert(result, v) result[#result+1] = v
end end
end end
return result return result
@ -372,7 +367,7 @@ function keys(t)
local keys = { } local keys = { }
if t then if t then
for k, _ in kspairs(t) do for k, _ in kspairs(t) do
table.insert( keys, k ) keys[#keys+1] = k
end end
end end
return keys return keys
@ -569,17 +564,16 @@ function _sortiter( t, f )
local keys = { } local keys = { }
for k, v in pairs(t) do for k, v in pairs(t) do
table.insert( keys, k ) keys[#keys+1] = k
end end
local _pos = 0 local _pos = 0
local _len = table.getn( keys )
table.sort( keys, f ) table.sort( keys, f )
return function() return function()
_pos = _pos + 1 _pos = _pos + 1
if _pos <= _len then if _pos <= #keys then
return keys[_pos], t[keys[_pos]] return keys[_pos], t[keys[_pos]]
end end
end end
@ -658,7 +652,7 @@ function execl(command)
while true do while true do
line = pp:read() line = pp:read()
if (line == nil) then break end if (line == nil) then break end
table.insert(data, line) data[#data+1] = line
end end
pp:close() pp:close()

View file

@ -88,7 +88,7 @@ function Cursor.delete_all(self, config, stype, comparator)
local function helper (section) local function helper (section)
if not comparator or comparator(section) then if not comparator or comparator(section) then
table.insert(del, section[".name"]) del[#del+1] = section[".name"]
end end
end end
@ -201,14 +201,14 @@ function Cursor._affected(self, configlist)
function(section) function(section)
if section.affects then if section.affects then
for i, aff in ipairs(section.affects) do for i, aff in ipairs(section.affects) do
table.insert(deps, aff) deps[#deps+1] = aff
end end
end end
end) end)
for i, dep in ipairs(deps) do for i, dep in ipairs(deps) do
for j, add in ipairs(_resolve_deps(dep)) do for j, add in ipairs(_resolve_deps(dep)) do
table.insert(reload, add) reload[#reload+1] = add
end end
end end
@ -219,7 +219,7 @@ function Cursor._affected(self, configlist)
for j, config in ipairs(configlist) do for j, config in ipairs(configlist) do
for i, e in ipairs(_resolve_deps(config)) do for i, e in ipairs(_resolve_deps(config)) do
if not util.contains(reloadlist, e) then if not util.contains(reloadlist, e) then
table.insert(reloadlist, e) reloadlist[#reloadlist+1] = e
end end
end end
end end

View file

@ -63,7 +63,7 @@ function compile(template)
-- Search all <% %> expressions -- Search all <% %> expressions
local function expr_add(ws1, skip1, command, skip2, ws2) local function expr_add(ws1, skip1, command, skip2, ws2)
table.insert(expr, command) expr[#expr+1] = command
return ( #skip1 > 0 and "" or ws1 ) .. return ( #skip1 > 0 and "" or ws1 ) ..
"<%" .. tostring(#expr) .. "%>" .. "<%" .. tostring(#expr) .. "%>" ..
( #skip2 > 0 and "" or ws2 ) ( #skip2 > 0 and "" or ws2 )