Safe Strings Library Reference Manual
Safe Strings library, version 2.0.1
2. SStrings Basics
- Basic types
- Error model
- Using the Garbage Collector
- "Hello World" with SStrings
- Installing SString
- Using SString
I. Basic types Top
SStrings declares only one custom type
ss_string
, which is a C struct. Note: This struct will be called in this documentss_string Object
.The
ss_string-Object
has 3 variables: We have 2 reasons why we save the length of the string inlen
.
1st:strlen
works in O(n). We don't need then to callstrlen
to determine the length of the string. (It works in O(1))
2nd: The information saved inbytes
helps by determining how may bytes are (re)allocated. SStrings allocates the needed space of memory, when a string gets smaller then SString won't reallocate the memory to fit the string length plus one. The advantage of such a strategie is that next time thess_string Object
has to expand memory, SStrings won't need to reallocate too much memory, in most cases it won't be even necessary to reallocate new memory.If you want to read the length of the string then read the
len
element of thess_string Object
.
II. Error model Top
Since SStrings is written in C, we are not able to handle with errors with exceptions. SString declares the new variable
ss_errno
that can be used just like the Cerrno
variable. This variable is always set if a SString function terminates with a fault. In that casess_errno
is assigned with one of the following error constants:
SS_NOERR
The function was successful SS_NOMEM
The function could not (re)allocate the requested memory space because of lack of memory. The actual ss_string-Object
and its string are not modifiedSS_NULL
The function does not know what to do because one of its arguments (that shouldn't be NULL
) isNULL
. The actualss_string-Object
and its string are not modifiedSS_EINVAL
One argument has an invalid value, for example passing an invalid ss_string-Object
or a wrong string position. The actualss_string-Object
and its string are not modifiedSS_NUM
This constant is used by SStrings only! It helps determining the number of error codes available.
III. Using the Garbage Collector Top
SStrings implements an experimental gargabe collector and should be used at your own risk. Do not use this function if your program has already executed a SString function. Use it at the top of every program. This garbage collector can be used if you don't want to free the
ss_string-Objects
manually.SStrings Library Code
WARNING!!! This function must always be used before you call any other function of this library. If you call this function after having called other SStrings functions then you have to remeber which variables were not allocated with the garbage collector and free them manually! If you don't do this and your program crashes then don't come crying to us! You have been warned.
IV. "Hello World" with SStrings Top
This is the basic "Hello World" programm using SStrings (without the garbage collector):
"Hello World" Example
This is the basic "Hello World" programm using SStrings (using the garbage collector):
"Hello World" Example
You don't strictly need the GNU GCC to compile your source code with SString support. But I will show GCC examples only!
BASH
V. Installing SStrings Top
You first need to fetch the tarballs
After having decompressed the tarball you must execute in your shell:
BASH
For more information about the installation take a look at the
INSTALL
file in the tarball.
VI. Using SStrings Top
You have to include the
sstrings2.h
in your source code. To link with the GNU GCC you have to use the-lsstrings2
option. Take a look at "Hello World" with SStrings.