1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-17 20:09:02 +00:00

merge soc07 r5144 - added a seperate strbuf-module (code mainly taken from eddies implementation in stdnse.lua

This commit is contained in:
fyodor
2007-08-11 05:45:11 +00:00
parent 5528726e5b
commit ed6a634ebc
5 changed files with 89 additions and 52 deletions

View File

@@ -1,3 +1,4 @@
-- See nmaps COPYING for licence
module(...,package.seeall)

View File

@@ -1,3 +1,4 @@
-- See nmaps COPYING for licence
module(..., package.seeall)
--[[

View File

@@ -1,3 +1,4 @@
-- See nmaps COPYING for licence
module(..., package.seeall)
portnumber = function(port, _proto, _state)

View File

@@ -1,3 +1,4 @@
-- See nmaps COPYING for licence
module(..., package.seeall)
print_debug = function(...)
@@ -13,17 +14,7 @@ end
-- separated by the string delimiter (just like in perl)
-- example: strjoin(", ", {"Anna", "Bob", "Charlie", "Dolores"})
function strjoin(delimiter, list)
local len = getn(list)
if len == 0 then
return ""
end
local string = list[1]
for i = 2, len do
string = string .. delimiter .. list[i]
end
return string
return table.concat(list, delimiter);
end
-- Split text into a list consisting of the strings in text,
@@ -51,47 +42,6 @@ function strsplit(delimiter, text)
return list
end
-- String buffer functions. Concatenation is not efficient in
-- lua as strings are immutable. If a large amount of '..'
-- operations are needed a string buffer should be used instead
--[[
local buf = strbuf.new()
strbuf.add(buf, 'string') ; strbuf.add(buf, 'data')
print(buf) -- default seperator is a new line
print(strbuf.dump(buf)) -- no seperator
print(strbuf.dump(buf, ' ')) -- seperated by spaces
strbuf.clear(buf)
--]]
strbuf_dump = table.concat
function strbuf_new()
local sbuf = {}
sbuf.mt = {}
setmetatable(sbuf, sbuf.mt)
sbuf.mt.__tostring = function(s) return strbuf_dump(s, '\n') end
return sbuf
end
function strbuf_add(sbuf, s)
if not (type(s) == 'string') or
not (type(sbuf) == 'table') then
return nil
end
table.insert(sbuf, s)
return table.getn(sbuf)
end
function strbuf_clear(sbuf)
for i, v in pairs(sbuf) do
sbuf[i] = nil
end
end
-- pseudo namespace for string buffers
strbuf = { new=strbuf_new, add=strbuf_add, dump=strbuf_dump, clear=strbuf_clear }
-- Generic buffer implementation using lexical closures
--

84
nselib/strbuf.lua Normal file
View File

@@ -0,0 +1,84 @@
-- license = "See nmaps COPYING for license"
module("strbuf" ,package.seeall)
-- String buffer functions. Concatenation is not efficient in
-- lua as strings are immutable. If a large amount of '..'
-- operations are needed a string buffer should be used instead
--[[
local buf = strbuf.new()
-- from here buf may be used like a string for concatenation operations
-- (the lefthand-operand has to be a strbuf, the righthand-operand may be
-- a string or a strbuf)
-- alternativly you can assign a value (which will become the first string
-- inside the buffer) with new
local buf2 = strbuf.new('hello')
buf = buf .. 'string'
buf = buf .. 'data'
print(buf) -- default seperator is a new line
print(strbuf.dump(buf)) -- no seperator
print(strbuf.dump(buf, ' ')) -- seperated by spaces
strbuf.clear(buf)
--]]
dump = table.concat
concatbuf =function(sbuf, s)
if sbuf == s then
error("refusing to concat the same buffer (recursion)!")
end
if getmetatable(sbuf) ~= mt then
error("left-hand operand of the concat operation has to be a strbuf!")
end
if type(s)=="string" then
table.insert(sbuf, s)
elseif getmetatable(s) == mt then
for _,v in ipairs(s) do
table.insert(sbuf, v)
end
else
error("right-hand operand of concat has to be either string or strbuf!")
end
return sbuf
end
local eqbuf = function(sbuf1, sbuf2)
if getmetatable(sbuf1) ~= mt then
error("equal function expects a value of type strbuf as left-hand operand")
end
if getmetatable(sbuf1) ~= getmetatable(sbuf2) then
return false
end
if #sbuf1 ~= #sbuf2 then
return false
end
for i=1, #sbuf1 do
if sbuf1[i] ~= sbuf2[i] then
return false
end
end
return true
end
clear = function(sbuf)
for i, v in pairs(sbuf) do
sbuf[i] = nil
end
end
mt = { __concat = concatbuf, __tostring = function(s) return dump(s, '\n') end , __eq=eqbuf}
new = function(val)
local tmp ={}
setmetatable(tmp, mt)
if val ~=nil then
table.insert(tmp, val)
end
return tmp
end