Library strbuf
String buffer facilities.
Lua's string operations are very flexible and offer an easy-to-use way to
manipulate strings. Concatenation using the ..
operator is such
an operation. The drawback of the built-in API however is the way it handles
concatenation of many string values. Since strings in Lua are immutable
values, each time you concatenate two strings both get copied into the
result string.
The strbuf
module offers a workaround for this problem, while
maintaining the nice syntax. This is accomplished by overloading the
concatenation operator (..
), the equality operator (==
) and the tostring
operator. A string buffer is created by passing a string to
strbuf.new
. Afterwards you can append to the string buffer,
or compare two string buffers for equality just as you would do with normal
strings.
When looking at the details there are some more restrictions/oddities: The
concatenation operator requires its left-hand value to be a string buffer.
Therefore, if you want to prepend a string to a given string buffer you have
to create a new string buffer out of the string you want to prepend. The
string buffer's tostring
operator concatenates the strings
inside the buffer using newlines by default, since this appears to be the
separator used most often.
Example usage:
local buf = strbuf.new() local buf2 = strbuf.new('hello') buf = buf .. 'string' buf = buf .. 'data' print(buf) -- default separator is a newline print(strbuf.dump(buf)) -- no separator print(strbuf.dump(buf, ' ')) -- separated by spaces strbuf.clear(buf)
Copyright © Same as Nmap--See https://nmap.org/book/man-legal.html
Source: https://svn.nmap.org/nmap/nselib/strbuf.lua
Functions
- clear (sbuf)
Clears a string buffer.
- concatbuf (sbuf, s)
Appends a string to a string buffer.
- dump (sbuf, delimiter)
Dumps the string buffer as a string.
- eqbuf (sbuf1, sbuf2)
Determines if the two string buffers are equal. Two buffers are equal if they are the same or if they have equivalent contents.
- new (...)
Create a new string buffer.
- tostring (sbuf)
Returns the string buffer as a string. The delimiter used is a newline.
Functions
- clear (sbuf)
-
Clears a string buffer.
Parameters
- sbuf
- String buffer.
- concatbuf (sbuf, s)
-
Appends a string to a string buffer.
Parameters
- sbuf
- String buffer.
- s
- String to append.
Return value:
sbuf
. - dump (sbuf, delimiter)
-
Dumps the string buffer as a string.
The second parameter is used as a delimiter between the strings stored inside the string buffer.
Parameters
- sbuf
- String buffer to dump.
- delimiter
- String to separate the buffer's contents.
Return value:
Concatenated string result. - eqbuf (sbuf1, sbuf2)
-
Determines if the two string buffers are equal. Two buffers are equal if they are the same or if they have equivalent contents.
Parameters
- sbuf1
- String buffer one.
- sbuf2
- String buffer two.
Return value:
True if equal, false otherwise. - new (...)
-
Create a new string buffer.
The optional arguments are added to the string buffer. The result of adding non-strings is undefined. The
equals
andtostring
operators for string buffers are overloaded to beeqbuf
andtostring
respectively.Parameters
- ...
- Strings to add to the buffer initially.
Return value:
String buffer. - tostring (sbuf)
-
Returns the string buffer as a string. The delimiter used is a newline.
Parameters
- sbuf
- String buffer.
Return value:
String made from concatenating the buffer.