mirror of
https://github.com/nmap/nmap.git
synced 2025-12-12 02:39:03 +00:00
Avoid excessive string concatenations
psl_tree now takes an accumulator argument. It adds lines to the result table instead of concatenating them together in a string. Then psl_print concats all the lines together.
This commit is contained in:
@@ -105,10 +105,10 @@ end
|
|||||||
|
|
||||||
function psl_print (psl, lvl)
|
function psl_print (psl, lvl)
|
||||||
-- Print out table header.
|
-- Print out table header.
|
||||||
local result = ""
|
local result = {}
|
||||||
if lvl == 2 then
|
if lvl == 2 then
|
||||||
result = result .. " PID PPID Priority Threads Handles\n"
|
result[#result+1] = " PID PPID Priority Threads Handles\n"
|
||||||
result = result .. "----- ----- -------- ------- -------\n"
|
result[#result+1] = "----- ----- -------- ------- -------\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Find how many root processes there are.
|
-- Find how many root processes there are.
|
||||||
@@ -129,13 +129,13 @@ function psl_print (psl, lvl)
|
|||||||
-- Print out each root of the tree.
|
-- Print out each root of the tree.
|
||||||
for i, root in ipairs(roots) do
|
for i, root in ipairs(roots) do
|
||||||
local mode = psl_mode(roots, i)
|
local mode = psl_mode(roots, i)
|
||||||
result = result .. psl_tree(psl, root, 0, bars, mode, lvl)
|
psl_tree(psl, root, 0, bars, mode, lvl, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
return result
|
return table.concat(result)
|
||||||
end
|
end
|
||||||
|
|
||||||
function psl_tree (psl, pid, column, bars, mode, lvl)
|
function psl_tree (psl, pid, column, bars, mode, lvl, result)
|
||||||
local ps = psl[pid]
|
local ps = psl[pid]
|
||||||
|
|
||||||
-- Delete vertical sibling link.
|
-- Delete vertical sibling link.
|
||||||
@@ -146,23 +146,13 @@ function psl_tree (psl, pid, column, bars, mode, lvl)
|
|||||||
-- Print information table.
|
-- Print information table.
|
||||||
local info = ""
|
local info = ""
|
||||||
if lvl == 2 then
|
if lvl == 2 then
|
||||||
info = info .. string.format("% 5d ", ps.pid)
|
info = string.format("% 5d % 5d % 8d % 7d % 7d ", ps.pid, ps.ppid, ps.prio, ps.thrd, ps.hndl)
|
||||||
info = info .. string.format("% 5d ", ps.ppid)
|
|
||||||
info = info .. string.format("% 8d ", ps.prio)
|
|
||||||
info = info .. string.format("% 7d ", ps.thrd)
|
|
||||||
info = info .. string.format("% 7d ", ps.hndl)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Print vertical sibling bars.
|
-- Print vertical sibling bars.
|
||||||
local prefix = ""
|
local prefix = ""
|
||||||
local i = 1
|
for i=1, #bars do
|
||||||
for j = 1, column do
|
prefix = prefix .. string.rep(" ", bars[i] - 1) .. "|"
|
||||||
if bars[i] == j then
|
|
||||||
prefix = prefix .. "|"
|
|
||||||
i = i + 1
|
|
||||||
else
|
|
||||||
prefix = prefix .. " "
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Strings used to separate processes from one another.
|
-- Strings used to separate processes from one another.
|
||||||
@@ -174,7 +164,7 @@ function psl_tree (psl, pid, column, bars, mode, lvl)
|
|||||||
}
|
}
|
||||||
|
|
||||||
-- Format process itself.
|
-- Format process itself.
|
||||||
local result = "\n" .. info .. prefix .. separators[mode] .. ps.name
|
result[#result+1] = "\n" .. info .. prefix .. separators[mode] .. ps.name
|
||||||
|
|
||||||
-- Find children of the process.
|
-- Find children of the process.
|
||||||
local children = {}
|
local children = {}
|
||||||
@@ -194,7 +184,7 @@ function psl_tree (psl, pid, column, bars, mode, lvl)
|
|||||||
-- Format process's children.
|
-- Format process's children.
|
||||||
for i, pid in ipairs(children) do
|
for i, pid in ipairs(children) do
|
||||||
local mode = psl_mode(children, i)
|
local mode = psl_mode(children, i)
|
||||||
result = result .. psl_tree(psl, pid, column, bars, mode, lvl)
|
psl_tree(psl, pid, column, bars, mode, lvl, result)
|
||||||
end
|
end
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user