1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-10 09:49:05 +00:00

Minor perf boost: use locals, not class members, for string funcs in stdnse

This commit is contained in:
dmiller
2021-12-07 19:50:46 +00:00
parent fa029ca531
commit 8a85cdd06b

View File

@@ -220,13 +220,13 @@ function make_buffer(socket, sep)
return self(); return self();
end end
else else
local i, j = buffer:find(sep, point); local i, j = find(buffer, sep, point);
if i then if i then
local ret = buffer:sub(point, i-1); local ret = sub(buffer, point, i-1);
point = j + 1; point = j + 1;
return ret; return ret;
else else
point, left, buffer = 1, buffer:sub(point), nil; point, left, buffer = 1, sub(buffer, point), nil;
return self(); return self();
end end
end end
@@ -264,8 +264,8 @@ do
-- @param n Number to convert. -- @param n Number to convert.
-- @return String in binary format. -- @return String in binary format.
function tobinary(n) function tobinary(n)
assert(tonumber(n), "number expected"); -- enforced by string.format: assert(tonumber(n), "number expected");
return (("%x"):format(n):gsub("%w", t)) return gsub(format("%x", n), "%w", t)
end end
end end
@@ -274,8 +274,8 @@ end
-- @param n Number to convert. -- @param n Number to convert.
-- @return String in octal format. -- @return String in octal format.
function tooctal(n) function tooctal(n)
assert(tonumber(n), "number expected"); -- enforced by string.format: assert(tonumber(n), "number expected");
return ("%o"):format(n) return format("%o", n)
end end
--- Encode a string or integer in hexadecimal (12 becomes "c", "AB" becomes --- Encode a string or integer in hexadecimal (12 becomes "c", "AB" becomes
@@ -340,15 +340,15 @@ end
-- @return A string of bytes or nil if string could not be decoded -- @return A string of bytes or nil if string could not be decoded
-- @return Error message if string could not be decoded -- @return Error message if string could not be decoded
function fromhex (hex) function fromhex (hex)
local p = hex:find("[^%x%s]") local p = find(hex, "[^%x%s]")
if p then if p then
return nil, "Invalid hexadecimal digits at position " .. p return nil, "Invalid hexadecimal digits at position " .. p
end end
hex = hex:gsub("%s+", "") hex = gsub(hex, "%s+", "")
if #hex % 2 ~= 0 then if #hex % 2 ~= 0 then
return nil, "Odd number of hexadecimal digits" return nil, "Odd number of hexadecimal digits"
end end
return hex:gsub("..", fromhex_helper) return gsub(hex, "..", fromhex_helper)
end end
local colonsep = {separator=":"} local colonsep = {separator=":"}
@@ -606,7 +606,7 @@ local function arg_value(argname)
end end
-- if scriptname.arg is not there, check "arg" -- if scriptname.arg is not there, check "arg"
local shortname = argname:match("%.([^.]*)$") local shortname = match(argname, "%.([^.]*)$")
if shortname then if shortname then
-- as a key/value pair -- as a key/value pair
if nmap.registry.args[shortname] then if nmap.registry.args[shortname] then
@@ -902,7 +902,7 @@ do end -- no function here, see nse_main.lua
function module (name, ...) function module (name, ...)
local env = {}; local env = {};
env._NAME = name; env._NAME = name;
env._PACKAGE = name:match("(.+)%.[^.]+$"); env._PACKAGE = match(name, "(.+)%.[^.]+$");
env._M = env; env._M = env;
local mods = pack(...); local mods = pack(...);
for i = 1, mods.n do for i = 1, mods.n do