ucx
UAP Common Extensions
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions | Variables
string.h File Reference

Strings that know their length. More...

#include "common.h"
#include "allocator.h"

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_strcast(str)
 Casts a mutable string to an immutable string.
 
#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_a(allocator, string)    cx_strdup_a_((allocator), cx_strcast(string))
 Creates a duplicate of the specified string.
 
#define cx_strdup(string)   cx_strdup_a(cxDefaultAllocator, string)
 Creates a duplicate of the specified string.
 
#define cx_strreplacen(str, search, replacement, replmax)   cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, replmax)
 Replaces a string with another string.
 
#define cx_strreplace_a(allocator, str, search, replacement)   cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX)
 Replaces a string with another string.
 
#define cx_strreplace(str, search, replacement)   cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, SIZE_MAX)
 Replaces a string with another string.
 
#define cx_strtok(str, delim, limit)    cx_strtok_(cx_strcast(str), cx_strcast(delim), (limit))
 Creates a string tokenization context.
 
#define cx_strtos_lc(str, output, base, groupsep)   cx_strtos_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoi_lc(str, output, base, groupsep)   cx_strtoi_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtol_lc(str, output, base, groupsep)   cx_strtol_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoll_lc(str, output, base, groupsep)   cx_strtoll_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoi8_lc(str, output, base, groupsep)   cx_strtoi8_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoi16_lc(str, output, base, groupsep)   cx_strtoi16_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoi32_lc(str, output, base, groupsep)   cx_strtoi32_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoi64_lc(str, output, base, groupsep)   cx_strtoi64_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtous_lc(str, output, base, groupsep)   cx_strtous_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtou_lc(str, output, base, groupsep)   cx_strtou_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoul_lc(str, output, base, groupsep)   cx_strtoul_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoull_lc(str, output, base, groupsep)   cx_strtoull_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtou8_lc(str, output, base, groupsep)   cx_strtou8_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtou16_lc(str, output, base, groupsep)   cx_strtou16_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtou32_lc(str, output, base, groupsep)   cx_strtou32_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtou64_lc(str, output, base, groupsep)   cx_strtou64_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtoz_lc(str, output, base, groupsep)   cx_strtoz_lc_(cx_strcast(str), output, base, groupsep)
 Converts a string to a number.
 
#define cx_strtos(str, output, base)   cx_strtos_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoi(str, output, base)   cx_strtoi_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtol(str, output, base)   cx_strtol_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoll(str, output, base)   cx_strtoll_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoi8(str, output, base)   cx_strtoi8_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoi16(str, output, base)   cx_strtoi16_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoi32(str, output, base)   cx_strtoi32_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoi64(str, output, base)   cx_strtoi64_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoz(str, output, base)   cx_strtoz_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtous(str, output, base)   cx_strtous_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtou(str, output, base)   cx_strtou_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoul(str, output, base)   cx_strtoul_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtoull(str, output, base)   cx_strtoull_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtou8(str, output, base)   cx_strtou8_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtou16(str, output, base)   cx_strtou16_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtou32(str, output, base)   cx_strtou32_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtou64(str, output, base)   cx_strtou64_lc_(cx_strcast(str), output, base, ",")
 Converts a string to a number.
 
#define cx_strtof_lc(str, output, decsep, groupsep)   cx_strtof_lc_(cx_strcast(str), output, decsep, groupsep)
 Converts a string to a single precision floating point number.
 
#define cx_strtod_lc(str, output, decsep, groupsep)   cx_strtod_lc_(cx_strcast(str), output, decsep, groupsep)
 Converts a string to a double precision floating point number.
 
#define cx_strtof(str, output)   cx_strtof_lc_(cx_strcast(str), output, '.', ",")
 Converts a string to a single precision floating point number.
 
#define cx_strtod(str, output)   cx_strtod_lc_(cx_strcast(str), output, '.', ",")
 Converts a string to a double precision floating point number.
 

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 (const char *cstring)
 Wraps a string that must be zero-terminated.
 
cxstring cx_strn (const char *cstring, size_t length)
 Wraps a string that does not need to be zero-terminated.
 
static cxstring cx_strcast_m (cxmutstr str)
 Internal function, do not use.
 
static cxstring cx_strcast_c (cxstring str)
 Internal function, do not use.
 
void cx_strfree (cxmutstr *str)
 Passes the pointer in this string to free().
 
void cx_strfree_a (const CxAllocator *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 (const CxAllocator *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 (const CxAllocator *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 (const CxAllocator *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 (const void *s1, const void *s2)
 Compares two strings.
 
int cx_strcasecmp_p (const void *s1, const void *s2)
 Compares two strings ignoring case.
 
cxmutstr cx_strdup_a_ (const CxAllocator *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.
 
cxmutstr cx_strreplacen_a (const CxAllocator *allocator, cxstring str, cxstring search, cxstring replacement, size_t replmax)
 Replaces a string with another string.
 
CxStrtokCtx cx_strtok_ (cxstring str, cxstring delim, size_t limit)
 Creates a string tokenization context.
 
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, const cxstring *delim, size_t count)
 Defines an array of more delimiters for the specified tokenization context.
 
int cx_strtos_lc_ (cxstring str, short *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoi_lc_ (cxstring str, int *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtol_lc_ (cxstring str, long *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoll_lc_ (cxstring str, long long *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoi8_lc_ (cxstring str, int8_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoi16_lc_ (cxstring str, int16_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoi32_lc_ (cxstring str, int32_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoi64_lc_ (cxstring str, int64_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtous_lc_ (cxstring str, unsigned short *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtou_lc_ (cxstring str, unsigned int *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoul_lc_ (cxstring str, unsigned long *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoull_lc_ (cxstring str, unsigned long long *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtou8_lc_ (cxstring str, uint8_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtou16_lc_ (cxstring str, uint16_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtou32_lc_ (cxstring str, uint32_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtou64_lc_ (cxstring str, uint64_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtoz_lc_ (cxstring str, size_t *output, int base, const char *groupsep)
 Converts a string to a number.
 
int cx_strtof_lc_ (cxstring str, float *output, char decsep, const char *groupsep)
 Converts a string to a single precision floating point number.
 
int cx_strtod_lc_ (cxstring str, double *output, char decsep, const char *groupsep)
 Converts a string to a double precision floating point number.
 

Variables

const unsigned cx_strstr_sbo_size
 The maximum length of the "needle" in cx_strstr() that can use SBO.
 

Detailed Description

Strings that know their length.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ CX_STR

#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.

Parameters
literalthe string literal

◆ cx_strcast

#define cx_strcast ( str)
Value:
_Generic((str), \
(str)
static cxstring cx_strcast_m(cxmutstr str)
Internal function, do not use.
Definition string.h:269
static cxstring cx_strcast_c(cxstring str)
Internal function, do not use.
Definition string.h:279
The UCX string structure.
Definition string.h:51
The UCX string structure for immutable (constant) strings.
Definition string.h:69

Casts a mutable string to an immutable string.

Does nothing for already immutable strings.

Note
This is not seriously a cast. Instead, you get a copy of the struct with the desired pointer type. Both structs still point to the same location, though!
Parameters
str(cxstring or cxmutstr) the string to cast
Returns
(cxstring) an immutable copy of the string pointer

◆ cx_strcat

#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.

If memory allocation fails, the pointer in the returned string will be NULL and errno might be set.

Note
It is guaranteed that there is only one allocation for the resulting string. It is also guaranteed that the returned string is zero-terminated.
Parameters
count(size_t) the number of the other following strings to concatenate
...all other UCX strings
Returns
(cxmutstr) the concatenated string

◆ cx_strcat_a

#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.

If memory allocation fails, the pointer in the returned string will be NULL. Depending on the allocator, errno might be set.

Note
It is guaranteed that there is only one allocation for the resulting string. It is also guaranteed that the returned string is zero-terminated.
Parameters
alloc(CxAllocator*) the allocator to use
count(size_t) the number of the other following strings to concatenate
...all other UCX strings
Returns
(cxmutstr) the concatenated string

◆ cx_strcat_m

#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.

If memory allocation fails, the pointer in the returned string will be NULL and errno might be set.

Note
It is guaranteed that there is only one allocation for the resulting string. It is also guaranteed that the returned string is zero-terminated.
Parameters
str(cxmutstr) the string the other strings shall be concatenated to
count(size_t) the number of the other following strings to concatenate
...all other strings
Returns
(cxmutstr) the concatenated string

◆ cx_strdup

#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().

Note
The returned string is guaranteed to be zero-terminated.
Parameters
stringthe string to duplicate
Returns
(cxmutstr) a duplicate of the string
See also
cx_strdup_a()
cx_strfree()

◆ cx_strdup_a

#define cx_strdup_a ( 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.

Note
The returned string is guaranteed to be zero-terminated.
Parameters
allocator(CxAllocator*) the allocator to use
stringthe string to duplicate
Returns
(cxmutstr) a duplicate of the string
See also
cx_strdup()
cx_strfree_a()

◆ cx_strreplace

#define cx_strreplace ( str,
search,
replacement )   cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, SIZE_MAX)

Replaces a string with another string.

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.

Parameters
str(cxstring) the string where replacements should be applied
search(cxstring) the string to search for
replacement(cxstring) the replacement string
Returns
(cxmutstr) the resulting string after applying the replacements

◆ cx_strreplace_a

#define cx_strreplace_a ( allocator,
str,
search,
replacement )   cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX)

Replaces a string with another string.

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.

Parameters
allocator(CxAllocator*) the allocator to use
str(cxstring) the string where replacements should be applied
search(cxstring) the string to search for
replacement(cxstring) the replacement string
Returns
(cxmutstr) the resulting string after applying the replacements

◆ cx_strreplacen

#define cx_strreplacen ( str,
search,
replacement,
replmax )   cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, replmax)

Replaces a string with another string.

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.

Parameters
str(cxstring) the string where replacements should be applied
search(cxstring) the string to search for
replacement(cxstring) the replacement string
replmax(size_t) maximum number of replacements
Returns
(cxmutstr) the resulting string after applying the replacements

◆ cx_strtod

#define cx_strtod ( str,
output )   cx_strtod_lc_(cx_strcast(str), output, '.', ",")

Converts a string to a double precision floating point number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character.

The decimal separator is assumed to be a dot character. The comma character is treated as group separator and ignored during parsing. If you want to choose a different format, use cx_strtof_lc().

Parameters
strthe string to convert
outputa pointer to the double variable where the result shall be stored
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtod_lc

#define cx_strtod_lc ( str,
output,
decsep,
groupsep )   cx_strtod_lc_(cx_strcast(str), output, decsep, groupsep)

Converts a string to a double precision floating point number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character.

Parameters
strthe string to convert
outputa pointer to the double variable where the result shall be stored
decsepthe decimal separator
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtof

#define cx_strtof ( str,
output )   cx_strtof_lc_(cx_strcast(str), output, '.', ",")

Converts a string to a single precision floating point number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character. It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.

The decimal separator is assumed to be a dot character. The comma character is treated as group separator and ignored during parsing. If you want to choose a different format, use cx_strtof_lc().

Parameters
strthe string to convert
outputa pointer to the float variable where the result shall be stored
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtof_lc

#define cx_strtof_lc ( str,
output,
decsep,
groupsep )   cx_strtof_lc_(cx_strcast(str), output, decsep, groupsep)

Converts a string to a single precision floating point number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character. It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.

Parameters
strthe string to convert
outputa pointer to the float variable where the result shall be stored
decsepthe decimal separator
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi

#define cx_strtoi ( str,
output,
base )   cx_strtoi_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi16

#define cx_strtoi16 ( str,
output,
base )   cx_strtoi16_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi16_lc

#define cx_strtoi16_lc ( str,
output,
base,
groupsep )   cx_strtoi16_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi32

#define cx_strtoi32 ( str,
output,
base )   cx_strtoi32_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi32_lc

#define cx_strtoi32_lc ( str,
output,
base,
groupsep )   cx_strtoi32_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi64

#define cx_strtoi64 ( str,
output,
base )   cx_strtoi64_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi64_lc

#define cx_strtoi64_lc ( str,
output,
base,
groupsep )   cx_strtoi64_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi8

#define cx_strtoi8 ( str,
output,
base )   cx_strtoi8_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi8_lc

#define cx_strtoi8_lc ( str,
output,
base,
groupsep )   cx_strtoi8_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi_lc

#define cx_strtoi_lc ( str,
output,
base,
groupsep )   cx_strtoi_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtok

#define cx_strtok ( str,
delim,
limit )    cx_strtok_(cx_strcast(str), cx_strcast(delim), (limit))

Creates a string tokenization context.

Parameters
strthe string to tokenize
delimthe delimiter string (must not be empty)
limit(size_t) the maximum number of tokens that shall be returned
Returns
(CxStrtokCtx) a new string tokenization context

◆ cx_strtol

#define cx_strtol ( str,
output,
base )   cx_strtol_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtol_lc

#define cx_strtol_lc ( str,
output,
base,
groupsep )   cx_strtol_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoll

#define cx_strtoll ( str,
output,
base )   cx_strtoll_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoll_lc

#define cx_strtoll_lc ( str,
output,
base,
groupsep )   cx_strtoll_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtos

#define cx_strtos ( str,
output,
base )   cx_strtos_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtos_lc

#define cx_strtos_lc ( str,
output,
base,
groupsep )   cx_strtos_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou

#define cx_strtou ( str,
output,
base )   cx_strtou_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou16

#define cx_strtou16 ( str,
output,
base )   cx_strtou16_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou16_lc

#define cx_strtou16_lc ( str,
output,
base,
groupsep )   cx_strtou16_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou32

#define cx_strtou32 ( str,
output,
base )   cx_strtou32_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou32_lc

#define cx_strtou32_lc ( str,
output,
base,
groupsep )   cx_strtou32_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou64

#define cx_strtou64 ( str,
output,
base )   cx_strtou64_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou64_lc

