|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
Implementation of high-performance C string with SSO. More...
#include "cstr.h"#include <assert.h>#include <ctype.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <string.h>Go to the source code of this file.
Functions | |
| cstr * | cstr_init (size_t initial_capacity) |
| Create a new heap-allocated cstr with a given initial capacity. | |
| cstr * | cstr_new (const char *input) |
| Create a new cstr from a C string. | |
| cstr * | cstr_new_len (const char *data, size_t length) |
| Create a new cstr from a buffer of known length (no strlen needed). | |
| void | cstr_drop (cstr *s) |
| Release only the internal heap buffer of an embedded cstr (one created via cstr_init_inplace). Does NOT free the cstr struct itself. | |
| void | cstr_free (cstr *s) |
| Free a heap-allocated cstr and its storage. Safe to call with NULL. | |
| void | cstr_debug (const cstr *s) |
| Print debug information about a cstr to stderr. | |
| bool | cstr_reserve (cstr *s, size_t capacity) |
Ensure at least capacity usable bytes are available (NUL extra). | |
| void | cstr_shrink_to_fit (cstr *s) |
| Shrink heap allocation to fit the current length (frees wasted memory). | |
| bool | cstr_append_char (cstr *s, char c) |
| Append a single character. | |
| bool | cstr_remove (cstr *s, size_t index, size_t count) |
Remove count characters starting at index. | |
| void | cstr_remove_char (cstr *s, char c) |
| Remove a specific character from every position. | |
| void | cstr_remove_substr (cstr *s, size_t start, size_t slen) |
Remove a run of substr_length bytes starting at start. | |
| int | cstr_cmp (const cstr *s1, const cstr *s2) |
| Lexicographic compare. NULL < non-NULL; two NULLs are equal. | |
| cstr * | cstr_join (const cstr **strings, size_t count, const char *delim) |
| Join an array of cstr pointers with a delimiter. | |
Implementation of high-performance C string with SSO.
Optimisation notes:
SSO self-referential pointer On init/SSO-promote, data is set to &s->buf[0]. Every read of string bytes goes through one unconditional pointer dereference — no ternary, no branch. Only free/resize needs cstr_is_heap().
uint32_t fields Struct is 32 bytes on 64-bit. length and capacity fit in L1 cache together with one hot 16-byte SSO string.
Search — cstr_search_impl() Uses a Sunday/Horspool bad-character skip table for needle length > 8. For short needles (≤ 8 bytes) it uses a SWAR (SIMD-Within-A-Register) first-byte scan to skip directly to candidates, avoiding glibc memmem's dynamic dispatch and the dynamic linker overhead it sometimes carries. Both paths avoid touching more haystack memory than necessary.
replace_all Single forward scan with a fixed-size stack-local offset table to avoid heap allocation for the common case (< 64 matches).
Growth policy Exact doubling from the next power-of-two above the request, ensuring amortised O(1) appends with no fractional-factor rounding surprises.
Definition in file cstr.c.
| cstr * cstr_init | ( | size_t | initial_capacity | ) |
Create a new heap-allocated cstr with a given initial capacity.
| initial_capacity | Desired usable capacity (NUL not counted). |
Definition at line 127 of file cstr.c.
References cstr_init_inplace(), and CSTR_SSO_CAP.
Referenced by cstr_join().
| cstr * cstr_new | ( | const char * | input | ) |
Create a new cstr from a C string.
| input | NUL-terminated source. Must not be NULL. |
Definition at line 146 of file cstr.c.
References cstr_new_len().
| cstr * cstr_new_len | ( | const char * | data, |
| size_t | length | ||
| ) |
Create a new cstr from a buffer of known length (no strlen needed).
| data | Pointer to characters (need not be NUL-terminated). |
| length | Number of bytes to copy. |
Definition at line 151 of file cstr.c.
References cstr_init_inplace(), cstr::data, and cstr::length.
Referenced by cstr_join(), and cstr_new().
| bool cstr_reserve | ( | cstr * | s, |
| size_t | capacity | ||
| ) |
Ensure at least capacity usable bytes are available (NUL extra).
Definition at line 205 of file cstr.c.
Referenced by cstr_resize().