8# define __CSTR_DIRUTILS_CSTR_CPP_OPEN extern "C" {
9# define __CSTR_DIRUTILS_CSTR_CPP_CLOSE }
11# define __CSTR_DIRUTILS_CSTR_CPP_OPEN
12# define __CSTR_DIRUTILS_CSTR_CPP_CLOSE
15__CSTR_DIRUTILS_CSTR_CPP_OPEN
21#if __STDC_VERSION__ >= CSTR_DEFAULT_C_STD || defined(__cplusplus)
24# undef CSTR_sys_strdup
27#if defined(CSTR_OS_WIN32)
29# define CSTR_sys_strdup _strdup
31# ifdef _POSIX_C_SOURCE
32# undef _POSIX_C_SOURCE
34# define _POSIX_C_SOURCE 200809L
36# define CSTR_sys_strdup strdup
45#define CSTR_FORCECAP_LIMIT 2
48#ifdef CSTR_RECOMMENDED
49# define CSTR_DYNAMIC_CAPACITY
50# define CSTR_ENABLE_GET_RETURN
51# define CSTR_ENABLE_GET_CONST_RETURN
97#ifdef CSTR_DEFINE_INTERNAL
404#define cstr_join(_str, ...) \
406 const char *__arr[] = { __VA_ARGS__ }; \
407 const size_t __count = sizeof(__arr) / sizeof(__arr[0]); \
408 __cstr_join(_str, __arr, __count); \
414# ifdef CSTR_ENABLE_GET_RETURN
415# define CSTR_GET_RETURN(_cstr, _call, ...) ((_call(&_cstr, __VA_ARGS__)), _cstr)
417# define CSTR_GET_RETURN(_cstr, _call, ...)
420# ifdef CSTR_CONST_RETURN_USE_PTR_METHOD
421# define CSTR_CONST_RETURN_PTR_METHOD ->
423# define CSTR_CONST_RETURN_PTR_METHOD .
426# ifdef CSTR_ENABLE_GET_CONST_RETURN
427static inline void CSTR_GET_CONST_RETURN_warning(
void) __attribute__((deprecated(
"CSTR_GET_CONST_RETURN is no longer maintained; memory leaks can't be removed")));
429# define CSTR_GET_CONST_RETURN(_str, _call, ...) \
431 CSTR_GET_CONST_RETURN_warning(); \
433 cstr_initCopy(&tmpS, _str CSTR_CONST_RETURN_PTR_METHOD data); \
434 _call(&tmpS, __VA_ARGS__); \
438# define CSTR_GET_CONST_RETURN(_str, _call, ...)
442__CSTR_DIRUTILS_CSTR_CPP_CLOSE
445# error "Must use C11 as the minimum standard"
int cstr_insert(CSTR *_str, const char *_data, size_t _pos)
Insert a string at a specific position in a CSTR.
int cstr_reverse(CSTR *_str)
Reverse the content of a CSTR in place.
bool cstr_comp(const CSTR _s1, const CSTR _s2)
Compare two CSTR instances for equality.
int cstr_lower(CSTR *_str)
Convert all characters in a CSTR to lowercase.
int cstr_replaceAll(CSTR *_str, const char *_old, const char *_new)
Replace all occurrences of a substring.
int __cstr_join(CSTR *_str, const char *_arr[], const size_t _count)
Add content using an array (From Private API).
int cstr_fromFloat(CSTR *_str, float _val)
Create a numeric string from a float.
int cstr_fromHex(CSTR *_str, unsigned int _val)
Create a hexadecimal string from an unsigned integer.
int cstr_shrink(CSTR *_str, const size_t _len)
Clear the content of a CSTR and optionally shrink capacity.
int cstr_destroy(CSTR *_str)
Destroy and clean up a CSTR and its allocated objects.
int cstr_substr(CSTR *_str, size_t _start, size_t _len)
Extract a substring in place from a CSTR.
int cstr_fromInt(CSTR *_str, int _val)
Create a numeric string from an integer.
int cstr_set(CSTR *_str, const char *_data)
Set the content of a CSTR to a new C-string in .data.
bool cstr_startsWith(CSTR _str, const char *_prefix)
Check if a CSTR begins with the given prefix.
int cstr_fromDouble(CSTR *_str, double _val)
Create a numeric string from a double.
static void CSTR_GET_CONST_RETURN_warning(void) __attribute__((deprecated("CSTR_GET_CONST_RETURN is no longer maintained
int cstr_erase(CSTR *_str, int _pos, size_t _len)
Erase a portion of the string.
int cstr_add(CSTR *_str, const char *_suffix)
Append a suffix to the end of a CSTR.
int cstr_initCopy(CSTR *_dest, const char *_src)
Initialize a CSTR with a copy of an original C-string.
int cstr_upper(CSTR *_str)
Convert all characters in a CSTR to uppercase.
static int __cstr_setFormat(CSTR *_str, const char *_fmt,...)
Internal helper to format and set the string content.
int __cstr_updateCap(CSTR *_str)
Update the capacity automatically IMPORTANT: This function won't do anything if macro CSTR_DYNAMIC_CA...
bool cstr_empty(const CSTR *_str)
Get if the string is empty Return true if: .len smaller or equal to 0 .data is empty ("") ....
int cstr_fromLong(CSTR *_str, long _val)
Create a numeric string from a long.
size_t cstr_find(const CSTR *_str, const char *_find)
Find the first occurrence of a substring.
CSTR cstr_init(void)
Initialize a new empty CSTR.
const char * cstr_bool(const bool _bool)
Return "true" or "false" based on a boolean value.
size_t cstr_countChar(const CSTR *_str, const char ch)
Count occurrences of a single character within a CSTR.
int cstr_replace(CSTR *_str, const char *_old, const char *_new)
Replace only the first occurrence of a substring.
size_t cstr_count(const CSTR *_str, const char *ch)
Count occurrences of a substring within a CSTR.
size_t cstr_findFrom(const CSTR *_str, const char *_find, size_t pos)
Find a substring starting from a specific index.
int cstr_clear(CSTR *_str)
Clear a CSTR to an empty string without freeing memory.
bool cstr_endsWith(const CSTR _str, const char *_suffix)
Check if a CSTR ends with the given suffix.
Custom dynamic string type.
bool initialized
Tracks whether this CSTR has been initialized.
size_t len
Current string length (not counting the NULL terminator).
size_t cap
Total allocated capacity (including NULL terminator).
char * data
Pointer to the character buffer Always NULL terminated if properly managed through the API.
bool forceCap
If true, capacity is fixed and operations that exceed it return an error instead of reallocating the ...