#define cx_strtou64_lc ( str,
output,
base,
groupsep )   cx_strtou64_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou8

#define cx_strtou8 ( str,
output,
base )   cx_strtou8_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou8_lc

#define cx_strtou8_lc ( str,
output,
base,
groupsep )   cx_strtou8_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou_lc

#define cx_strtou_lc ( str,
output,
base,
groupsep )   cx_strtou_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoul

#define cx_strtoul ( str,
output,
base )   cx_strtoul_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoul_lc

#define cx_strtoul_lc ( str,
output,
base,
groupsep )   cx_strtoul_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoull

#define cx_strtoull ( str,
output,
base )   cx_strtoull_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoull_lc

#define cx_strtoull_lc ( str,
output,
base,
groupsep )   cx_strtoull_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtous

#define cx_strtous ( str,
output,
base )   cx_strtous_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtous_lc

#define cx_strtous_lc ( str,
output,
base,
groupsep )   cx_strtous_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoz

#define cx_strtoz ( str,
output,
base )   cx_strtoz_lc_(cx_strcast(str), output, base, ",")

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

The comma character is treated as group separator and ignored during parsing. If you want to choose the set of group separators, use the _lc variant of this function (e.g. cx_strtoz_lc()).

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoz_lc

#define cx_strtoz_lc ( str,
output,
base,
groupsep )   cx_strtoz_lc_(cx_strcast(str), output, base, groupsep)

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsep(const char*) each character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

Function Documentation

◆ cx_mutstr()

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().

Note
the wrapped string will share the specified pointer to the string. If you do want a copy, use cx_strdup() on the return value of this function.

If you need to wrap a constant string, use cx_str().

