C Standard Library Extensions 1.2.6
|
Typedefs | |
typedef struct _cx_string_ | cx_string |
The cx_string data type. | |
Functions | |
cx_string * | cx_string_new (void) |
Create a new, empty string container. More... | |
cx_string * | cx_string_copy (const cx_string *self) |
Create a copy a cx_string. More... | |
cx_string * | cx_string_create (const cxchar *value) |
Create a new string from a standard C string. More... | |
void | cx_string_delete (cx_string *self) |
Destroy a string. More... | |
cxsize | cx_string_size (const cx_string *self) |
Computes the length of the string. More... | |
cxbool | cx_string_empty (const cx_string *self) |
Checks whether a string contains any characters. More... | |
void | cx_string_set (cx_string *self, const cxchar *data) |
Assign a value to a string. More... | |
const cxchar * | cx_string_get (const cx_string *self) |
Get the string's value. More... | |
cx_string * | cx_string_upper (cx_string *self) |
Converts the string into uppercase. More... | |
cx_string * | cx_string_lower (cx_string *self) |
Converts the string into lowercase. More... | |
cx_string * | cx_string_trim (cx_string *self) |
Remove leading whitespaces from the string. More... | |
cx_string * | cx_string_rtrim (cx_string *self) |
Remove trailing whitespaces from the string. More... | |
cx_string * | cx_string_strip (cx_string *self) |
Remove leading and trailing whitespaces from the string. More... | |
cx_string * | cx_string_prepend (cx_string *self, const cxchar *data) |
Prepend an array of characters to the string. More... | |
cx_string * | cx_string_append (cx_string *self, const cxchar *data) |
Append an array of characters to the string. More... | |
cx_string * | cx_string_insert (cx_string *self, cxssize position, const cxchar *data) |
Inserts a copy of a string at a given position. More... | |
cx_string * | cx_string_erase (cx_string *self, cxssize position, cxssize length) |
Erase a portion of the string. More... | |
cx_string * | cx_string_truncate (cx_string *self, cxsize length) |
Truncate the string. More... | |
cxbool | cx_string_equal (const cx_string *string1, const cx_string *string2) |
Compare two cx_string for equality. More... | |
cxint | cx_string_compare (const cx_string *string1, const cx_string *string2) |
Compare two strings. More... | |
cxint | cx_string_casecmp (const cx_string *string1, const cx_string *string2) |
Compare two strings ignoring the case of characters. More... | |
cxint | cx_string_ncasecmp (const cx_string *string1, const cx_string *string2, cxsize n) |
Compare the first n characters of two strings ignoring the case of characters. More... | |
cxint | cx_string_sprintf (cx_string *self, const char *format,...) |
Writes to a string under format control. More... | |
cxint | cx_string_vsprintf (cx_string *self, const cxchar *format, va_list args) |
Write to the string from a variable-length argument list under format control. More... | |
void | cx_string_print (const cx_string *string) |
Print the value of a cx_string to the standard output. More... | |
void | cx_string_replace_character (cx_string *self, cxsize start, cxsize end, cxchar old_value, cxchar new_value) |
Replace a given character with a new character in a portion of a string. More... | |
void | cx_string_resize (cx_string *self, cxsize size, cxchar c) |
Resize a string to a given length. More... | |
void | cx_string_extend (cx_string *self, cxsize size, cxchar c) |
Extend a string to a given length. More... | |
cxsize | cx_string_find_first_not_of (const cx_string *self, const cxchar *characters) |
Search a string for the first character that does not match any of the given characters. More... | |
cxsize | cx_string_find_last_not_of (const cx_string *self, const cxchar *characters) |
Search a string for the last character that does not match any of the given characters. More... | |
cx_string * | cx_string_substr (const cx_string *self, cxsize pos, cxsize len) |
Create a new string from a portion of a string. More... | |
A cx_string is similar to a standard C string, except that it grows automatically as text is appended or inserted. The character data the string contains is '\0' terminated in order to guarantee full compatibility with string utility functions processing standard C strings. Together with the character data it also stores the length of the string.
Append an array of characters to the string.
self | The string. |
data | Pointer to character array to be appended. |
NULL
in case of errors.The function adds the contents of the character buffer data to the end of the string. If data is a NULL
pointer the string self is not modified.
References cx_free(), and cx_malloc().
Referenced by cx_log_default_handler().
Compare two strings ignoring the case of characters.
string1 | First cx_string. |
string2 | Second cx_string. |
0
if string1 is found, respectively, to be less than, to match, or to be greater than string2.The function compares string2 with string in the same way as the standard C function strcmp() does, but ignores the case of ASCII characters.
References cx_strcasecmp().
Compare two strings.
string1 | First cx_string. |
string2 | Second cx_string. |
0
if string1 is found, respectively, to be less than, to match, or to be greater than string2.The function compares string2 with string in the same way as the standard C function strcmp() does.
Create a copy a cx_string.
self | The string to copy. |
cx_string * cx_string_create | ( | const cxchar * | value | ) |
Create a new string from a standard C string.
value | The initial text to copy into the string. |
A new string is created and the text value is initially copied into the string.
Referenced by cx_string_substr().
void cx_string_delete | ( | cx_string * | self | ) |
Destroy a string.
self | The string to destroy. |
The function deallocates string's character buffer and finally frees the memory allocated for string itself.
References cx_free().
Referenced by cx_log_default_handler().
cxbool cx_string_empty | ( | const cx_string * | self | ) |
Checks whether a string contains any characters.
self | The string. |
A string is considered to be empty if its size is 0 or if it has not been initialized, i.e. no value has been assigned yet.
Compare two cx_string for equality.
string1 | First cx_string. |
string2 | Second cx_string. |
The function checks whether two strings are equal. Two strings are equal if their values match on a character by character basis.
Erase a portion of the string.
self | The string. |
position | Position of the first character to be erased. |
length | Number of characters to erase. |
NULL
in case of errors.The function removes length characters from the string starting at the character index position. The number of characters to be removed is inclusive the character at index position. The characters following the removed portion are shifted to fill the gap. Character positions start counting from 0.
If the number of characters to erase length is less the 0
all characters starting at position up to the end of the string are erased.
References cx_free(), and cx_malloc().
void cx_string_extend | ( | cx_string * | self, |
cxsize | size, | ||
cxchar | c | ||
) |
Extend a string to a given length.
self | A cx_string. |
size | The number of characters by which the string is enlarged. |
c | The character used to fill the new character space |
The function extends a string by adding to its end size number of of characters. The added characters are initialized with the character c.
Extending a string with zero characters leaves the string untouched.
References cx_calloc(), and cx_free().
cxsize cx_string_find_first_not_of | ( | const cx_string * | self, |
const cxchar * | characters | ||
) |
Search a string for the first character that does not match any of the given characters.
self | A string. |
characters | Another string with a set of characters to be used in the search of the string. |
The function searches the given string for the first character that does not match any of the characters specified in the set of characters characters.
cxsize cx_string_find_last_not_of | ( | const cx_string * | self, |
const cxchar * | characters | ||
) |
Search a string for the last character that does not match any of the given characters.
self | A string. |
characters | Another string with a set of characters to be used in the search of the string. |
The function searches the given string for the last character that does not match any of the characters specified in the set of characters characters.
const cxchar * cx_string_get | ( | const cx_string * | self | ) |
Get the string's value.
self | The string. |
NULL
if the string is uninitialized.A pointer to the strings character data. The character array pointed to by this pointer is an standard C string, i.e. '\0' terminated and can be used together with any string processing function from the standard C library (but see below).
Referenced by cx_log_default_handler().
Inserts a copy of a string at a given position.
self | The string. |
position | Character position at which the data is inserted. |
data | Pointer to character array to be inserted. |
NULL
in case of errors.The function inserts the contents of the character buffer data at the character index position into the string, expanding the string if necessary. Character positions start counting from 0. If data is a NULL
pointer the string self is not modified.
References cx_free(), and cx_malloc().
Converts the string into lowercase.
self | The string. |
NULL
in case of errors.All uppercase letters stored in the string are converted to lowercase letters. The conversion is done using the standard C function tolower().
Compare the first n characters of two strings ignoring the case of characters.
string1 | First string. |
string2 | Second string. |
n | Number of characters to compare. |
The function compares the first n characters of the two strings string1 and string2 as strncmp() does, but ignores the case of ASCII characters.
References cx_strncasecmp().
cx_string * cx_string_new | ( | void | ) |
Create a new, empty string container.
The function allocates memory for a new string object and initializes it to represent the empty string.
Using this constructor is the only way to correctly create and setup a new string.
Referenced by cx_log_default_handler(), and cx_string_substr().
Prepend an array of characters to the string.
self | The string. |
data | Pointer to character array to be prepended. |
NULL
in case of errors.The function adds the contents of the character buffer data to the beginning of the string. If data is a NULL
pointer the string self is not modified.
References cx_free(), and cx_malloc().
Referenced by cx_log_default_handler().
void cx_string_print | ( | const cx_string * | string | ) |
Print the value of a cx_string to the standard output.
string | A cx_string. |
This function is provided for debugging purposes. It just writes the strings contents to the standard output using cx_print().
References cx_print().
void cx_string_replace_character | ( | cx_string * | self, |
cxsize | start, | ||
cxsize | end, | ||
cxchar | old_value, | ||
cxchar | new_value | ||
) |
Replace a given character with a new character in a portion of a string.
self | A cx_string. |
start | The initial position of the range. |
end | The final position of the range. |
old_value | The character to be replaced. |
new_value | The character used as replacement. |
The function replaces the all occurrences of the character old_value with the character new_value in the given range. The range of characters considered is given by the initial position start and the final position end, and does not include the final position, i.e. the range of characters is defined as [start, end).
If start is larger than the size of the string string, the function does nothing.
void cx_string_resize | ( | cx_string * | self, |
cxsize | size, | ||
cxchar | c | ||
) |
Resize a string to a given length.
self | A cx_string. |
size | The new length of the string. |
c | The character used to fill the new character space |
The function resizes a string to a new length size. If the new length is smaller than the current length of the string, the string is shortened to its first size characters. If size is larger than the current length of the string its current contents is extended by adding to its end as many characters as needed to reach a length of size characters. In this latter case the added characters are initialized with the character c.
Specifying zero as the new length of the string, results in the empty string.
References cx_free(), and cx_malloc().
Remove trailing whitespaces from the string.
self | The string. |
NULL
in case of errors.The function removes trailing whitespace characters from the string. Whitespace characters are recognized by the standard C function isspace().
void cx_string_set | ( | cx_string * | self, |
const cxchar * | data | ||
) |
Assign a value to a string.
self | The string. |
data | Character array to be assigned. |
Stores the contents of the character array pointed to by data into the string.
cxsize cx_string_size | ( | const cx_string * | self | ) |
Computes the length of the string.
self | The string. |
Computes the length of the string.
cxint cx_string_sprintf | ( | cx_string * | self, |
const char * | format, | ||
... | |||
) |
Writes to a string under format control.
self | The string to write to. |
format | The format string. |
... | The arguments to insert into format. |
The function writes the formatted character array to the string. The function works similar to sprintf() function, except that the string's buffer expands automatically to contain the formatted result. The previous contents of the string is destroyed.
Referenced by cx_log_default_handler().
Remove leading and trailing whitespaces from the string.
self | The string. |
NULL
in case of errors.The function removes leading and trailing whitespace characters from the string. Whitespace characters are recognized by the standard C function isspace().
Create a new string from a portion of a string.
self | A string. |
pos | Position of the first character of the substring. |
len | The length of the substring. |
The function constructs a new string with its value initialized to a copy of a substring of self. The substring is specified by the position of the first character of the substring pos, and the number of characters of the substring len (or the end of the string, whichever comes first).
If pos is equal to the length of self, an empty string is returned. It is an error if pos is greater than the length of self.
References cx_free(), cx_malloc(), cx_string_create(), and cx_string_new().
Remove leading whitespaces from the string.
self | The string. |
NULL
in case of errors.The function removes leading whitespace characters from the string. Whitespace characters are recognized by the standard C function isspace().
Truncate the string.
self | The string. |
length | The length to which the string is truncated. |
NULL
in case of errors.The function removes all characters from the string starting at the character index length up to the end of the string, effectively truncating the string from its original size to a string of length length.
Calling the truncate method is equivalent to:
Converts the string into uppercase.
self | The string. |
NULL
in case of errors.All lowercase letters stored in the string are converted to uppercase letters. The conversion is done using the standard C function toupper().
cxint cx_string_vsprintf | ( | cx_string * | self, |
const cxchar * | format, | ||
va_list | args | ||
) |
Write to the string from a variable-length argument list under format control.
self | The string. |
format | The format string. |
args | Variable-length arguments to be inserted into format. |
The function writes the formatted character array to the string. The function works similar to vsprintf() function, except that the string's buffer expands automatically to contain the formatted result. The previous contents of the string is destroyed.