cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
sstring.c File Reference

Mutable string helpers used throughout CWIST for request/response and utility text handling. More...

#include <cwist/core/sstring/sstring.h>
#include <cwist/core/mem/alloc.h>
#include <cwist/sys/err/cwist_err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <ctype.h>
Include dependency graph for sstring.c:

Go to the source code of this file.

Functions

size_t cwist_sstring_get_size (cwist_sstring *str)
 Forward declaration for the method-table size callback.
 
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_copy_sstring (cwist_sstring *origin, const cwist_sstring *from)
 Forward declaration for the method-table copy callback.
 
cwist_error_t cwist_sstring_append_sstring (cwist_sstring *str, const cwist_sstring *from)
 Forward declaration for the method-table append callback.
 
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_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_borrow (cwist_sstring *str, const char *data, size_t len)
 Point the string at external storage without copying.
 
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_adopt_region (cwist_sstring *str, char *base, size_t offset, size_t len)
 Adopt a heap buffer as a region view without copying.
 
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_init (cwist_sstring *str)
 Initialize a mutable string with the default append/copy behavior.
 
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_ltrim (cwist_sstring *str)
 Trim leading ASCII whitespace from the string in place.
 
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_change_size (cwist_sstring *str, size_t new_size, bool blow_data)
 Resize the string buffer, optionally allowing truncation.
 
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_append (cwist_sstring *str, const char *data)
 Append a NUL-terminated C string to the target string.
 
cwist_error_t cwist_sstring_append_escaped (cwist_sstring *str, const char *data)
 Append a string while escaping a small HTML-sensitive character set.
 
cwist_error_t cwist_sstring_seek (cwist_sstring *str, char *substr, int location)
 Copy a suffix of the string into a caller-provided buffer.
 
cwist_error_t cwist_sstring_copy (cwist_sstring *origin, char *destination)
 Copy the entire string into a caller-provided destination buffer.
 
cwist_sstring * cwist_sstring_create (void)
 Allocate and initialize a new heap-owned CWIST string object.
 
void cwist_sstring_destroy (cwist_sstring *str)
 Destroy a heap-owned CWIST string and its backing buffer.
 
int cwist_sstring_compare (cwist_sstring *str, const char *compare_to)
 Compare a CWIST string against a raw C string using strcmp semantics.
 
cwist_sstring * cwist_sstring_substr (cwist_sstring *str, int start, int length)
 Create a heap-owned substring spanning the requested range.
 

Detailed Description

Mutable string helpers used throughout CWIST for request/response and utility text handling.

Definition in file sstring.c.

Function Documentation

◆ cwist_sstring_adopt_len()

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.

Adopt a cwist_alloc'd heap buffer, taking ownership without copying.

Adopt a heap buffer, taking ownership without copying.

Parameters
strTarget string object; any owned buffer it holds is released.
bufcwist_alloc'd buffer with room for a NUL at buf[len]; ownership transfers to the string (freed on destroy/reassign). NULL clears.
lenPayload length in bytes.
Returns
ERR_SSTRING_OKAY on success, or ERR_SSTRING_NULL_STRING for NULL input.

< adopted buffer size is unknown beyond len + NUL

Definition at line 151 of file sstring.c.

References cwist_sstring::base, cwist_sstring::borrows_buffer, cap, cwist_sstring::capacity, CWIST_ERR_INT8, cwist_free(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, make_error(), and cwist_sstring::size.

Referenced by cwist_http_receive_request(), cwist_http_receive_request_nb(), cwist_http_response_send_file(), and cwist_https_receive_request().

◆ cwist_sstring_adopt_region()

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.

Adopt a cwist_alloc'd heap buffer as a region view without copying.

Parameters
strTarget string object; any owned buffer it holds is released.
basecwist_alloc'd allocation base; ownership transfers to the string (freed on destroy/reassign). NULL clears.
offsetPayload start relative to base.
lenPayload length in bytes; base[offset + len] must be the NUL slot.
Returns
ERR_SSTRING_OKAY on success, or ERR_SSTRING_NULL_STRING for NULL input.
Note
Growth reallocs base and preserves the offset, so data keeps viewing the same region.

< adopted region size is unknown beyond len + NUL

Definition at line 183 of file sstring.c.

References cwist_sstring::base, cwist_sstring::borrows_buffer, cap, cwist_sstring::capacity, CWIST_ERR_INT8, cwist_free(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, make_error(), and cwist_sstring::size.

Referenced by cwist_http_receive_request_nb().

◆ cwist_sstring_append()

◆ cwist_sstring_append_escaped()

cwist_error_t cwist_sstring_append_escaped ( cwist_sstring *  str,
const char *  data 
)

Append a string while escaping a small HTML-sensitive character set.

Append an escaped C string.

Parameters
strTarget string object.
dataSource string to append in escaped form.
Returns
ERR_SSTRING_OKAY on success, or an error payload describing the failure.

Definition at line 616 of file sstring.c.

References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, CWIST_ERR_JSON, cwist_sstring_append_escaped(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, cwist_sstring::is_fixed, make_error(), and cwist_sstring::size.

Referenced by cwist_sstring_append_escaped(), and cwist_sstring_append_sstring_escaped().

◆ cwist_sstring_append_len()

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.

Append raw bytes with length.

Parameters
strTarget string object.
dataSource bytes to append.
lenNumber of source bytes to append.
Returns
ERR_SSTRING_OKAY on success, or an error payload describing the failure.

Definition at line 212 of file sstring.c.

References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, CWIST_ERR_JSON, cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, make_error(), and cwist_sstring::size.

Referenced by cwist_grpc_stream_send(), cwist_http2_serve_connection_ex(), cwist_http_response_stream_end(), cwist_http_response_stream_write(), cwist_http_stringify_response(), cwist_redis_command_argv_reply(), cwist_sse_response_comment(), and cwist_sse_response_event().

◆ cwist_sstring_append_sstring()

cwist_error_t cwist_sstring_append_sstring ( cwist_sstring *  str,
const cwist_sstring *  from 
)

Forward declaration for the method-table append callback.

Append another sstring.

Append the contents of one CWIST string onto another.

Parameters
strDestination string.
fromSource CWIST string.
Returns
ERR_SSTRING_OKAY on success, or an error describing invalid input.

Definition at line 700 of file sstring.c.

References cap, CWIST_ERR_INT8, cwist_sstring_append(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, and make_error().

Referenced by cwist_sstring_create(), and cwist_sstring_init().

◆ cwist_sstring_append_sstring_escaped()

cwist_error_t cwist_sstring_append_sstring_escaped ( cwist_sstring *  str,
const cwist_sstring *  from 
)

Forward declaration for the escaped-append method-table callback.

Append another sstring with escaping.

Append one CWIST string to another while escaping HTML-sensitive characters.

Parameters
strDestination string.
fromSource CWIST string.
Returns
ERR_SSTRING_OKAY on success, or an error describing invalid input.

Definition at line 720 of file sstring.c.

References cap, CWIST_ERR_INT8, cwist_sstring_append_escaped(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, and make_error().

Referenced by cwist_sstring_init_escaped().

◆ cwist_sstring_assign()

◆ cwist_sstring_assign_len()

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.

Assign raw bytes with length.

Parameters
strTarget string object.
dataSource bytes to copy.
lenNumber of source bytes to copy.
Returns
ERR_SSTRING_OKAY on success, or an error payload describing the failure.

Definition at line 88 of file sstring.c.

References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, CWIST_ERR_JSON, cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, make_error(), and cwist_sstring::size.

Referenced by cwist_async_respond(), cwist_css_minify(), cwist_graphql_serve(), cwist_grpc_set_response(), cwist_http2_push_resource(), cwist_http_client_request(), cwist_http_response_set_html(), and cwist_http_stringify_response().

◆ cwist_sstring_borrow()

cwist_error_t cwist_sstring_borrow ( cwist_sstring *  str,
const char *  data,
size_t  len 
)

Point the string at external storage without copying.

Borrow an external buffer without copying; detached to heap on first mutation.

Borrow an external buffer without copying.

Parameters
strTarget string object; any owned buffer it holds is released.
dataBorrowed bytes; must outlive the string (literal, arena chunk, ...).
lenLength of the borrowed bytes.
Returns
ERR_SSTRING_OKAY on success, or ERR_SSTRING_NULL_STRING for NULL input.
Note
The first mutating call detaches the contents into owned heap storage, so the borrowed source is never written through.

< borrowed capacity is unknown until detach

Definition at line 122 of file sstring.c.

References cwist_sstring::base, cwist_sstring::borrows_buffer, cap, cwist_sstring::capacity, CWIST_ERR_INT8, cwist_free(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, cwist_sstring::is_fixed, make_error(), and cwist_sstring::size.

Referenced by cwist_http_request_create(), and cwist_http_response_send_file().

◆ cwist_sstring_change_size()

cwist_error_t cwist_sstring_change_size ( cwist_sstring *  str,
size_t  new_size,
bool  blow_data 
)

Resize the string buffer, optionally allowing truncation.

Change the internal buffer size.

Parameters
strString object to resize.
new_sizeRequested new size in bytes.
blow_dataWhen true, allow shrinking below the current content length.
Returns
ERR_SSTRING_OKAY on success, or an error describing the resize failure.

Definition at line 417 of file sstring.c.

References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, CWIST_ERR_JSON, cwist_sstring::data, ERR_SSTRING_CONSTANT, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, ERR_SSTRING_RESIZE_TOO_LARGE, cwist_sstring::is_fixed, make_error(), and cwist_sstring::size.

Referenced by cwist_sstring_assign().

◆ cwist_sstring_compare()

int cwist_sstring_compare ( cwist_sstring *  str,
const char *  compare_to 
)

Compare a CWIST string against a raw C string using strcmp semantics.

Compare with a C string.

Parameters
strLeft-hand CWIST string.
compare_toRight-hand raw C string.
Returns
Negative, zero, or positive depending on lexical ordering.

Definition at line 852 of file sstring.c.

References cap.

◆ cwist_sstring_compare_sstring()

int cwist_sstring_compare_sstring ( cwist_sstring *  left,
const cwist_sstring *  right 
)

Forward declaration for the method-table compare callback.

Compare two sstrings.

Compare two CWIST string objects using strcmp semantics.

Parameters
leftLeft-hand string.
rightRight-hand string.
Returns
Negative, zero, or positive depending on lexical ordering.

Definition at line 320 of file sstring.c.

References cap.

Referenced by cwist_sstring_create(), cwist_sstring_init(), and cwist_sstring_init_escaped().

◆ cwist_sstring_copy()

cwist_error_t cwist_sstring_copy ( cwist_sstring *  origin,
char *  destination 
)

Copy the entire string into a caller-provided destination buffer.

Copy to a C string buffer.

Parameters
originSource string object.
destinationDestination C buffer.
Returns
ERR_SSTRING_OKAY on success, or an error describing invalid input.

Definition at line 766 of file sstring.c.

References cap, CWIST_ERR_INT8, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, and make_error().

◆ cwist_sstring_copy_sstring()

cwist_error_t cwist_sstring_copy_sstring ( cwist_sstring *  origin,
const cwist_sstring *  from 
)

Forward declaration for the method-table copy callback.

Copy from another sstring.

Copy one CWIST string into another.

Parameters
originDestination string object.
fromSource string object, or NULL to clear the destination.
Returns
ERR_SSTRING_OKAY on success, or an error describing invalid input.

Definition at line 786 of file sstring.c.

References cap, CWIST_ERR_INT8, cwist_sstring_assign(), ERR_SSTRING_NULL_STRING, and make_error().

Referenced by cwist_sstring_create(), cwist_sstring_init(), and cwist_sstring_init_escaped().

◆ cwist_sstring_create()

◆ cwist_sstring_destroy()

◆ cwist_sstring_get_size()

size_t cwist_sstring_get_size ( cwist_sstring *  str)

Forward declaration for the method-table size callback.

Get the current size.

Return the cached string size in bytes.

Parameters
strString object to inspect.
Returns
Stored size, or 0 when the string is NULL.

Definition at line 306 of file sstring.c.

References cwist_sstring::size.

Referenced by cwist_sstring_create(), cwist_sstring_init(), and cwist_sstring_init_escaped().

◆ cwist_sstring_init()

cwist_error_t cwist_sstring_init ( cwist_sstring *  str)

Initialize a mutable string with the default append/copy behavior.

Initialize an sstring.

Parameters
strString object to initialize in caller-owned storage.
Returns
ERR_SSTRING_OKAY on success, or ERR_SSTRING_NULL_STRING for NULL input.

Definition at line 250 of file sstring.c.

References cwist_sstring::append, cwist_sstring::base, cwist_sstring::borrows_buffer, cap, cwist_sstring::capacity, cwist_sstring::compare, cwist_sstring::copy, CWIST_ERR_INT8, cwist_sstring_append_sstring(), cwist_sstring_compare_sstring(), cwist_sstring_copy_sstring(), cwist_sstring_get_size(), cwist_sstring::data, __prim_cwist_error_t::err_i8, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, cwist_error_t::error, cwist_sstring::get_size, cwist_sstring::is_fixed, make_error(), cwist_sstring::owns_storage, and cwist_sstring::size.

◆ cwist_sstring_init_escaped()

cwist_error_t cwist_sstring_init_escaped ( cwist_sstring *  str)

Initialize a mutable string whose append helper performs HTML escaping.

Initialize an sstring with escaping enabled.

Parameters
strString object to initialize in caller-owned storage.
Returns
ERR_SSTRING_OKAY on success, or ERR_SSTRING_NULL_STRING for NULL input.

Definition at line 278 of file sstring.c.

References cwist_sstring::append, cwist_sstring::base, cwist_sstring::borrows_buffer, cap, cwist_sstring::capacity, cwist_sstring::compare, cwist_sstring::copy, CWIST_ERR_INT8, cwist_sstring_append_sstring_escaped(), cwist_sstring_compare_sstring(), cwist_sstring_copy_sstring(), cwist_sstring_get_size(), cwist_sstring::data, __prim_cwist_error_t::err_i8, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, cwist_error_t::error, cwist_sstring::get_size, cwist_sstring::is_fixed, make_error(), cwist_sstring::owns_storage, and cwist_sstring::size.

◆ cwist_sstring_ltrim()

cwist_error_t cwist_sstring_ltrim ( cwist_sstring *  str)

Trim leading ASCII whitespace from the string in place.

Left-trim whitespace.

Parameters
strString object to mutate.
Returns
ERR_SSTRING_OKAY on success, or ERR_SSTRING_NULL_STRING for invalid input.

Definition at line 329 of file sstring.c.

References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, make_error(), and cwist_sstring::size.

Referenced by cwist_sstring_trim().

◆ cwist_sstring_rtrim()

cwist_error_t cwist_sstring_rtrim ( cwist_sstring *  str)

Trim trailing ASCII whitespace from the string in place.

Right-trim whitespace.

Parameters
strString object to mutate.
Returns
ERR_SSTRING_OKAY on success, or an error describing invalid input.

Definition at line 365 of file sstring.c.

References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, make_error(), and cwist_sstring::size.

Referenced by cwist_sstring_trim().

◆ cwist_sstring_seek()

cwist_error_t cwist_sstring_seek ( cwist_sstring *  str,
char *  substr,
int  location 
)

Copy a suffix of the string into a caller-provided buffer.

Seek a substring at a location.

Parameters
strSource string object.
substrDestination C buffer.
locationStarting offset inside the source string.
Returns
ERR_SSTRING_OKAY on success, or an error describing invalid bounds/input.

Definition at line 741 of file sstring.c.

References cap, CWIST_ERR_INT8, cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, ERR_SSTRING_OUTOFBOUND, and make_error().

◆ cwist_sstring_substr()

cwist_sstring * cwist_sstring_substr ( cwist_sstring *  str,
int  start,
int  length 
)

Create a heap-owned substring spanning the requested range.

Extract a substring.

Parameters
strSource string object.
startZero-based starting index.
lengthRequested substring length.
Returns
Newly allocated substring, or NULL when the range is invalid or OOM occurs.

Definition at line 863 of file sstring.c.

References cwist_sstring::base, cap, cwist_sstring::capacity, cwist_alloc(), cwist_sstring_create(), cwist_sstring_destroy(), cwist_sstring::data, and cwist_sstring::size.

◆ cwist_sstring_trim()

cwist_error_t cwist_sstring_trim ( cwist_sstring *  str)

Trim both leading and trailing ASCII whitespace from the string.

Trim whitespace from both ends.

Parameters
strString object to mutate.
Returns
ERR_SSTRING_OKAY on success, or the first trim error encountered.

Definition at line 404 of file sstring.c.

References cap, cwist_sstring_ltrim(), cwist_sstring_rtrim(), and ERR_SSTRING_OKAY.