libs/core: improve luci.util.imatch() to not create temporary strings when iterating non-string values

This commit is contained in:
Jo-Philipp Wich 2012-06-26 22:58:24 +00:00
parent a99f570bf4
commit 9fab594101

View file

@ -36,7 +36,7 @@ local tparser = require "luci.template.parser"
local getmetatable, setmetatable = getmetatable, setmetatable local getmetatable, setmetatable = getmetatable, setmetatable
local rawget, rawset, unpack = rawget, rawset, unpack local rawget, rawset, unpack = rawget, rawset, unpack
local tostring, type, assert = tostring, type, assert local tostring, type, assert = tostring, type, assert
local ipairs, pairs, loadstring = ipairs, pairs, loadstring local ipairs, pairs, next, loadstring = ipairs, pairs, next, loadstring
local require, pcall, xpcall = require, pcall, xpcall local require, pcall, xpcall = require, pcall, xpcall
local collectgarbage, get_memory_limit = collectgarbage, get_memory_limit local collectgarbage, get_memory_limit = collectgarbage, get_memory_limit
@ -273,15 +273,27 @@ end
-- @param val The value to scan (table, string or nil) -- @param val The value to scan (table, string or nil)
-- @return Iterator which returns one token per call -- @return Iterator which returns one token per call
function imatch(v) function imatch(v)
if v == nil then if type(v) == "table" then
v = "" local k = nil
elseif type(v) == "table" then return function()
v = table.concat(v, " ") k = next(v, k)
elseif type(v) ~= "string" then return v[k]
v = tostring(v) end
elseif type(v) == "number" or type(v) == "boolean" then
local x = true
return function()
if x then
x = false
return tostring(v)
end
end
elseif type(v) == "userdata" or type(v) == "string" then
return tostring(v):gmatch("%S+")
end end
return v:gmatch("%S+") return function() end
end end
--- Parse certain units from the given string and return the canonical integer --- Parse certain units from the given string and return the canonical integer