solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
Functions
cstr.c File Reference

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>
Include dependency graph for cstr.c:

Go to the source code of this file.

Functions

cstrcstr_init (size_t initial_capacity)
 Create a new heap-allocated cstr with a given initial capacity.
 
cstrcstr_new (const char *input)
 Create a new cstr from a C string.
 
cstrcstr_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.
 
cstrcstr_join (const cstr **strings, size_t count, const char *delim)
 Join an array of cstr pointers with a delimiter.
 

Detailed Description

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.

Function Documentation

◆ cstr_init()

cstr * cstr_init ( size_t  initial_capacity)

Create a new heap-allocated cstr with a given initial capacity.

Parameters
initial_capacityDesired usable capacity (NUL not counted).
Returns
New cstr, or NULL on OOM.
Note
Caller must call cstr_free().

Definition at line 127 of file cstr.c.

References cstr_init_inplace(), and CSTR_SSO_CAP.

Referenced by cstr_join().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cstr_new()

cstr * cstr_new ( const char *  input)

Create a new cstr from a C string.

Parameters
inputNUL-terminated source. Must not be NULL.
Returns
New cstr, or NULL on OOM.
Note
Caller must call cstr_free().

Definition at line 146 of file cstr.c.

References cstr_new_len().

Here is the call graph for this function:

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

Parameters
dataPointer to characters (need not be NUL-terminated).
lengthNumber of bytes to copy.
Returns
New cstr, or NULL on OOM.

Definition at line 151 of file cstr.c.

References cstr_init_inplace(), cstr::data, and cstr::length.

Referenced by cstr_join(), and cstr_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cstr_reserve()

bool cstr_reserve ( cstr s,
size_t  capacity 
)

Ensure at least capacity usable bytes are available (NUL extra).

Returns
true on success, false on OOM or overflow.

Definition at line 205 of file cstr.c.

Referenced by cstr_resize().

Here is the caller graph for this function: