|
cbase 1.50.0
C/C++ Static Template
|
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. | |
Accumulate strings efficiently and join them into a single allocation.
| 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.
|
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.
| sb | The builder to append to. |
| alloc | Allocator from which to allocate the tracking node. |
| str | The string slice to append. |
Definition at line 75 of file str_builder.h.

|
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.
| sb | The builder to append to. |
| alloc | Allocator from which to allocate the node. |
| cstr | Null-terminated string to append. |
Definition at line 103 of file str_builder.h.

|
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.
| sb | The builder to append to. |
| alloc | Allocator for both the node and the formatted string data. |
| fmt | printf-style format string. |
| ... | Format arguments. |
Definition at line 121 of file str_builder.h.

|
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.
| sb | The builder whose segments to join. |
| out_alloc | Allocator that will own the final contiguous string. |
Definition at line 157 of file str_builder.h.

|
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.
| sb | The builder to reset. |
Definition at line 184 of file str_builder.h.