[SLP-Homepage]    [Source Modules]    [Manual]    [Run]    [Examples]
 

The String Datatype

The C++ language is lacking a real string datatype. Of course, there are pointers to characters. But these can be used for a number of different purposes.

This module defines a type str_t (as a synonym for const char *) which is always used for null-termined constant character arrays in the SLP-system. Also a constant STR_NIL is defined.

Furthermore, there are a few utility functions for strings.


Utility Functions for Strings:
str_cpy:
void str_cpy(char* buf, int len, str_t str);
This function is a safer version of the strcpy standard library function. It copies the characters of str one by one into the buffer buf, just as strcpy does. However, the size size of the buffer is respected. If it is too small, as many as possible characters are copied, and the buffer contents is ended in "...". Also, the routine doesn't crash if buf or str are NIL. If str is NIL, but buf is not, "<NIL>" is written into the buffer.
str_cat:
void str_cat(char* buf, int len, str_t str);
This function copies the characters of str one by one into the buffer buf, but after the string which is already contained in buf. So it concatenates both strings, just as the standard library function strcat does. However, it is more secure (see the explanation under str_cpy, which is used internally for copying the characters).
str_eq:
bool_t str_eq(str_t str1, str_t str2);
This function compares the strings str1 and str2 character by character. If the strings are equal, BOOL_TRUE is returned, otherwise BOOL_FALSE. This function is related to the standard library function strcmp, but it avoids the possible trap that strcmp always returns the wrong value if interpreted as boolean value.
str_contains:
bool_t str_contains(char c, str_t str);
This function returns BOOL_TRUE if str contains c, and BOOL_FALSE otherwise. If str is NIL, the function doesn't crash, but returns BOOL_FALSE. If str is not NIL, but c is the null character, the function currently returns BOOL_TRUE (this might change in future versions).


Implementation:


Stefan Brass (sbrass@sis.pitt.edu), March 18, 2001.    [HTML 3.2 Checked]