Parameters
cstringthe string to wrap, must be zero-terminated
Returns
the wrapped string
See also
cx_mutstrn()

◆ cx_mutstrn()

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.

Note
the wrapped string will share the specified pointer to the string. If you do want a copy, use cx_strdup() on the return value of this function.

If you need to wrap a constant string, use cx_strn().

Parameters
cstringthe string to wrap (or NULL, only if the length is zero)
lengththe length of the string
Returns
the wrapped string
See also
cx_mutstr()

◆ cx_str()

cxstring cx_str ( const char * cstring)

Wraps a string that must be zero-terminated.

The length is implicitly inferred by using a call to strlen().

Note
the wrapped string will share the specified pointer to the string. If you do want a copy, use cx_strdup() on the return value of this function.

If you need to wrap a non-constant string, use cx_mutstr().

Parameters
cstringthe string to wrap, must be zero-terminated
Returns
the wrapped string
See also
cx_strn()

◆ cx_strcasecmp()

int cx_strcasecmp ( cxstring s1,
cxstring s2 )

Compares two strings ignoring case.

Parameters
s1the first string
s2the second string
Returns
negative if s1 is smaller than s2, positive if s1 is larger than s2, zero if both strings equal ignoring case

◆ cx_strcasecmp_p()

int cx_strcasecmp_p ( const void * s1,
const void * s2 )

Compares two strings ignoring case.

This function has a compatible signature for the use as a cx_compare_func.

Parameters
s1the first string
s2the second string
Returns
negative if s1 is smaller than s2, positive if s1 is larger than s2, zero if both strings equal ignoring case

◆ cx_strcaseprefix()

bool cx_strcaseprefix ( cxstring string,
cxstring prefix )

Checks, if a string has a specific prefix, ignoring the case.

Parameters
stringthe string to check
prefixthe prefix the string should have
Returns
true, if and only if the string has the specified prefix, false otherwise

◆ cx_strcasesuffix()

bool cx_strcasesuffix ( cxstring string,
cxstring suffix )

Checks, if a string has a specific suffix, ignoring the case.

Parameters
stringthe string to check
suffixthe suffix the string should have
Returns
true, if and only if the string has the specified suffix, false otherwise

◆ cx_strcast_c()

static cxstring cx_strcast_c ( cxstring str)
inlinestatic

Internal function, do not use.

Parameters
str
Returns
See also
cx_strcast()

◆ cx_strcast_m()

static cxstring cx_strcast_m ( cxmutstr str)
inlinestatic

Internal function, do not use.

Parameters
str
Returns
See also
cx_strcast()

◆ cx_strcat_ma()

