cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
String Builder

Accumulate strings efficiently and join them into a single allocation. More...

Classes

struct  StringBuilder
 An allocator-agnostic string builder. More...

Typedefs

typedef struct StringBuilder StringBuilder
 An allocator-agnostic string builder.

Functions

static void sb_append (StringBuilder *sb, MemAllocator *alloc, String8 str)
 Appends a String8 slice to the builder.
static void sb_append_cstr (StringBuilder *sb, MemAllocator *alloc, const char *cstr)
 Appends a null-terminated C string to the builder.
static void sb_append_fmt (StringBuilder *sb, MemAllocator *alloc, const char *fmt,...)
 Appends a printf-style formatted string to the builder.
static String8 sb_join (StringBuilder *sb, MemAllocator *out_alloc)
 Materializes all appended segments into a single contiguous String8.
static void sb_reset (StringBuilder *sb)
 Resets the builder to empty without touching the allocator.

Detailed Description

Accumulate strings efficiently and join them into a single allocation.

Typedef Documentation

◆ StringBuilder

typedef struct StringBuilder StringBuilder

An allocator-agnostic string builder.

Zero-initialize to get an empty builder. Does not own any memory - all allocations go through the MemAllocator passed to each call.

Function Documentation

◆ sb_append()

void sb_append ( StringBuilder * sb,
MemAllocator * alloc,
String8 str )
inlinestatic

Appends a String8 slice to the builder.

Allocates a single String8Node from alloc to track the reference. The string data itself is NOT copied - the node points into whatever memory backs str. That memory must outlive the sb_join() call.

Parameters
sbThe builder to append to.
allocAllocator from which to allocate the tracking node.
strThe string slice to append.

Definition at line 75 of file str_builder.h.

Here is the caller graph for this function:

◆ sb_append_cstr()

void sb_append_cstr ( StringBuilder * sb,
MemAllocator * alloc,
const char * cstr )
inlinestatic

Appends a null-terminated C string to the builder.

Wraps the C string in a String8 (no copy of the data) and appends a node. The C string must remain valid until sb_join() is called.

Parameters
sbThe builder to append to.
allocAllocator from which to allocate the node.
cstrNull-terminated string to append.

Definition at line 103 of file str_builder.h.

Here is the call graph for this function:

◆ sb_append_fmt()

void sb_append_fmt ( StringBuilder * sb,
MemAllocator * alloc,
const char * fmt,
... )
inlinestatic

Appends a printf-style formatted string to the builder.

The formatted output is written into alloc as a new allocation, so the resulting data is owned by the allocator and does not depend on the format arguments after this call returns.

Parameters
sbThe builder to append to.
allocAllocator for both the node and the formatted string data.
fmtprintf-style format string.
...Format arguments.

Definition at line 121 of file str_builder.h.

Here is the call graph for this function:

◆ sb_join()

String8 sb_join ( StringBuilder * sb,
MemAllocator * out_alloc )
inlinestatic

Materializes all appended segments into a single contiguous String8.

Allocates one buffer from out_alloc, copies every segment in order, and returns the result. The returned string is NOT null-terminated unless the last segment ended with a NUL byte.

The node list is left intact. Call sb_reset() or mem_free_all() on the scratch allocator to reclaim node memory.

Parameters
sbThe builder whose segments to join.
out_allocAllocator that will own the final contiguous string.
Returns
A String8 pointing into out_alloc, or a zero String8 if empty.

Definition at line 157 of file str_builder.h.

Here is the call graph for this function:

◆ sb_reset()

void sb_reset ( StringBuilder * sb)
inlinestatic

Resets the builder to empty without touching the allocator.

Clears node list pointers and counters. Use mem_free_all() on the scratch allocator if you also want to reclaim the node memory.

Parameters
sbThe builder to reset.

Definition at line 184 of file str_builder.h.