|
cwist
Modern C web framework with libttak runtime
|
Safe string type with helper methods. More...


Go to the source code of this file.
Classes | |
| struct | cwist_sstring |
Typedefs | |
| typedef struct cwist_sstring | cwist_sstring |
Safe string type with helper methods.
Definition in file sstring.h.
| 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.
| 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.
| 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 C 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(), and cwist_url_for().
| cwist_error_t cwist_sstring_append_escaped | ( | cwist_sstring * | str, |
| const char * | data | ||
| ) |
Append an escaped C string.
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 raw bytes with length.
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 | ||
| ) |
Append another sstring.
Append the contents of one CWIST string onto another.
| str | Destination string. |
| from | Source CWIST string. |
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().
| 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.
| str | Destination string. |
| from | Source CWIST string. |
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 | ||
| ) |
Assign a 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(), and cwist_websocket_upgrade_response().
| 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.
| 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 | ||
| ) |
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.
| 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 | ||
| ) |
Change the internal buffer size.
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.
| int cwist_sstring_compare | ( | cwist_sstring * | str, |
| const char * | compare_to | ||
| ) |
| int cwist_sstring_compare_sstring | ( | cwist_sstring * | left, |
| const cwist_sstring * | right | ||
| ) |
Compare two sstrings.
Compare two CWIST string objects using strcmp semantics.
| left | Left-hand string. |
| right | Right-hand string. |
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.
| cwist_error_t cwist_sstring_copy | ( | cwist_sstring * | origin, |
| char * | destination | ||
| ) |
Copy to a C string 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 | ||
| ) |
Copy from another sstring.
Copy one CWIST string into another.
| origin | Destination string object. |
| from | Source string object, or NULL to clear the destination. |
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().
| cwist_sstring * cwist_sstring_create | ( | void | ) |
Create a new sstring instance.
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(), and cwist_url_for().
| void cwist_sstring_destroy | ( | cwist_sstring * | str | ) |
Destroy an sstring instance.
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(), and cwist_url_for().
| size_t cwist_sstring_get_size | ( | cwist_sstring * | str | ) |
Get the current size.
Return the cached string size in bytes.
| str | String object to inspect. |
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.
| cwist_error_t cwist_sstring_init | ( | cwist_sstring * | str | ) |
Initialize an sstring.
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 an sstring with escaping enabled.
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 | ) |
Left-trim whitespace.
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.
| cwist_error_t cwist_sstring_rtrim | ( | cwist_sstring * | str | ) |
Right-trim whitespace.
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.
| 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.
| 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 | ||
| ) |
Extract a substring.
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 whitespace from both ends.
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.