ucx
UAP Common Extensions
|
Strings that know their length. More...
Go to the source code of this file.
Data Structures | |
struct | cx_mutstr_s |
The UCX string structure. More... | |
struct | cx_string_s |
The UCX string structure for immutable (constant) strings. More... | |
struct | cx_strtok_ctx_s |
Context for string tokenizing. More... | |
Macros | |
#define | CX_STR(literal) (cxstring){literal, sizeof(literal) - 1} |
A literal initializer for an UCX string structure. | |
#define | cx_strcat_a(alloc, count, ...) cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
Concatenates strings and returns a new string. | |
#define | cx_strcat(count, ...) cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
Concatenates strings and returns a new string. | |
#define | cx_strcat_m(str, count, ...) cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__) |
Concatenates strings. | |
#define | cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string) |
Creates a duplicate of the specified string. | |
#define | cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string)) |
Creates a duplicate of the specified string. | |
#define | cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string)) |
Creates a duplicate of the specified string. | |
#define | cx_strreplacen(str, pattern, replacement, replmax) cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, replmax) |
Replaces a pattern in a string with another string. | |
#define | cx_strreplace_a(allocator, str, pattern, replacement) cx_strreplacen_a(allocator, str, pattern, replacement, SIZE_MAX) |
Replaces a pattern in a string with another string. | |
#define | cx_strreplace(str, pattern, replacement) cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX) |
Replaces a pattern in a string with another string. | |
Typedefs | |
typedef struct cx_mutstr_s | cxmutstr |
A mutable string. | |
typedef struct cx_string_s | cxstring |
An immutable string. | |
typedef struct cx_strtok_ctx_s | CxStrtokCtx |
A string tokenizing context. | |
Functions | |
cxmutstr | cx_mutstr (char *cstring) |
Wraps a mutable string that must be zero-terminated. | |
cxmutstr | cx_mutstrn (char *cstring, size_t length) |
Wraps a string that does not need to be zero-terminated. | |
cxstring | cx_str (char const *cstring) |
Wraps a string that must be zero-terminated. | |
cxstring | cx_strn (char const *cstring, size_t length) |
Wraps a string that does not need to be zero-terminated. | |
cxstring | cx_strcast (cxmutstr str) |
Casts a mutable string to an immutable string. | |
void | cx_strfree (cxmutstr *str) |
Passes the pointer in this string to free() . | |
void | cx_strfree_a (CxAllocator const *alloc, cxmutstr *str) |
Passes the pointer in this string to the allocators free function. | |
size_t | cx_strlen (size_t count,...) |
Returns the accumulated length of all specified strings. | |
cxmutstr | cx_strcat_ma (CxAllocator const *alloc, cxmutstr str, size_t count,...) |
Concatenates strings. | |
cxstring | cx_strsubs (cxstring string, size_t start) |
Returns a substring starting at the specified location. | |
cxstring | cx_strsubsl (cxstring string, size_t start, size_t length) |
Returns a substring starting at the specified location. | |
cxmutstr | cx_strsubs_m (cxmutstr string, size_t start) |
Returns a substring starting at the specified location. | |
cxmutstr | cx_strsubsl_m (cxmutstr string, size_t start, size_t length) |
Returns a substring starting at the specified location. | |
cxstring | cx_strchr (cxstring string, int chr) |
Returns a substring starting at the location of the first occurrence of the specified character. | |
cxmutstr | cx_strchr_m (cxmutstr string, int chr) |
Returns a substring starting at the location of the first occurrence of the specified character. | |
cxstring | cx_strrchr (cxstring string, int chr) |
Returns a substring starting at the location of the last occurrence of the specified character. | |
cxmutstr | cx_strrchr_m (cxmutstr string, int chr) |
Returns a substring starting at the location of the last occurrence of the specified character. | |
cxstring | cx_strstr (cxstring haystack, cxstring needle) |
Returns a substring starting at the location of the first occurrence of the specified string. | |
cxmutstr | cx_strstr_m (cxmutstr haystack, cxstring needle) |
Returns a substring starting at the location of the first occurrence of the specified string. | |
size_t | cx_strsplit (cxstring string, cxstring delim, size_t limit, cxstring *output) |
Splits a given string using a delimiter string. | |
size_t | cx_strsplit_a (CxAllocator const *allocator, cxstring string, cxstring delim, size_t limit, cxstring **output) |
Splits a given string using a delimiter string. | |
size_t | cx_strsplit_m (cxmutstr string, cxstring delim, size_t limit, cxmutstr *output) |
Splits a given string using a delimiter string. | |
size_t | cx_strsplit_ma (CxAllocator const *allocator, cxmutstr string, cxstring delim, size_t limit, cxmutstr **output) |
Splits a given string using a delimiter string. | |
int | cx_strcmp (cxstring s1, cxstring s2) |
Compares two strings. | |
int | cx_strcasecmp (cxstring s1, cxstring s2) |
Compares two strings ignoring case. | |
int | cx_strcmp_p (void const *s1, void const *s2) |
Compares two strings. | |
int | cx_strcasecmp_p (void const *s1, void const *s2) |
Compares two strings ignoring case. | |
cxmutstr | cx_strdup_a (CxAllocator const *allocator, cxstring string) |
Creates a duplicate of the specified string. | |
cxstring | cx_strtrim (cxstring string) |
Omits leading and trailing spaces. | |
cxmutstr | cx_strtrim_m (cxmutstr string) |
Omits leading and trailing spaces. | |
bool | cx_strprefix (cxstring string, cxstring prefix) |
Checks, if a string has a specific prefix. | |
bool | cx_strsuffix (cxstring string, cxstring suffix) |
Checks, if a string has a specific suffix. | |
bool | cx_strcaseprefix (cxstring string, cxstring prefix) |
Checks, if a string has a specific prefix, ignoring the case. | |
bool | cx_strcasesuffix (cxstring string, cxstring suffix) |
Checks, if a string has a specific suffix, ignoring the case. | |
void | cx_strlower (cxmutstr string) |
Converts the string to lower case. | |
void | cx_strupper (cxmutstr string) |
Converts the string to upper case. | |
cxmutstr | cx_strreplacen_a (CxAllocator const *allocator, cxstring str, cxstring pattern, cxstring replacement, size_t replmax) |
Replaces a pattern in a string with another string. | |
CxStrtokCtx | cx_strtok (cxstring str, cxstring delim, size_t limit) |
Creates a string tokenization context. | |
CxStrtokCtx | cx_strtok_m (cxmutstr str, cxstring delim, size_t limit) |
Creates a string tokenization context for a mutable string. | |
bool | cx_strtok_next (CxStrtokCtx *ctx, cxstring *token) |
Returns the next token. | |
bool | cx_strtok_next_m (CxStrtokCtx *ctx, cxmutstr *token) |
Returns the next token of a mutable string. | |
void | cx_strtok_delim (CxStrtokCtx *ctx, cxstring const *delim, size_t count) |
Defines an array of more delimiters for the specified tokenization context. | |
Strings that know their length.
#define CX_STR | ( | literal | ) | (cxstring){literal, sizeof(literal) - 1} |
A literal initializer for an UCX string structure.
The argument MUST be a string (const char*) literal.
literal | the string literal |
#define cx_strcat | ( | count, | |
... | |||
) | cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
Concatenates strings and returns a new string.
The resulting string will be allocated by standard malloc()
. So developers must pass the return value to cx_strfree() eventually.
count | the number of the other following strings to concatenate |
... | all other strings |
#define cx_strcat_a | ( | alloc, | |
count, | |||
... | |||
) | cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
Concatenates strings and returns a new string.
The resulting string will be allocated by the specified allocator. So developers must pass the return value to cx_strfree_a() eventually.
alloc | the allocator to use |
count | the number of the other following strings to concatenate |
... | all other strings |
#define cx_strcat_m | ( | str, | |
count, | |||
... | |||
) | cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__) |
Concatenates strings.
The resulting string will be allocated by standard malloc()
. So developers must pass the return value to cx_strfree() eventually.
If str
already contains a string, the memory will be reallocated and the other strings are appended. Otherwise, new memory is allocated.
str | the string the other strings shall be concatenated to |
count | the number of the other following strings to concatenate |
... | all other strings |
#define cx_strdup | ( | string | ) | cx_strdup_a(cxDefaultAllocator, string) |
Creates a duplicate of the specified string.
The new string will contain a copy allocated by standard malloc()
. So developers must pass the return value to cx_strfree().
string | the string to duplicate |
#define cx_strdup_m | ( | string | ) | cx_strdup_a(cxDefaultAllocator, cx_strcast(string)) |
Creates a duplicate of the specified string.
The new string will contain a copy allocated by standard malloc()
. So developers must pass the return value to cx_strfree().
string | the string to duplicate |
#define cx_strdup_ma | ( | allocator, | |
string | |||
) | cx_strdup_a(allocator, cx_strcast(string)) |
Creates a duplicate of the specified string.
The new string will contain a copy allocated by allocator
.
allocator | the allocator to use |
string | the string to duplicate |
#define cx_strreplace | ( | str, | |
pattern, | |||
replacement | |||
) | cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, SIZE_MAX) |
Replaces a pattern in a string with another string.
The pattern is taken literally and is no regular expression. Replaces at most replmax
occurrences.
The returned string will be allocated by malloc()
and is guaranteed to be zero-terminated.
If allocation fails, or the input string is empty, the returned string will be empty.
str | the string where replacements should be applied |
pattern | the pattern to search for |
replacement | the replacement string |
#define cx_strreplace_a | ( | allocator, | |
str, | |||
pattern, | |||
replacement | |||
) | cx_strreplacen_a(allocator, str, pattern, replacement, SIZE_MAX) |
Replaces a pattern in a string with another string.
The pattern is taken literally and is no regular expression.
The returned string will be allocated by allocator
and is guaranteed to be zero-terminated.
If allocation fails, or the input string is empty, the returned string will be empty.
allocator | the allocator to use |
str | the string where replacements should be applied |
pattern | the pattern to search for |
replacement | the replacement string |
#define cx_strreplacen | ( | str, | |
pattern, | |||
replacement, | |||
replmax | |||
) | cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, replmax) |
Replaces a pattern in a string with another string.
The pattern is taken literally and is no regular expression. Replaces at most replmax
occurrences.
The returned string will be allocated by malloc()
and is guaranteed to be zero-terminated.
If allocation fails, or the input string is empty, the returned string will be empty.
str | the string where replacements should be applied |
pattern | the pattern to search for |
replacement | the replacement string |
replmax | maximum number of replacements |
cxmutstr cx_mutstr | ( | char * | cstring | ) |
Wraps a mutable string that must be zero-terminated.
The length is implicitly inferred by using a call to strlen()
.
If you need to wrap a constant string, use cx_str().
cstring | the string to wrap, must be zero-terminated |
cxmutstr cx_mutstrn | ( | char * | cstring, |
size_t | length | ||
) |
Wraps a string that does not need to be zero-terminated.
The argument may be NULL
if the length is zero.
If you need to wrap a constant string, use cx_strn().
cstring | the string to wrap (or NULL , only if the length is zero) |
length | the length of the string |
cxstring cx_str | ( | char const * | cstring | ) |
Wraps a string that must be zero-terminated.
The length is implicitly inferred by using a call to strlen()
.
If you need to wrap a non-constant string, use cx_mutstr().
cstring | the string to wrap, must be zero-terminated |
Compares two strings ignoring case.
s1 | the first string |
s2 | the second string |
s1
is smaller than s2
, positive if s1
is larger than s2
, zero if both strings equal ignoring case int cx_strcasecmp_p | ( | void const * | s1, |
void const * | s2 | ||
) |
Compares two strings ignoring case.
This function has a compatible signature for the use as a cx_compare_func.
s1 | the first string |
s2 | the second string |
s1
is smaller than s2
, positive if s1
is larger than s2
, zero if both strings equal ignoring case Checks, if a string has a specific prefix, ignoring the case.
string | the string to check |
prefix | the prefix the string should have |
true
, if and only if the string has the specified prefix, false
otherwise Checks, if a string has a specific suffix, ignoring the case.
string | the string to check |
suffix | the suffix the string should have |
true
, if and only if the string has the specified suffix, false
otherwise Casts a mutable string to an immutable string.
str | the mutable string to cast |
cxmutstr cx_strcat_ma | ( | CxAllocator const * | alloc, |
cxmutstr | str, | ||
size_t | count, | ||
... | |||
) |
Concatenates strings.
The resulting string will be allocated by the specified allocator. So developers must pass the return value to cx_strfree_a() eventually.
If str
already contains a string, the memory will be reallocated and the other strings are appended. Otherwise, new memory is allocated.
alloc | the allocator to use |
str | the string the other strings shall be concatenated to |
count | the number of the other following strings to concatenate |
... | all other strings |
Returns a substring starting at the location of the first occurrence of the specified character.
If the string does not contain the character, an empty string is returned.
string | the string where to locate the character |
chr | the character to locate |
chr
Returns a substring starting at the location of the first occurrence of the specified character.
If the string does not contain the character, an empty string is returned.
string | the string where to locate the character |
chr | the character to locate |
chr
Compares two strings.
s1 | the first string |
s2 | the second string |
s1
is smaller than s2
, positive if s1
is larger than s2
, zero if both strings equal int cx_strcmp_p | ( | void const * | s1, |
void const * | s2 | ||
) |
Compares two strings.
This function has a compatible signature for the use as a cx_compare_func.
s1 | the first string |
s2 | the second string |
s1
is smaller than s2
, positive if s1
is larger than s2
, zero if both strings equal cxmutstr cx_strdup_a | ( | CxAllocator const * | allocator, |
cxstring | string | ||
) |
Creates a duplicate of the specified string.
The new string will contain a copy allocated by allocator
.
allocator | the allocator to use |
string | the string to duplicate |
void cx_strfree | ( | cxmutstr * | str | ) |
Passes the pointer in this string to free()
.
The pointer in the struct is set to NULL
and the length is set to zero.
char
const*
you are really supposed to free. If you encounter such situation, you should double-check your code.str | the string to free |
void cx_strfree_a | ( | CxAllocator const * | alloc, |
cxmutstr * | str | ||
) |
Passes the pointer in this string to the allocators free function.
The pointer in the struct is set to NULL
and the length is set to zero.
char
const*
you are really supposed to free. If you encounter such situation, you should double-check your code.alloc | the allocator |
str | the string to free |
size_t cx_strlen | ( | size_t | count, |
... | |||
) |
Returns the accumulated length of all specified strings.
count | the total number of specified strings |
... | all strings |
void cx_strlower | ( | cxmutstr | string | ) |
Converts the string to lower case.
The change is made in-place. If you want a copy, use cx_strdup(), first.
string | the string to modify |
cxstring cx_strn | ( | char const * | cstring, |
size_t | length | ||
) |
Wraps a string that does not need to be zero-terminated.
The argument may be NULL
if the length is zero.
If you need to wrap a non-constant string, use cx_mutstrn().
cstring | the string to wrap (or NULL , only if the length is zero) |
length | the length of the string |
Checks, if a string has a specific prefix.
string | the string to check |
prefix | the prefix the string should have |
true
, if and only if the string has the specified prefix, false
otherwise Returns a substring starting at the location of the last occurrence of the specified character.
If the string does not contain the character, an empty string is returned.
string | the string where to locate the character |
chr | the character to locate |
chr
Returns a substring starting at the location of the last occurrence of the specified character.
If the string does not contain the character, an empty string is returned.
string | the string where to locate the character |
chr | the character to locate |
chr
cxmutstr cx_strreplacen_a | ( | CxAllocator const * | allocator, |
cxstring | str, | ||
cxstring | pattern, | ||
cxstring | replacement, | ||
size_t | replmax | ||
) |
Replaces a pattern in a string with another string.
The pattern is taken literally and is no regular expression. Replaces at most replmax
occurrences.
The returned string will be allocated by allocator
and is guaranteed to be zero-terminated.
If allocation fails, or the input string is empty, the returned string will be empty.
allocator | the allocator to use |
str | the string where replacements should be applied |
pattern | the pattern to search for |
replacement | the replacement string |
replmax | maximum number of replacements |
Splits a given string using a delimiter string.
string
. Use cx_strdup() to get copies.string | the string to split |
delim | the delimiter |
limit | the maximum number of split items |
output | a pre-allocated array of at least limit length |
size_t cx_strsplit_a | ( | CxAllocator const * | allocator, |
cxstring | string, | ||
cxstring | delim, | ||
size_t | limit, | ||
cxstring ** | output | ||
) |
Splits a given string using a delimiter string.
The array pointed to by output
will be allocated by allocator
.
string
. Use cx_strdup() to get copies.NULL
pointer will be written to output
and the number returned will be zero.allocator | the allocator to use for allocating the resulting array |
string | the string to split |
delim | the delimiter |
limit | the maximum number of split items |
output | a pointer where the address of the allocated array shall be written to |
Splits a given string using a delimiter string.
string
. Use cx_strdup() to get copies.string | the string to split |
delim | the delimiter |
limit | the maximum number of split items |
output | a pre-allocated array of at least limit length |
size_t cx_strsplit_ma | ( | CxAllocator const * | allocator, |
cxmutstr | string, | ||
cxstring | delim, | ||
size_t | limit, | ||
cxmutstr ** | output | ||
) |
Splits a given string using a delimiter string.
The array pointed to by output
will be allocated by allocator
.
string
. Use cx_strdup() to get copies.NULL
pointer will be written to output
and the number returned will be zero.allocator | the allocator to use for allocating the resulting array |
string | the string to split |
delim | the delimiter |
limit | the maximum number of split items |
output | a pointer where the address of the allocated array shall be written to |
Returns a substring starting at the location of the first occurrence of the specified string.
If haystack
does not contain needle
, an empty string is returned.
If needle
is an empty string, the complete haystack
is returned.
haystack | the string to be scanned |
needle | string containing the sequence of characters to match |
needle
, or an empty string, if the sequence is not contained Returns a substring starting at the location of the first occurrence of the specified string.
If haystack
does not contain needle
, an empty string is returned.
If needle
is an empty string, the complete haystack
is returned.
haystack | the string to be scanned |
needle | string containing the sequence of characters to match |
needle
, or an empty string, if the sequence is not contained Returns a substring starting at the specified location.
string | input string |
start | start location of the substring |
string
starting at start
Returns a substring starting at the specified location.
string | input string |
start | start location of the substring |
string
starting at start
Returns a substring starting at the specified location.
The returned string will be limited to length
bytes or the number of bytes available in string
, whichever is smaller.
string | input string |
start | start location of the substring |
length | the maximum length of the returned string |
string
starting at start
Returns a substring starting at the specified location.
The returned string will be limited to length
bytes or the number of bytes available in string
, whichever is smaller.
string | input string |
start | start location of the substring |
length | the maximum length of the returned string |
string
starting at start
Checks, if a string has a specific suffix.
string | the string to check |
suffix | the suffix the string should have |
true
, if and only if the string has the specified suffix, false
otherwise CxStrtokCtx cx_strtok | ( | cxstring | str, |
cxstring | delim, | ||
size_t | limit | ||
) |
Creates a string tokenization context.
str | the string to tokenize |
delim | the delimiter (must not be empty) |
limit | the maximum number of tokens that shall be returned |
void cx_strtok_delim | ( | CxStrtokCtx * | ctx, |
cxstring const * | delim, | ||
size_t | count | ||
) |
Defines an array of more delimiters for the specified tokenization context.
ctx | the tokenization context |
delim | array of more delimiters |
count | number of elements in the array |
CxStrtokCtx cx_strtok_m | ( | cxmutstr | str, |
cxstring | delim, | ||
size_t | limit | ||
) |
Creates a string tokenization context for a mutable string.
str | the string to tokenize |
delim | the delimiter (must not be empty) |
limit | the maximum number of tokens that shall be returned |
bool cx_strtok_next | ( | CxStrtokCtx * | ctx, |
cxstring * | token | ||
) |
Returns the next token.
The token will point to the source string.
ctx | the tokenization context |
token | a pointer to memory where the next token shall be stored |
bool cx_strtok_next_m | ( | CxStrtokCtx * | ctx, |
cxmutstr * | token | ||
) |
Returns the next token of a mutable string.
The token will point to the source string. If the context was not initialized over a mutable string, modifying the data of the returned token is undefined behavior.
ctx | the tokenization context |
token | a pointer to memory where the next token shall be stored |
Omits leading and trailing spaces.
string | the string that shall be trimmed |
Omits leading and trailing spaces.
string | the string that shall be trimmed |
void cx_strupper | ( | cxmutstr | string | ) |
Converts the string to upper case.
The change is made in-place. If you want a copy, use cx_strdup(), first.
string | the string to modify |