1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-04 13:49:03 +00:00

Refactor some Lua string concatenations.

Using this regular expression, '\(\w*\)\s*=\s*\1\s*\.\.', found and
replaced many string concatenation-reassignments. These can cause
performance issues, since a new string gets allocated for each
reassignment. In many cases, the replacement is simply a single string,
wrapped across lines with the '\z' escape, which consumes a newline and
whitespace following it. In other cases, a table is used to hold the
substrings until the final string is built with a single table.concat
operation (same technique used in stdnse.strbuf).

Also, some string-building loops of this form:

s = ""
for i = 1, 100, 1 do
  s = s .. "\0"
end

were replaced with this much faster and cleaner version:

s = string.rep("\0", 100)
This commit is contained in:
dmiller
2014-02-18 18:10:23 +00:00
parent 74ebf1c892
commit 1c0c090ace
7 changed files with 160 additions and 175 deletions

View File

@@ -290,29 +290,27 @@ end
-- @return The corresponding path string
-----------------------------------------------------------------------------
function build_path(parsed, unsafe)
local path = ""
local path = {}
if parsed.is_absolute then path[#path+1] = "/" end
local n = #parsed
if unsafe then
for i = 1, n-1 do
path = path .. parsed[i]
path = path .. "/"
path[#path+1] = parsed[i] .. "/"
end
if n > 0 then
path = path .. parsed[n]
if parsed.is_directory then path = path .. "/" end
path[#path+1] = parsed[n]
if parsed.is_directory then path[#path+1] = "/" end
end
else
for i = 1, n-1 do
path = path .. protect_segment(parsed[i])
path = path .. "/"
path[#path+1] = protect_segment(parsed[i]) .. "/"
end
if n > 0 then
path = path .. protect_segment(parsed[n])
if parsed.is_directory then path = path .. "/" end
path[#path+1] = protect_segment(parsed[n])
if parsed.is_directory then path[#path+1] = "/" end
end
end
if parsed.is_absolute then path = "/" .. path end
return path
return table.concat(path)
end
---