cxmutstr cx_strcat_ma ( const CxAllocator * 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.

If memory allocation fails, the pointer in the returned string will be NULL. Depending on the allocator, errno might be set.

Note
It is guaranteed that there is only one allocation for the resulting string. It is also guaranteed that the returned string is zero-terminated.
Parameters
allocthe allocator to use
strthe string the other strings shall be concatenated to
countthe number of the other following strings to concatenate
...all other UCX strings
Returns
the concatenated string

◆ cx_strchr()

cxstring cx_strchr ( cxstring string,
int 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.

Parameters
stringthe string where to locate the character
chrthe character to locate
Returns
a substring starting at the first location of chr
See also
cx_strchr_m()

◆ cx_strchr_m()

cxmutstr cx_strchr_m ( cxmutstr string,
int 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.

Parameters
stringthe string where to locate the character
chrthe character to locate
Returns
a substring starting at the first location of chr
See also
cx_strchr()

◆ cx_strcmp()

int cx_strcmp ( cxstring s1,
cxstring s2 )

Compares two strings.

Parameters
s1the first string
s2the second string
Returns
negative if s1 is smaller than s2, positive if s1 is larger than s2, zero if both strings equal

◆ cx_strcmp_p()

int cx_strcmp_p ( const void * s1,
const void * s2 )

Compares two strings.

This function has a compatible signature for the use as a cx_compare_func.

Parameters
s1the first string
s2the second string
Returns
negative if s1 is smaller than s2, positive if s1 is larger than s2, zero if both strings equal

◆ cx_strdup_a_()

cxmutstr cx_strdup_a_ ( const CxAllocator * allocator,
cxstring string )

Creates a duplicate of the specified string.

The new string will contain a copy allocated by allocator.

Note
The returned string is guaranteed to be zero-terminated.
Parameters
allocatorthe allocator to use
stringthe string to duplicate
Returns
a duplicate of the string
See also
cx_strdup()

◆ cx_strfree()

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 which means that this function protects you against double-free.

Note
There is no implementation for cxstring, because it is unlikely that you ever have a const char* you are really supposed to free. If you encounter such situation, you should double-check your code.
Parameters
strthe string to free

◆ cx_strfree_a()

void cx_strfree_a ( const CxAllocator * 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 which means that this function protects you against double-free.

Note
There is no implementation for cxstring, because it is unlikely that you ever have a const char* you are really supposed to free. If you encounter such situation, you should double-check your code.
Parameters
allocthe allocator
strthe string to free

◆ cx_strlen()

size_t cx_strlen ( size_t count,
... )

Returns the accumulated length of all specified strings.

If this sum overflows, errno is set to EOVERFLOW.

Attention
if the count argument is larger than the number of the specified strings, the behavior is undefined.
Parameters
countthe total number of specified strings
...all strings
Returns
the accumulated length of all strings

◆ cx_strn()

cxstring cx_strn ( const 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.

Note
the wrapped string will share the specified pointer to the string. If you do want a copy, use cx_strdup() on the return value of this function.

If you need to wrap a non-constant string, use cx_mutstrn().

Parameters
cstringthe string to wrap (or NULL, only if the length is zero)
lengththe length of the string
Returns
the wrapped string
See also
cx_str()

◆ cx_strprefix()

bool cx_strprefix ( cxstring string,
cxstring prefix )

Checks, if a string has a specific prefix.

Parameters
stringthe string to check
prefixthe prefix the string should have
Returns
true, if and only if the string has the specified prefix, false otherwise

◆ cx_strrchr()

cxstring cx_strrchr ( cxstring string,
int 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.

Parameters
stringthe string where to locate the character
chrthe character to locate
Returns
a substring starting at the last location of chr
See also
cx_strrchr_m()

◆ cx_strrchr_m()

cxmutstr cx_strrchr_m ( cxmutstr string,
int 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.

Parameters
stringthe string where to locate the character
chrthe character to locate
Returns
a substring starting at the last location of chr
See also
cx_strrchr()

◆ cx_strreplacen_a()

cxmutstr cx_strreplacen_a ( const CxAllocator * allocator,
cxstring str,
cxstring search,
cxstring replacement,
size_t replmax )

Replaces a string with another string.

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.

Parameters
allocatorthe allocator to use
strthe string where replacements should be applied
searchthe string to search for
replacementthe replacement string
replmaxmaximum number of replacements
Returns
the resulting string after applying the replacements

◆ cx_strsplit()

size_t cx_strsplit ( cxstring string,
cxstring delim,
size_t limit,
cxstring * output )

Splits a given string using a delimiter string.

Note
The resulting array contains strings that point to the source string. Use cx_strdup() to get copies.
Parameters
stringthe string to split
delimthe delimiter
limitthe maximum number of split items
outputa preallocated array of at least limit length
Returns
the actual number of split items

◆ cx_strsplit_a()

size_t cx_strsplit_a ( const CxAllocator * 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.

Note
The resulting array contains strings that point to the source string. Use cx_strdup() to get copies.
Attention
If allocation fails, the NULL pointer will be written to output and the number returned will be zero.
Parameters
allocatorthe allocator to use for allocating the resulting array
stringthe string to split
delimthe delimiter
limitthe maximum number of split items
outputa pointer where the address of the allocated array shall be written to
Returns
the actual number of split items

◆ cx_strsplit_m()

size_t cx_strsplit_m ( cxmutstr string,
cxstring delim,
size_t limit,
cxmutstr * output )

Splits a given string using a delimiter string.

Note
The resulting array contains strings that point to the source string. Use cx_strdup() to get copies.
Parameters
stringthe string to split
delimthe delimiter
limitthe maximum number of split items
outputa preallocated array of at least limit length
Returns
the actual number of split items

◆ cx_strsplit_ma()

size_t cx_strsplit_ma ( const CxAllocator * 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.

Note
The resulting array contains strings that point to the source string. Use cx_strdup() to get copies.
Attention
If allocation fails, the NULL pointer will be written to output and the number returned will be zero.
Parameters
allocatorthe allocator to use for allocating the resulting array
stringthe string to split
delimthe delimiter
limitthe maximum number of split items
outputa pointer where the address of the allocated array shall be written to
Returns
the actual number of split items

◆ cx_strstr()

cxstring cx_strstr ( cxstring haystack,
cxstring needle )

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.

Parameters
haystackthe string to be scanned
needlestring containing the sequence of characters to match
Returns
a substring starting at the first occurrence of needle, or an empty string, if the sequence is not contained
See also
cx_strstr_m()

◆ cx_strstr_m()

cxmutstr cx_strstr_m ( cxmutstr haystack,
cxstring needle )

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.

Parameters
haystackthe string to be scanned
needlestring containing the sequence of characters to match
Returns
a substring starting at the first occurrence of needle, or an empty string, if the sequence is not contained
See also
cx_strstr()

◆ cx_strsubs()

cxstring cx_strsubs ( cxstring string,
size_t start )

Returns a substring starting at the specified location.

Attention
the new string references the same memory area as the input string and is usually not zero-terminated. Use cx_strdup() to get a copy.
Parameters
stringinput string
startstart location of the substring
Returns
a substring of string starting at start
See also
cx_strsubsl()
cx_strsubs_m()
cx_strsubsl_m()

◆ cx_strsubs_m()

cxmutstr cx_strsubs_m ( cxmutstr string,
size_t start )

Returns a substring starting at the specified location.

Attention
the new string references the same memory area as the input string and is usually not zero-terminated. Use cx_strdup() to get a copy.
Parameters
stringinput string
startstart location of the substring
Returns
a substring of string starting at start
See also
cx_strsubsl_m()
cx_strsubs()
cx_strsubsl()

◆ cx_strsubsl()

cxstring cx_strsubsl ( cxstring string,
size_t start,
size_t length )

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.

Attention
the new string references the same memory area as the input string and is usually not zero-terminated. Use cx_strdup() to get a copy.
Parameters
stringinput string
startstart location of the substring
lengththe maximum length of the returned string
Returns
a substring of string starting at start
See also
cx_strsubs()
cx_strsubs_m()
cx_strsubsl_m()

◆ cx_strsubsl_m()

cxmutstr cx_strsubsl_m ( cxmutstr string,
size_t start,
size_t length )

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.

Attention
the new string references the same memory area as the input string and is usually not zero-terminated. Use cx_strdup() to get a copy.
Parameters
stringinput string
startstart location of the substring
lengththe maximum length of the returned string
Returns
a substring of string starting at start
See also
cx_strsubs_m()
cx_strsubs()
cx_strsubsl()

◆ cx_strsuffix()

bool cx_strsuffix ( cxstring string,
cxstring suffix )

Checks, if a string has a specific suffix.

Parameters
stringthe string to check
suffixthe suffix the string should have
Returns
true, if and only if the string has the specified suffix, false otherwise

◆ cx_strtod_lc_()

int cx_strtod_lc_ ( cxstring str,
double * output,
char decsep,
const char * groupsep )

Converts a string to a double precision floating point number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character. It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.

Parameters
strthe string to convert
outputa pointer to the float variable where the result shall be stored
decsepthe decimal separator
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtof_lc_()

int cx_strtof_lc_ ( cxstring str,
float * output,
char decsep,
const char * groupsep )

Converts a string to a single precision floating point number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character. It sets errno to ERANGE when the necessary representation would exceed the limits defined in libc's float.h.

Parameters
strthe string to convert
outputa pointer to the float variable where the result shall be stored
decsepthe decimal separator
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi16_lc_()

int cx_strtoi16_lc_ ( cxstring str,
int16_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi32_lc_()

int cx_strtoi32_lc_ ( cxstring str,
int32_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi64_lc_()

int cx_strtoi64_lc_ ( cxstring str,
int64_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi8_lc_()

int cx_strtoi8_lc_ ( cxstring str,
int8_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoi_lc_()

int cx_strtoi_lc_ ( cxstring str,
int * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtok_()

CxStrtokCtx cx_strtok_ ( cxstring str,
cxstring delim,
size_t limit )

Creates a string tokenization context.

Parameters
strthe string to tokenize
delimthe delimiter (must not be empty)
limitthe maximum number of tokens that shall be returned
Returns
a new string tokenization context

◆ cx_strtok_delim()

void cx_strtok_delim ( CxStrtokCtx * ctx,
const cxstring * delim,
size_t count )

Defines an array of more delimiters for the specified tokenization context.

Parameters
ctxthe tokenization context
delimarray of more delimiters
countnumber of elements in the array

◆ cx_strtok_next()

bool cx_strtok_next ( CxStrtokCtx * ctx,
cxstring * token )

Returns the next token.

The token will point to the source string.

Parameters
ctxthe tokenization context
tokena pointer to memory where the next token shall be stored
Returns
true if successful, false if the limit or the end of the string has been reached

◆ cx_strtok_next_m()

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.

Attention
If the context was not initialized over a mutable string, modifying the data of the returned token is undefined behavior.
Parameters
ctxthe tokenization context
tokena pointer to memory where the next token shall be stored
Returns
true if successful, false if the limit or the end of the string has been reached

◆ cx_strtol_lc_()

int cx_strtol_lc_ ( cxstring str,
long * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoll_lc_()

int cx_strtoll_lc_ ( cxstring str,
long long * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtos_lc_()

int cx_strtos_lc_ ( cxstring str,
short * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou16_lc_()

int cx_strtou16_lc_ ( cxstring str,
uint16_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou32_lc_()

int cx_strtou32_lc_ ( cxstring str,
uint32_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou64_lc_()

int cx_strtou64_lc_ ( cxstring str,
uint64_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou8_lc_()

int cx_strtou8_lc_ ( cxstring str,
uint8_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtou_lc_()

int cx_strtou_lc_ ( cxstring str,
unsigned int * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoul_lc_()

int cx_strtoul_lc_ ( cxstring str,
unsigned long * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoull_lc_()

int cx_strtoull_lc_ ( cxstring str,
unsigned long long * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtous_lc_()

int cx_strtous_lc_ ( cxstring str,
unsigned short * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtoz_lc_()

int cx_strtoz_lc_ ( cxstring str,
size_t * output,
int base,
const char * groupsep )

Converts a string to a number.

The function returns non-zero when conversion is not possible. In that case the function sets errno to EINVAL when the reason is an invalid character or an unsupported base. It sets errno to ERANGE when the target datatype is too small.

Parameters
strthe string to convert
outputa pointer to the integer variable where the result shall be stored
base2, 8, 10, or 16
groupsepeach character in this string is treated as group separator and ignored during conversion
Return values
zerosuccess
non-zeroconversion was not possible

◆ cx_strtrim()

cxstring cx_strtrim ( cxstring string)

Omits leading and trailing spaces.

Note
the returned string references the same memory, thus you must not free the returned memory.
Parameters
stringthe string that shall be trimmed
Returns
the trimmed string

◆ cx_strtrim_m()

cxmutstr cx_strtrim_m ( cxmutstr string)

Omits leading and trailing spaces.

Note
the returned string references the same memory, thus you must not free the returned memory.
Parameters
stringthe string that shall be trimmed
Returns
the trimmed string