1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-08 13:41:29 +00:00

Let nsedebug.tostr support other datatypes with tostring

Instead of erroring, just use tostring to handle functions, userdata,
etc. This works for booleans and numbers, too, so no need for special
handling there.

Also switched from a string-concatenation model to a table-building one,
with a final concatenation. This could prevent catastrophic slowness
with representing large tables due to continuous reallocation of
strings.
This commit is contained in:
dmiller
2014-01-03 21:09:59 +00:00
parent 395a91b026
commit 54cea8be4f

View File

@@ -12,6 +12,7 @@ local io = require "io"
local math = require "math" local math = require "math"
local stdnse = require "stdnse" local stdnse = require "stdnse"
local string = require "string" local string = require "string"
local table = require "table"
_ENV = stdnse.module("nsedebug", stdnse.seeall) _ENV = stdnse.module("nsedebug", stdnse.seeall)
local EMPTY = {}; -- Empty constant table local EMPTY = {}; -- Empty constant table
@@ -25,41 +26,38 @@ local EMPTY = {}; -- Empty constant table
-- is 0. -- is 0.
--@return A string representation of a data, will be one or more full lines. --@return A string representation of a data, will be one or more full lines.
function tostr(data, indent) function tostr(data, indent)
local str = "" local str
if(indent == nil) then if(indent == nil) then
indent = 0 indent = 0
end end
-- Check the type -- Check the type
if(type(data) == "nil") then local typ = type(data)
str = str .. (" "):rep(indent) .. "nil\n" if(typ == "nil" or typ == "number" or typ == "boolean" or typ == "function" or typ == "thread" or typ == "userdata") then
str = {(" "):rep(indent), tostring(data), "\n"}
elseif(type(data) == "string") then elseif(type(data) == "string") then
str = str .. (" "):rep(indent) .. string.format("%q", data) .. "\n" str = {(" "):rep(indent), string.format("%q", data), "\n"}
elseif(type(data) == "number") then
str = str .. (" "):rep(indent) .. data .. "\n"
elseif(type(data) == "boolean") then
if(data == true) then
str = str .. "true\n"
else
str = str .. "false\n"
end
elseif(type(data) == "table") then elseif(type(data) == "table") then
local i, v local i, v
str = {}
for i, v in pairs(data) do for i, v in pairs(data) do
-- Check for a table in a table -- Check for a table in a table
str[#str+1] = (" "):rep(indent)
str[#str+1] = tostring(i)
if(type(v) == "table") then if(type(v) == "table") then
str = str .. (" "):rep(indent) .. tostring(i) .. ":\n" str[#str+1] = ":\n"
str = str .. tostr(v, indent + 2) str[#str+1] = tostr(v, indent + 2)
else else
str = str .. (" "):rep(indent) .. tostring(i) .. ": " .. tostr(v, 0) str[#str+1] = ": "
str[#str+1] = tostr(v, 0)
end end
end end
else else
stdnse.print_debug(1, "Error: unknown data type: %s", type(data)) stdnse.print_debug(1, "Error: unknown data type: %s", type(data))
end end
return str return table.concat(str)
end end
--- Print out a string in hex, for debugging. --- Print out a string in hex, for debugging.