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

Safe string type with helper methods. More...

#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <cwist/sys/err/cwist_err.h>
Include dependency graph for sstring.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  cwist_sstring
 

Typedefs

typedef struct cwist_sstring cwist_sstring
 

Enumerations

enum  cwist_sstring_error_t {
  ERR_SSTRING_OKAY , ERR_SSTRING_ZERO_LENGTH , ERR_SSTRING_NULL_STRING , ERR_SSTRING_CONSTANT ,
  ERR_SSTRING_RESIZE_TOO_SMALL , ERR_SSTRING_RESIZE_TOO_LARGE , ERR_SSTRING_OUTOFBOUND
}
 

Functions

cwist_sstring * cwist_sstring_create (void)
 Create a new sstring instance.
 
void cwist_sstring_destroy (cwist_sstring *str)
 Destroy an sstring instance.
 
String manipulation API
cwist_error_t cwist_sstring_append_len (cwist_sstring *str, const char *data, size_t len)
 Append raw bytes with length.
 
cwist_error_t cwist_sstring_assign_len (cwist_sstring *str, const char *data, size_t len)
 Assign raw bytes with length.
 
cwist_error_t cwist_sstring_borrow (cwist_sstring *str, const char *data, size_t len)
 Borrow an external buffer without copying.
 
cwist_error_t cwist_sstring_adopt_len (cwist_sstring *str, char *buf, size_t len)
 Adopt a heap buffer, taking ownership 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_init (cwist_sstring *str)
 Initialize an sstring.
 
cwist_error_t cwist_sstring_init_escaped (cwist_sstring *str)
 Initialize an sstring with escaping enabled.
 
cwist_error_t cwist_sstring_ltrim (cwist_sstring *str)
 Left-trim whitespace.
 
cwist_error_t cwist_sstring_rtrim (cwist_sstring *str)
 Right-trim whitespace.
 
cwist_error_t cwist_sstring_trim (cwist_sstring *str)
 Trim whitespace from both ends.
 
cwist_error_t cwist_sstring_change_size (cwist_sstring *str, size_t size, bool blow_data)
 Change the internal buffer size.
 
cwist_error_t cwist_sstring_assign (cwist_sstring *str, const char *data)
 Assign a C string.
 
cwist_error_t cwist_sstring_append (cwist_sstring *str, const char *data)
 Append a C string.
 
cwist_error_t cwist_sstring_append_escaped (cwist_sstring *str, const char *data)
 Append an escaped C string.
 
cwist_error_t cwist_sstring_append_sstring (cwist_sstring *str, const cwist_sstring *from)
 Append another sstring.
 
cwist_error_t cwist_sstring_append_sstring_escaped (cwist_sstring *str, const cwist_sstring *from)
 Append another sstring with escaping.
 
cwist_error_t cwist_sstring_seek (cwist_sstring *str, char *substr, int location)
 Seek a substring at a location.
 
cwist_error_t cwist_sstring_copy (cwist_sstring *origin, char *destination)
 Copy to a C string buffer.
 
cwist_error_t cwist_sstring_copy_sstring (cwist_sstring *origin, const cwist_sstring *from)
 Copy from another sstring.
 
int cwist_sstring_compare (cwist_sstring *str, const char *compare_to)
 Compare with a C string.
 
int cwist_sstring_compare_sstring (cwist_sstring *left, const cwist_sstring *right)
 Compare two sstrings.
 
size_t cwist_sstring_get_size (cwist_sstring *str)
 Get the current size.
 
cwist_sstring * cwist_sstring_substr (cwist_sstring *str, int start, int length)
 Extract a substring.
 

Detailed Description

Safe string type with helper methods.

Definition in file sstring.h.

Typedef Documentation

◆ cwist_sstring

Enumeration Type Documentation

◆ cwist_sstring_error_t

Enumerator
ERR_SSTRING_OKAY 
ERR_SSTRING_ZERO_LENGTH 
ERR_SSTRING_NULL_STRING 
ERR_SSTRING_CONSTANT 
ERR_SSTRING_RESIZE_TOO_SMALL 
ERR_SSTRING_RESIZE_TOO_LARGE 
ERR_SSTRING_OUTOFBOUND 

Definition at line 185 of file sstring.h.

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, taking ownership without copying.

buf must come from cwist_alloc (or compatible) with room for a NUL at buf[len]; the sstring frees it on destroy/reassign. The previous contents are released.

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.

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 an escaped C string.

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 raw bytes with length.

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 
)

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.

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

◆ cwist_sstring_append_sstring_escaped()

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

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.

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 
)

Assign raw bytes with length.

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 
)

Borrow an external buffer without copying.

The sstring points at data (which must outlive the sstring, e.g. a string literal or arena chunk) and never frees it. The first mutating call transparently detaches the contents into owned heap storage, so borrowed strings stay fully mutable from the caller's perspective.

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 
)

Change the internal buffer size.

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.

◆ cwist_sstring_compare()

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

Compare with a C string.

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 
)

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.

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.

◆ cwist_sstring_copy()

cwist_error_t cwist_sstring_copy ( cwist_sstring *  origin,
char *  destination 
)

Copy to a C string 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 
)

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.

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

◆ cwist_sstring_create()

◆ cwist_sstring_destroy()

◆ cwist_sstring_get_size()

size_t cwist_sstring_get_size ( cwist_sstring *  str)

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.

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.

◆ cwist_sstring_init()

◆ cwist_sstring_init_escaped()

cwist_error_t cwist_sstring_init_escaped ( cwist_sstring *  str)

◆ cwist_sstring_ltrim()

cwist_error_t cwist_sstring_ltrim ( cwist_sstring *  str)

Left-trim whitespace.

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.

◆ cwist_sstring_rtrim()

cwist_error_t cwist_sstring_rtrim ( cwist_sstring *  str)

Right-trim whitespace.

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.

◆ cwist_sstring_seek()

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

Seek a substring at a location.

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 
)

Extract a substring.

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 whitespace from both ends.

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.