45#define CX_NULLSTR cx_mutstr(NULL)
48#define CX_SFMT(s) (int) (s).length, (s).ptr
51#define CX_PRIstr ".*s"
166 str.
length = cstring == NULL ? 0 : strlen(cstring);
215 str.
length = cstring == NULL ? 0 : strlen(cstring);
267 return cx_str(
reinterpret_cast<const char*
>(
str));
277#define cx_strcast(s) cx_strcast_(cx_strcast_m(s))
356#define cx_strcast_m(str) _Generic((str), \
357 cxstring: cx_strcast_cxs, \
358 cxmutstr: cx_strcast_cxms, \
359 const unsigned char*: cx_strcast_ucc, \
360 unsigned char *: cx_strcast_uc, \
361 const char*: cx_strcast_cc, \
362 char *: cx_strcast_c) (str)
383#define cx_strcast_(str) _Generic((str), \
384 cxmutstr: cx_strcast_1, \
385 cxstring: cx_strcast_2)(str)
393#define cx_strcast(str) cx_strcast_(cx_strcast_m(str))
470#define cx_strcpy_a(alloc, dest, src) cx_strcpy_a_(alloc, dest, cx_strcast(src))
485#define cx_strcpy(dest, src) cx_strcpy_a(cxDefaultAllocator, dest, src)
542#define cx_strcat(str, count, ...) \
543 cx_strcat_a(cxDefaultAllocator, str, count, __VA_ARGS__)
608#define cx_strsubs(string, start) cx_strsubs_cpp_(cx_strcast_m(string), start)
609#define cx_strsubsl(string, start, length) cx_strsubsl_cpp_(cx_strcast_m(string), start, length)
624#define cx_strsubs(string, start) _Generic(cx_strcast_m(string), \
625 cxstring: cx_strsubs_, \
626 cxmutstr: cx_strsubs_m_)(cx_strcast_m(string), start)
645#define cx_strsubsl(string, start, length) _Generic(cx_strcast_m(string), \
646 cxstring: cx_strsubsl_, \
647 cxmutstr: cx_strsubsl_m_)(cx_strcast_m(string), start, length)
687#define cx_strat(str, index) cx_strat_(cx_strcast(str), index)
718#define cx_strchr(s, chr) cx_strchr_cpp_(cx_strcast_m(s), chr)
725#define cx_strrchr(s, chr) cx_strrchr_cpp_(cx_strcast_m(s), chr)
757#define cx_strchr(string, chr) _Generic(cx_strcast_m(string), \
758 cxstring: cx_strchr_, \
759 cxmutstr: cx_strchr_m_)(cx_strcast_m(string), chr)
772#define cx_strrchr(string, chr) _Generic(cx_strcast_m(string), \
773 cxstring: cx_strrchr_, \
774 cxmutstr: cx_strrchr_m_)(cx_strcast_m(string), chr)
798#define cx_strstr(h,n) cx_strstr_cpp_(cx_strcast_m(h), cx_strcast(n))
824#define cx_strstr(haystack, needle) _Generic(cx_strcast_m(haystack), \
825 cxstring: cx_strstr_,\
826 cxmutstr: cx_strstr_m_)(cx_strcast_m(haystack), cx_strcast(needle))
915#define cx_strsplit(string, delim, limit, output) \
916 cx_strsplit_cpp_(cx_strcast_m(string), cx_strcast(delim), limit, output)
917#define cx_strsplit_a(allocator, string, delim, limit, output) \
918 cx_strsplit_a_cpp_(allocator, cx_strcast_m(string), cx_strcast(delim), limit, output)
933#define cx_strsplit(string, delim, limit, output) \
934 _Generic(cx_strcast_m(string), \
935 cxstring: cx_strsplit_, \
936 cxmutstr: cx_strsplit_m_)\
937 (cx_strcast_m(string), cx_strcast(delim), limit, output)
958#define cx_strsplit_a(allocator, string, delim, limit, output) \
959 _Generic(cx_strcast_m(string), \
960 cxstring: cx_strsplit_a_, \
961 cxmutstr: cx_strsplit_ma_)\
962 (allocator, cx_strcast_m(string), cx_strcast(delim), limit, output)
984#define cx_strcmp(s1, s2) cx_strcmp_(cx_strcast(s1), cx_strcast(s2))
1005#define cx_strcasecmp(s1, s2) cx_strcasecmp_(cx_strcast(s1), cx_strcast(s2))
1064#define cx_strdup_a(allocator, string) cx_strdup_a_((allocator), cx_strcast(string))
1079#define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
1097#define cx_strtrim(string) cx_strtrim_cpp_(cx_strcast_m(string))
1117#define cx_strtrim(string) _Generic(cx_strcast_m(string), \
1118 cxstring: cx_strtrim_, \
1119 cxmutstr: cx_strtrim_m_)(cx_strcast_m(string))
1141#define cx_strprefix(string, prefix) cx_strprefix_(cx_strcast(string), cx_strcast(prefix))
1162#define cx_strsuffix(string, suffix) cx_strsuffix_(cx_strcast(string), cx_strcast(suffix))
1183#define cx_strcaseprefix(string, prefix) cx_strcaseprefix_(cx_strcast(string), cx_strcast(prefix))
1204#define cx_strcasesuffix(string, suffix) cx_strcasesuffix_(cx_strcast(string), cx_strcast(suffix))
1244#define cx_strreplacen_a(allocator, str, search, replacement, replmax) \
1245 cx_strreplace_(allocator, cx_strcast(str), cx_strcast(search), cx_strcast(replacement), replmax)
1264#define cx_strreplacen(str, search, replacement, replmax) \
1265 cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, replmax)
1282#define cx_strreplace_a(allocator, str, search, replacement) \
1283 cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX)
1299#define cx_strreplace(str, search, replacement) \
1300 cx_strreplacen_a(cxDefaultAllocator, str, search, replacement, SIZE_MAX)
1321#define cx_strtok(str, delim, limit) \
1322 cx_strtok_(cx_strcast(str), cx_strcast(delim), (limit))
1358#define cx_strtok_next(ctx, token) _Generic((token), \
1359 cxstring*: cx_strtok_next_, \
1360 cxmutstr*: cx_strtok_next_)(ctx, (cxstring*)token)
1714#define cx_strtos_lc(str, output, base, groupsep) cx_strtos_lc_(cx_strcast(str), output, base, groupsep)
1730#define cx_strtoi_lc(str, output, base, groupsep) cx_strtoi_lc_(cx_strcast(str), output, base, groupsep)
1746#define cx_strtol_lc(str, output, base, groupsep) cx_strtol_lc_(cx_strcast(str), output, base, groupsep)
1762#define cx_strtoll_lc(str, output, base, groupsep) cx_strtoll_lc_(cx_strcast(str), output, base, groupsep)
1778#define cx_strtoi8_lc(str, output, base, groupsep) cx_strtoi8_lc_(cx_strcast(str), output, base, groupsep)
1794#define cx_strtoi16_lc(str, output, base, groupsep) cx_strtoi16_lc_(cx_strcast(str), output, base, groupsep)
1810#define cx_strtoi32_lc(str, output, base, groupsep) cx_strtoi32_lc_(cx_strcast(str), output, base, groupsep)
1826#define cx_strtoi64_lc(str, output, base, groupsep) cx_strtoi64_lc_(cx_strcast(str), output, base, groupsep)
1842#define cx_strtous_lc(str, output, base, groupsep) cx_strtous_lc_(cx_strcast(str), output, base, groupsep)
1858#define cx_strtou_lc(str, output, base, groupsep) cx_strtou_lc_(cx_strcast(str), output, base, groupsep)
1874#define cx_strtoul_lc(str, output, base, groupsep) cx_strtoul_lc_(cx_strcast(str), output, base, groupsep)
1890#define cx_strtoull_lc(str, output, base, groupsep) cx_strtoull_lc_(cx_strcast(str), output, base, groupsep)
1906#define cx_strtou8_lc(str, output, base, groupsep) cx_strtou8_lc_(cx_strcast(str), output, base, groupsep)
1922#define cx_strtou16_lc(str, output, base, groupsep) cx_strtou16_lc_(cx_strcast(str), output, base, groupsep)
1938#define cx_strtou32_lc(str, output, base, groupsep) cx_strtou32_lc_(cx_strcast(str), output, base, groupsep)
1954#define cx_strtou64_lc(str, output, base, groupsep) cx_strtou64_lc_(cx_strcast(str), output, base, groupsep)
1970#define cx_strtoz_lc(str, output, base, groupsep) cx_strtoz_lc_(cx_strcast(str), output, base, groupsep)
1988#define cx_strtos(str, output, base) cx_strtos_lc_(cx_strcast(str), output, base, ",")
2006#define cx_strtoi(str, output, base) cx_strtoi_lc_(cx_strcast(str), output, base, ",")
2024#define cx_strtol(str, output, base) cx_strtol_lc_(cx_strcast(str), output, base, ",")
2042#define cx_strtoll(str, output, base) cx_strtoll_lc_(cx_strcast(str), output, base, ",")
2060#define cx_strtoi8(str, output, base) cx_strtoi8_lc_(cx_strcast(str), output, base, ",")
2078#define cx_strtoi16(str, output, base) cx_strtoi16_lc_(cx_strcast(str), output, base, ",")
2096#define cx_strtoi32(str, output, base) cx_strtoi32_lc_(cx_strcast(str), output, base, ",")
2114#define cx_strtoi64(str, output, base) cx_strtoi64_lc_(cx_strcast(str), output, base, ",")
2132#define cx_strtoz(str, output, base) cx_strtoz_lc_(cx_strcast(str), output, base, ",")
2150#define cx_strtous(str, output, base) cx_strtous_lc_(cx_strcast(str), output, base, ",")
2168#define cx_strtou(str, output, base) cx_strtou_lc_(cx_strcast(str), output, base, ",")
2186#define cx_strtoul(str, output, base) cx_strtoul_lc_(cx_strcast(str), output, base, ",")
2204#define cx_strtoull(str, output, base) cx_strtoull_lc_(cx_strcast(str), output, base, ",")
2222#define cx_strtou8(str, output, base) cx_strtou8_lc_(cx_strcast(str), output, base, ",")
2240#define cx_strtou16(str, output, base) cx_strtou16_lc_(cx_strcast(str), output, base, ",")
2258#define cx_strtou32(str, output, base) cx_strtou32_lc_(cx_strcast(str), output, base, ",")
2276#define cx_strtou64(str, output, base) cx_strtou64_lc_(cx_strcast(str), output, base, ",")
2292#define cx_strtof_lc(str, output, decsep, groupsep) cx_strtof_lc_(cx_strcast(str), output, decsep, groupsep)
2307#define cx_strtod_lc(str, output, decsep, groupsep) cx_strtod_lc_(cx_strcast(str), output, decsep, groupsep)
2325#define cx_strtof(str, output) cx_strtof_lc_(cx_strcast(str), output, '.', ",")
2342#define cx_strtod(str, output) cx_strtod_lc_(cx_strcast(str), output, '.', ",")
Interface for custom allocators.
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:80
Common definitions and feature checks.
#define CX_INLINE
Declares a function to be inlined.
Definition common.h:311
#define CX_CSTR_ARG(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:211
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:289
#define CX_NONNULL
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_NONNULL_ARG(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
#define CX_ACCESS_R(...)
Specifies that the function will only read through the given pointer.
Definition common.h:230
#define CX_ACCESS_RW(...)
Specifies that the function will read and write through the given pointer.
Definition common.h:238
#define CX_CPPDECL
Declares a compatibility function for C++ builds.
Definition common.h:316
#define CX_ACCESS_W(...)
Specifies that the function will only write through the given pointer.
Definition common.h:246
Strings that know their length.
static cxstring cx_strcast_1(cxmutstr str)
Internal function, do not use.
Definition string.h:369
cxmutstr cx_strreplace_(const CxAllocator *allocator, cxstring str, cxstring search, cxstring replacement, size_t replmax)
Replaces a string with another string.
int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep)
Converts a string to a number.
struct cx_string_s cxstring
An immutable string.
Definition string.h:92
static cxstring cx_strcast_cxs(cxstring str)
Internal function, do not use.
Definition string.h:298
const unsigned cx_strstr_sbo_size
The maximum length of the "needle" in cx_strstr() that can use SBO.
int cx_strtoz_lc_(cxstring str, size_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.
cxstring cx_strchr_(cxstring string, int chr)
Searches for a character in a string.
int cx_strcasecmp_p(const void *s1, const void *s2)
Compares two strings ignoring case.
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.
static cxmutstr cx_mutstrcast(cxstring str)
Casts away constness and converts a cxstring to a cxmutstr.
Definition string.h:402
cxstring cx_strsubsl_(cxstring string, size_t start, size_t length)
Returns a substring.
int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strcmp_(cxstring s1, cxstring s2)
Compares two strings.
int cx_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep)
Converts a string to a number.
static cxmutstr cx_strrchr_m_(cxmutstr string, int chr)
Internal conversion function - do not use.
Definition string.h:742
static cxmutstr cx_strcast_uc(unsigned char *str)
Internal function, do not use.
Definition string.h:310
int cx_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep)
Converts a string to a number.
static cxstring cx_str(const char *cstring)
Wraps a string that must be zero-terminated.
Definition string.h:212
cxstring cx_strtrim_(cxstring string)
Trims a string.
int cx_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep)
Converts a string to a number.
static cxmutstr cx_strcast_cxms(cxmutstr str)
Internal function, do not use.
Definition string.h:287
void cx_strfree(cxmutstr *str)
Passes the pointer in this string to the cxDefaultAllocator's free() function.
struct cx_mutstr_s cxmutstr
A mutable string.
Definition string.h:74
int cx_strtos_lc_(cxstring str, short *output, int base, const char *groupsep)
Converts a string to a number.
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.
cxstring cx_strsubs_(cxstring string, size_t start)
Returns a substring.
static cxmutstr cx_strsubsl_m_(cxmutstr string, size_t start, size_t length)
Internal conversion function - do not use.
Definition string.h:591
struct cx_strtok_ctx_s CxStrtokCtx
A string tokenizing context.
Definition string.h:143
int cx_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep)
Converts a string to a number.
static cxmutstr cx_mutstr(char *cstring)
Wraps a mutable string that must be zero-terminated.
Definition string.h:163
static cxmutstr cx_strchr_m_(cxmutstr string, int chr)
Internal conversion function - do not use.
Definition string.h:733
cxmutstr cx_strdup_a_(const CxAllocator *allocator, cxstring string)
Creates a duplicate of the specified string.
#define cx_strcast(str)
Converts any string to a cxstring.
Definition string.h:393
static cxmutstr cx_mutstrn(char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
Definition string.h:187
int cx_strcmp_p(const void *s1, const void *s2)
Compares two strings.
static cxstring cx_strcast_ucc(const unsigned char *str)
Internal function, do not use.
Definition string.h:334
bool cx_strcaseprefix_(cxstring string, cxstring prefix)
Checks if a string has a specific prefix, ignoring the case.
int cx_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep)
Converts a string to a number.
int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep)
Converts a string to a double precision floating-point number.
int cx_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep)
Converts a string to a number.
size_t cx_strsplit_(cxstring string, cxstring delim, size_t limit, cxstring *output)
Splits a given string using a delimiter string.
static char cx_strat_(cxstring str, off_t index)
Returns the character at the specified index offset.
Definition string.h:661
static cxmutstr cx_strcast_c(char *str)
Internal function, do not use.
Definition string.h:322
int cx_strtoul_lc_(cxstring str, unsigned long *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.
cxmutstr cx_strcat_a(const CxAllocator *alloc, cxmutstr str, size_t count,...)
Concatenates strings.
CxStrtokCtx cx_strtok_(cxstring str, cxstring delim, size_t limit)
Creates a string tokenization context.
static cxstring cx_strn(const char *cstring, size_t length)
Wraps a string that does not need to be zero-terminated.
Definition string.h:237
int cx_strcpy_a_(const CxAllocator *alloc, cxmutstr *dest, cxstring src)
Copies a string.
int cx_strcasecmp_(cxstring s1, cxstring s2)
Compares two strings ignoring case.
size_t cx_strlen(size_t count,...)
Returns the accumulated length of all specified strings.
#define cx_strtok_next(ctx, token)
Returns the next token.
Definition string.h:1358
static cxstring cx_strcast_2(cxstring str)
Internal function, do not use.
Definition string.h:378
int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep)
Converts a string to a number.
size_t cx_strsplit_m_(cxmutstr string, cxstring delim, size_t limit, cxmutstr *output)
Splits a given string using a delimiter string.
static cxmutstr cx_strsubs_m_(cxmutstr string, size_t start)
Internal conversion function - do not use.
Definition string.h:579
int cx_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep)
Converts a string to a number.
cxstring cx_strrchr_(cxstring string, int chr)
Searches for a character in a string.
static cxmutstr cx_strstr_m_(cxmutstr haystack, cxstring needle)
Internal conversion - do not use.
Definition string.h:806
int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep)
Converts a string to a number.
bool cx_strtok_next_(CxStrtokCtx *ctx, cxstring *token)
Returns the next token.
bool cx_strprefix_(cxstring string, cxstring prefix)
Checks if a string has a specific prefix.
static cxstring cx_strcast_cc(const char *str)
Internal function, do not use.
Definition string.h:346
#define cx_strcast_(str)
internal conversion macro
Definition string.h:383
bool cx_strsuffix_(cxstring string, cxstring suffix)
Checks if a string has a specific suffix.
cxstring cx_strstr_(cxstring haystack, cxstring needle)
Searches for a specific substring.
static cxmutstr cx_strtrim_m_(cxmutstr string)
Internal conversion function.
Definition string.h:1104
void cx_strtok_delim(CxStrtokCtx *ctx, const cxstring *delim, size_t count)
Defines an array of more delimiters for the specified tokenization context.
#define cx_strcast_m(str)
Wraps any string into an UCX string.
Definition string.h:356
int cx_strtol_lc_(cxstring str, long *output, int base, const char *groupsep)
Converts a string to a number.
bool cx_strcasesuffix_(cxstring string, cxstring suffix)
Checks, if a string has a specific suffix, ignoring the case.
int cx_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep)
Converts a string to a number.
void cx_strfree_a(const CxAllocator *alloc, cxmutstr *str)
Passes the pointer in this string to the allocator's free function.
The UCX string structure.
Definition string.h:61
char * ptr
A pointer to the string.
Definition string.h:66
size_t length
The length of the string.
Definition string.h:68
The UCX string structure for immutable (constant) strings.
Definition string.h:79
size_t length
The length of the string.
Definition string.h:86
const char * ptr
A pointer to the immutable string.
Definition string.h:84
Context for string tokenizing.
Definition string.h:97
size_t delim_pos
Position of the next delimiter in the source string.
Definition string.h:125
cxstring str
The string to tokenize.
Definition string.h:101
cxstring delim
The primary delimiter.
Definition string.h:105
size_t delim_more_count
Length of the array containing more delimiters.
Definition string.h:113
size_t pos
Position of the currently active token in the source string.
Definition string.h:117
size_t next_pos
The position of the next token in the source string.
Definition string.h:129
size_t found
The number of already found tokens.
Definition string.h:133
size_t limit
The maximum number of tokens that shall be returned.
Definition string.h:137
const cxstring * delim_more
Optional array of more delimiters.
Definition string.h:109