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-Objecthas 3 variables: We have 2 reasons why we save the length of the string inlen.
1st:strlenworks in O(n). We don't need then to callstrlento determine the length of the string. (It works in O(1))
2nd: The information saved inbyteshelps 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 Objecthas 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
lenelement 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_errnothat can be used just like the Cerrnovariable. This variable is always set if a SString function terminates with a fault. In that casess_errnois assigned with one of the following error constants:
SS_NOERRThe function was successful SS_NOMEMThe function could not (re)allocate the requested memory space because of lack of memory. The actual ss_string-Objectand its string are not modifiedSS_NULLThe function does not know what to do because one of its arguments (that shouldn't be NULL) isNULL. The actualss_string-Objectand its string are not modifiedSS_EINVALOne argument has an invalid value, for example passing an invalid ss_string-Objector a wrong string position. The actualss_string-Objectand its string are not modifiedSS_NUMThis 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-Objectsmanually.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
INSTALLfile in the tarball.
VI. Using SStrings
Top
You have to include the
sstrings2.hin your source code. To link with the GNU GCC you have to use the-lsstrings2option. Take a look at "Hello World" with SStrings.

