diff --git a/nselib/ipOps.lua b/nselib/ipOps.lua index 9531f64c7..5bcbc32be 100644 --- a/nselib/ipOps.lua +++ b/nselib/ipOps.lua @@ -1,3 +1,4 @@ +-- See nmaps COPYING for licence module(...,package.seeall) diff --git a/nselib/listop.lua b/nselib/listop.lua index 1f3936782..474c99767 100644 --- a/nselib/listop.lua +++ b/nselib/listop.lua @@ -1,3 +1,4 @@ +-- See nmaps COPYING for licence module(..., package.seeall) --[[ diff --git a/nselib/shortport.lua b/nselib/shortport.lua index 77a06587a..d3d48e1df 100644 --- a/nselib/shortport.lua +++ b/nselib/shortport.lua @@ -1,3 +1,4 @@ +-- See nmaps COPYING for licence module(..., package.seeall) portnumber = function(port, _proto, _state) diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 3a5d13c3d..58b69cc12 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -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 -- diff --git a/nselib/strbuf.lua b/nselib/strbuf.lua new file mode 100644 index 000000000..f7ad014e7 --- /dev/null +++ b/nselib/strbuf.lua @@ -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 + + +