StringBuilder
The StringBuilder is a package, designed to easily concatenate multiple elements into a string (without ConcatStrings
and IntToString
).
All created StringBuilders are transient. All functions starting from SB_InitBuffer
, including it, use the active StringBuilder set with SB_New
or SB_Use
, so there is no var int stringBuilder
parameter in functions. A look at the example explains what I mean.
Warning
The StringBuilder works with pointers, not handles like many other LeGo packages.
Dependencies
N/A
Initialization
N/A
Implementation
Functions
SB_New
Creates and returns a new StringBuilder
. At the same time, this new StringBuilder
is set as active. (See SB_Use
.)
The function returns a pointer to a new StringBuilder
.
SB_Use
Marks this StringBuilder
as active. It can now be used with the functions.
var int sb
Pointer to aStringBuilder
, returned fromSB_New
SB_Get
Returns the active StringBuilder
.
The function returns the active StringBuilder
object - last set with SB_Use
or just created with SB_New
.
SB_InitBuffer
If the size of the resulting string is already known, the buffer can be set manually. This is usually not necessary.
var int size
Size in bytes. Warning! Only works if theStringBuilder
has been newly created!
SB_Clear
Empties the current StringBuilder
. It is not destroyed in the process, so it can be used again. If the object has a buffer allocated, the buffer is freed.
SB_Release
Releases the current stream of the StringBuilder
. The StringBuilder
is destroyed, and the stream can be obtained via SB_GetStream
.
SB_Destroy
Completely destroys the StringBuilder
.
SB_ToString
Returns a copy of the stream as a string.
The function returns the copy of the active StringBuilder
as a string. If the StringBuilder
object doesn't have a buffer allocated, an empty string is returned.
SB_ToStream
Returns a copy of the stream in raw format.
The function returns a copy of the stream in raw format (char[]
)
SB_GetStream
Doesn't copy the stream, but returns it as it is.
The function returns the stream as it is. SB_Destroy
or SB_Clear
destroy the returned pointer.
SB_Length
Returns the current length of the stream. Similar to STR_Len
from Ikarus .
The function returns the current length of the active StringBuilder
.
SB_SetLength
Sets the length of the stream. When increasing, zero bytes are appended.
Stream operations
SB
Appends a string, to the active StringBuilder
.
var string s
The appended string
SBi
Appends an integer in text form, to the active StringBuilder
.
var int i
The appended integer
SBc
Appends a byte, to the active StringBuilder
. (e.g. 82 for 'R' - An ASCII table can be quickly found)
var int c
The appended byte (ASCII table character)
SBraw
Appends a raw bytes array, to the active StringBuilder
.
var int ptr
Pointer to the appended arrayvar int len
Length of an array
SBflt
Appends a Daedalus float in text form, to the active StringBuilder
.
var float x
The appended Daedalus float value
SBf
Appends an Ikarus float in text form, to the active StringBuilder
.
var float x
The appended Ikarus float value
SBw
Appends a 4-byte raw data (interpreted as an integer x
), to the active StringBuilder
.
var int i
The appended value
Independent Functions
STR_Escape
Makes escape sequences out of non-writable characters. For example, newline character \n
becomes \\n
, tab character \t
becomes \\t
, etc.
var string s0
The string to be added escape sequences
Return value
The function returns a new string with escape sequences added for special characters.
STR_Unescape
Counterpart to STR_Escape
. Escape sequences like \n
, \r
or \t
are converted back.
var string s0
The string to be removed escape sequences
Return value
The function returns a new string with escape sequences replaced by their corresponding characters.
STR_StartsWith
Checks if the input string s
starts with the specified prefix string.
var string str
The string to be checkedvar string start
The searched prefix
Return value
The function returns TRUE
if the string starts with the prefix, FALSE
is returned otherwise.
Additional Functions
BuildStringSymbolsArray
Creates an array of all string symbols found in the parser's string table.
The function returns created array.
GetStringSymbolByAddr
Retrieves the symbol at the specified address from the string table.
The function returns a parser symbol at the given address.
Examples
Basic functionality
This is how function that builds a string looks without StringBuilder
:
StringBulider
: SB_New()
and set as the active StringBuilder in the background. The package only supports one StringBuilder at a time, which will keep the pointer in case we want to use another StringBuilder in between. Multiple StringBuilders
Simple example. We want to fill two StringBuilders at the same time and then return them combined: