|
cwist
Modern C web framework with libttak runtime
|
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>
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. | |
Mutable string helpers used throughout CWIST for request/response and utility text handling.
Definition in file sstring.c.
| 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.
| str | Target string object; any owned buffer it holds is released. |
| buf | cwist_alloc'd buffer with room for a NUL at buf[len]; ownership transfers to the string (freed on destroy/reassign). NULL clears. |
| len | Payload length in bytes. |
< 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_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.
| str | Target string object; any owned buffer it holds is released. |
| base | cwist_alloc'd allocation base; ownership transfers to the string (freed on destroy/reassign). NULL clears. |
| offset | Payload start relative to base. |
| len | Payload length in bytes; base[offset + len] must be the NUL slot. |
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_error_t cwist_sstring_append | ( | cwist_sstring * | str, |
| const char * | data | ||
| ) |
Append a NUL-terminated C string to the target string.
Append a C string.
| str | Target string object. |
| data | Source string to append. |
Definition at line 559 of file sstring.c.
References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, CWIST_ERR_JSON, cwist_sstring_append(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, cwist_sstring::is_fixed, make_error(), and cwist_sstring::size.
Referenced by cwist_cookie_delete(), cwist_cookie_set(), cwist_css_generate_stylesheet(), cwist_css_generate_utility_classes(), cwist_css_generate_variables(), cwist_html_element_add_class(), cwist_http_response_add_oob(), cwist_json_add_bool(), cwist_json_add_int(), cwist_json_add_null(), cwist_json_add_string(), cwist_json_begin_array(), cwist_json_begin_object(), cwist_json_end_array(), cwist_json_end_object(), cwist_redis_del(), cwist_redis_expire(), cwist_redis_get(), cwist_redis_publish(), cwist_redis_set(), cwist_redis_setex(), cwist_redis_subscribe(), cwist_sstring_append(), cwist_sstring_append_sstring(), and cwist_url_for().
| 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.
| str | Target string object. |
| data | Source string to append in escaped form. |
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_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.
| str | Target string object. |
| data | Source bytes to append. |
| len | Number of source bytes to append. |
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_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.
| str | Destination string. |
| from | Source CWIST string. |
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_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.
| str | Destination string. |
| from | Source CWIST string. |
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_error_t cwist_sstring_assign | ( | cwist_sstring * | str, |
| const char * | data | ||
| ) |
Replace the string contents with a NUL-terminated C string.
Assign a C string.
| str | Target string object. |
| data | Source string, or NULL to clear the value. |
Definition at line 504 of file sstring.c.
References cwist_sstring::borrows_buffer, cap, CWIST_ERR_INT8, CWIST_ERR_JSON, cwist_sstring_assign(), cwist_sstring_change_size(), cwist_sstring::data, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, cwist_sstring::is_fixed, make_error(), and cwist_sstring::size.
Referenced by cwist_app_healthz(), cwist_assets_respond(), cwist_async_abort(), cwist_async_respond(), cwist_bind_write_error_response(), cwist_css_bundle(), cwist_css_scope_generate_stylesheet(), cwist_get_client_ip_from_fd(), cwist_graphql_execute(), cwist_graphql_serve(), cwist_grpc_set_response(), cwist_html_element_add_class(), cwist_html_element_create(), cwist_html_element_set_text(), cwist_html_render(), cwist_http_client_request(), cwist_http_response_html_redirect(), cwist_metrics_serve_http(), cwist_mux_handle(), cwist_sstring_assign(), cwist_sstring_copy_sstring(), and cwist_websocket_upgrade_response().
| 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.
| str | Target string object. |
| data | Source bytes to copy. |
| len | Number of source bytes to copy. |
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_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.
| str | Target string object; any owned buffer it holds is released. |
| data | Borrowed bytes; must outlive the string (literal, arena chunk, ...). |
| len | Length of the borrowed bytes. |
< 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_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.
| str | String object to resize. |
| new_size | Requested new size in bytes. |
| blow_data | When true, allow shrinking below the current content length. |
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().
| int cwist_sstring_compare | ( | cwist_sstring * | str, |
| const char * | compare_to | ||
| ) |
| 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.
| left | Left-hand string. |
| right | Right-hand string. |
Definition at line 320 of file sstring.c.
References cap.
Referenced by cwist_sstring_create(), cwist_sstring_init(), and cwist_sstring_init_escaped().
| 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.
| origin | Source string object. |
| destination | Destination C buffer. |
Definition at line 766 of file sstring.c.
References cap, CWIST_ERR_INT8, ERR_SSTRING_NULL_STRING, ERR_SSTRING_OKAY, and make_error().
| 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.
| origin | Destination string object. |
| from | Source string object, or NULL to clear the destination. |
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 * cwist_sstring_create | ( | void | ) |
Allocate and initialize a new heap-owned CWIST string object.
Create a new sstring instance.
Definition at line 802 of file sstring.c.
References cwist_sstring::append, cap, cwist_sstring::compare, cwist_sstring::copy, cwist_alloc(), cwist_sstring_append_sstring(), cwist_sstring_compare_sstring(), cwist_sstring_copy_sstring(), cwist_sstring_get_size(), cwist_sstring::data, cwist_sstring::get_size, cwist_sstring::is_fixed, cwist_sstring::owns_storage, and cwist_sstring::size.
Referenced by cwist_cookie_delete(), cwist_cookie_set(), cwist_css_bundle(), cwist_css_generate_stylesheet(), cwist_css_generate_utility_classes(), cwist_css_generate_variables(), cwist_css_minify(), cwist_css_scope_generate_stylesheet(), cwist_get_client_ip_from_fd(), cwist_graphql_execute(), cwist_html_element_add_class(), cwist_html_element_create(), cwist_html_element_set_text(), cwist_html_render(), cwist_http_receive_request(), cwist_http_receive_request_nb(), cwist_http_stringify_response(), cwist_json_builder_create(), cwist_mux_handle(), cwist_redis_command_argv(), cwist_redis_command_argv_reply(), cwist_redis_del(), cwist_redis_expire(), cwist_redis_get(), cwist_redis_publish(), cwist_redis_set(), cwist_redis_setex(), cwist_redis_subscribe(), cwist_sstring_substr(), and cwist_url_for().
| void cwist_sstring_destroy | ( | cwist_sstring * | str | ) |
Destroy a heap-owned CWIST string and its backing buffer.
Destroy an sstring instance.
| str | String object to destroy. |
Definition at line 823 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_free(), cwist_sstring::data, cwist_sstring::get_size, cwist_sstring::owns_storage, and cwist_sstring::size.
Referenced by cwist_cookie_delete(), cwist_cookie_set(), cwist_css_bundle(), cwist_css_generate_stylesheet(), cwist_css_minify(), cwist_css_scope_destroy(), cwist_css_scope_generate_stylesheet(), cwist_graphql_serve(), cwist_html_element_add_class(), cwist_html_element_destroy(), cwist_http_header_free_all(), cwist_http_header_remove(), cwist_http_receive_request(), cwist_http_receive_request_nb(), cwist_http_request_destroy(), cwist_http_response_add_oob(), cwist_http_response_destroy(), cwist_http_response_set_html(), cwist_json_builder_destroy(), cwist_mux_router_destroy(), cwist_redis_command_argv(), cwist_redis_command_argv_reply(), cwist_redis_del(), cwist_redis_expire(), cwist_redis_get(), cwist_redis_publish(), cwist_redis_set(), cwist_redis_setex(), cwist_redis_subscribe(), cwist_sse_response_comment(), cwist_sse_response_event(), cwist_sse_stream_comment(), cwist_sse_stream_send(), cwist_sstring_substr(), and cwist_url_for().
| 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.
| str | String object to inspect. |
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_error_t cwist_sstring_init | ( | cwist_sstring * | str | ) |
Initialize a mutable string with the default append/copy behavior.
Initialize an sstring.
| str | String object to initialize in caller-owned storage. |
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_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.
| str | String object to initialize in caller-owned storage. |
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_error_t cwist_sstring_ltrim | ( | cwist_sstring * | str | ) |
Trim leading ASCII whitespace from the string in place.
Left-trim whitespace.
| str | String object to mutate. |
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_error_t cwist_sstring_rtrim | ( | cwist_sstring * | str | ) |
Trim trailing ASCII whitespace from the string in place.
Right-trim whitespace.
| str | String object to mutate. |
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_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.
| str | Source string object. |
| substr | Destination C buffer. |
| location | Starting offset inside the source string. |
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 * cwist_sstring_substr | ( | cwist_sstring * | str, |
| int | start, | ||
| int | length | ||
| ) |
Create a heap-owned substring spanning the requested range.
Extract a substring.
| str | Source string object. |
| start | Zero-based starting index. |
| length | Requested substring length. |
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_error_t cwist_sstring_trim | ( | cwist_sstring * | str | ) |
Trim both leading and trailing ASCII whitespace from the string.
Trim whitespace from both ends.
| str | String object to mutate. |
Definition at line 404 of file sstring.c.
References cap, cwist_sstring_ltrim(), cwist_sstring_rtrim(), and ERR_SSTRING_OKAY.