95 char *
new_data = cwist_sstring_reserve(str, len, 0);
105 if (data && len > 0)
memcpy(str->
data, data, len);
106 if (str->
data) str->
data[len] =
'\0';
133 str->
data = (
char *)(data ? data :
"");
134 str->
size = data ? len : 0;
163 str->
size = buf ? len : 0;
166 if (buf) buf[len] =
'\0';
195 str->
data = base ? base + offset :
NULL;
196 str->
size = base ? len : 0;
199 if (base) base[offset + len] =
'\0';
218 if (!data || len == 0) {
307 return str ? str->
size : 0;
310static inline const char *sstring_as_text(
const cwist_sstring *s) {
311 return (s && s->
data) ? s->
data :
"";
332 if (!str || !str->
data)
return err;
368 if (!str || !str->
data)
return err;
377 size_t end = len - 1;
436 err.error.err_json,
"err",
437 "New size is smaller than current data length and blow_data is false.");
476static bool sstring_aliases(
const cwist_sstring *str,
const char *
p) {
481 return at >= lo &&
at < hi;
515 size_t data_len = data ?
strlen(data) : 0;
518 if (data_len > str->
size) {
520 "string's assigned size is smaller than given data");
531 char *
new_data = cwist_sstring_reserve(str, data_len, 0);
534 "cannot assign string: memory is full");
539 str->
size = data_len;
583 "Cannot append: would exceed fixed size");
638 if (
err.error.err_json) {
640 "Cannot append: would exceed fixed size");
649 if (
err.error.err_json) {
682 default: *ptr++ = data[
i];
break;
804 if (!str)
return NULL;
864 if (!str || !str->
data ||
start < 0 || length < 0)
return NULL;
875 if (!sub)
return NULL;
887 sub->
data[length] =
'\0';
Memory allocation wrappers backed by libttak.
void cwist_free(void *ptr)
Free memory obtained via cwist_alloc/cwist_strdup/etc.
void * cwist_realloc(void *ptr, size_t new_size)
Resize an existing allocation (libttak tracked).
char * cwist_strdup(const char *src)
Duplicate a string using libttak-backed storage.
void * cwist_alloc(size_t size)
Allocate zeroed memory tracked by libttak.
Safe string type with helper methods.
@ ERR_SSTRING_NULL_STRING
@ ERR_SSTRING_RESIZE_TOO_LARGE
@ CWIST_ERR_INT8
mostly used to check a char
cwist_error_t make_error(cwist_errtype_t type)
Creates an error object initialized with the specified type.
int cwist_sstring_compare(cwist_sstring *str, const char *compare_to)
Compare a CWIST string against a raw C string using strcmp semantics.
cwist_error_t cwist_sstring_append_len(cwist_sstring *str, const char *data, size_t len)
Append an arbitrary byte range to the end of the string.
cwist_error_t cwist_sstring_adopt_region(cwist_sstring *str, char *base, size_t offset, size_t len)
Adopt a heap buffer as a region view without copying.
size_t cwist_sstring_get_size(cwist_sstring *str)
Forward declaration for the method-table size callback.
cwist_sstring * cwist_sstring_create(void)
Allocate and initialize a new heap-owned CWIST string object.
cwist_error_t cwist_sstring_init(cwist_sstring *str)
Initialize a mutable string with the default append/copy behavior.
cwist_sstring * cwist_sstring_substr(cwist_sstring *str, int start, int length)
Create a heap-owned substring spanning the requested range.
cwist_error_t cwist_sstring_append(cwist_sstring *str, const char *data)
Append a NUL-terminated C string to the target string.
cwist_error_t cwist_sstring_assign_len(cwist_sstring *str, const char *data, size_t len)
Replace the string contents with an arbitrary byte range.
cwist_error_t cwist_sstring_assign(cwist_sstring *str, const char *data)
Replace the string contents with a NUL-terminated C string.
cwist_error_t cwist_sstring_copy(cwist_sstring *origin, char *destination)
Copy the entire string into a caller-provided destination buffer.
cwist_error_t cwist_sstring_append_sstring(cwist_sstring *str, const cwist_sstring *from)
Forward declaration for the method-table append callback.
void cwist_sstring_destroy(cwist_sstring *str)
Destroy a heap-owned CWIST string and its backing buffer.
cwist_error_t cwist_sstring_append_sstring_escaped(cwist_sstring *str, const cwist_sstring *from)
Forward declaration for the escaped-append method-table callback.
cwist_error_t cwist_sstring_borrow(cwist_sstring *str, const char *data, size_t len)
Point the string at external storage without copying.
cwist_error_t cwist_sstring_change_size(cwist_sstring *str, size_t new_size, bool blow_data)
Resize the string buffer, optionally allowing truncation.
cwist_error_t cwist_sstring_rtrim(cwist_sstring *str)
Trim trailing ASCII whitespace from the string in place.
cwist_error_t cwist_sstring_trim(cwist_sstring *str)
Trim both leading and trailing ASCII whitespace from the string.
cwist_error_t cwist_sstring_ltrim(cwist_sstring *str)
Trim leading ASCII whitespace from the string in place.
cwist_error_t cwist_sstring_copy_sstring(cwist_sstring *origin, const cwist_sstring *from)
Forward declaration for the method-table copy callback.
cwist_error_t cwist_sstring_seek(cwist_sstring *str, char *substr, int location)
Copy a suffix of the string into a caller-provided buffer.
int cwist_sstring_compare_sstring(cwist_sstring *left, const cwist_sstring *right)
Forward declaration for the method-table compare callback.
cwist_error_t cwist_sstring_init_escaped(cwist_sstring *str)
Initialize a mutable string whose append helper performs HTML escaping.
cwist_error_t cwist_sstring_adopt_len(cwist_sstring *str, char *buf, size_t len)
Adopt a heap buffer as the string contents without copying.
cwist_error_t cwist_sstring_append_escaped(cwist_sstring *str, const char *data)
Append a string while escaping a small HTML-sensitive character set.
Integrated error structure used throughout the system.
__prim_cwist_error_t error
int(* compare)(struct cwist_sstring *left, const struct cwist_sstring *right)
Should mimic strcmp, internally use strncmp.
char * data
Access directly only when raw handling is necessary.
cwist_error_t(* append)(struct cwist_sstring *str, const struct cwist_sstring *from)
Append another sstring.
cwist_error_t(* copy)(struct cwist_sstring *str, const struct cwist_sstring *from)
size_t(* get_size)(struct cwist_sstring *str)
bool borrows_buffer
data is borrowed (static/arena); never freed, detached on mutation
Unified error code and type definitions for the CWIST system layer.