Faster implementation of fromhex()

Over 40% faster and creates only a quarter of temporary Lua strings than
the legacy implementation
This commit is contained in:
nnposter 2020-06-04 19:38:10 +00:00
parent 19ad95c926
commit f5b4d98153

View file

@ -334,18 +334,15 @@ end
-- @return A string of bytes or nil if string could not be decoded
-- @return Error message if string could not be decoded
function fromhex (hex)
local len = #hex
local out = {}
local i = 1
while i <= len do
local p, q, c1, c2 = find(hex, "^%s*(%x)%s*(%x)%s*", i)
if not p then
return nil, format("Invalid characters or odd number of hex digits at %d", i)
end
out[#out+1] = char(tonumber(c1..c2, 16))
i = q + 1
local p = hex:find("[^%x%s]")
if p then
return nil, "Invalid hexadecimal digits at position " .. p
end
return concat(out)
hex = hex:gsub("%s+", "")
if #hex % 2 ~= 0 then
return nil, "Odd number of hexadecimal digits"
end
return (hex:gsub("..", function (h) return string.char(tonumber(h, 16)) end))
end
---Format a MAC address as colon-separated hex bytes.