cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
str_builder.h
Go to the documentation of this file.
1
31
32#ifndef STR_BUILDER_H
33#define STR_BUILDER_H
34
35#include "base_macros.h"
36#include "base_strings.h"
37#include "base_types.h"
39#include <stdarg.h>
40#include <stdio.h>
41
43
49
62
74function inline void
76{
77 String8Node *node = MAKE_STRUCT(String8Node, alloc);
78 ASSERT_NOT_NULL(node);
79 node->string = str;
80 node->next = NULL;
81
82 if (sb->last != NULL) {
83 sb->last->next = node;
84 } else {
85 sb->first = node;
86 }
87 sb->last = node;
88 sb->node_count += 1;
89 sb->total_size += str.size;
90}
91
102function inline void
103sb_append_cstr(StringBuilder *sb, MemAllocator *alloc, const char *cstr)
104{
105 sb_append(sb, alloc, str8_cstr(cstr));
106}
107
120function inline void
121sb_append_fmt(StringBuilder *sb, MemAllocator *alloc, const char *fmt, ...)
122{
123 va_list args;
124 va_start(args, fmt);
125 int len = vsnprintf(NULL, 0, fmt, args);
126 va_end(args);
127
128 if (len <= 0) {
129 return;
130 }
131
132 char *buf = (char *)mem_alloc(alloc, (usize)len + 1);
133 ASSERT_NOT_NULL(buf);
134
135 va_start(args, fmt);
136 vsnprintf(buf, (usize)len + 1, fmt, args);
137 va_end(args);
138
139 sb_append(sb, alloc, str8((u8 *)buf, (usize)len));
140}
141
156function inline String8
158{
159 if (sb->total_size == 0) {
160 return (String8){0};
161 }
162
163 u8 *buf = (u8 *)mem_alloc(out_alloc, sb->total_size);
164 ASSERT_NOT_NULL(buf);
165
166 usize offset = 0;
167 for (String8Node *n = sb->first; n != NULL; n = n->next) {
168 MEM_COPY(buf + offset, n->string.str, n->string.size);
169 offset += n->string.size;
170 }
171
172 return str8(buf, sb->total_size);
173}
174
183function inline void
185{
186 sb->first = NULL;
187 sb->last = NULL;
188 sb->node_count = 0;
189 sb->total_size = 0;
190}
191
193
195
196#endif // STR_BUILDER_H
Context detection, compiler abstractions, utility macros, and data structures.
Sized UTF-8 string slices (String8) and manipulation utilities.
Core fixed-width type aliases and memory size constants.
uint8_t u8
Definition base_types.h:46
#define ASSERT_NOT_NULL(ptr)
Asserts that a given pointer is not NULL.
#define function
File-scoped function (invisible to the linker).
#define C_LINKAGE_END
Closes a C linkage block for C++ compilers (Empty in C).
#define C_LINKAGE_BEGIN
Opens a C linkage block for C++ compilers (Empty in C).
#define MAKE_STRUCT(type, alloc)
Allocates and zero-initialises a single instance of type.
function void * mem_alloc(MemAllocator *alloc, usize size)
Allocator Helper Functions.
#define MEM_COPY(dst, src, size)
Copies memory using memmove (overlap-safe).
size_t usize
Definition base_types.h:68
static void sb_append_cstr(StringBuilder *sb, MemAllocator *alloc, const char *cstr)
Appends a null-terminated C string to the builder.
static void sb_reset(StringBuilder *sb)
Resets the builder to empty without touching the allocator.
static void sb_append(StringBuilder *sb, MemAllocator *alloc, String8 str)
Appends a String8 slice to the builder.
Definition str_builder.h:75
static String8 sb_join(StringBuilder *sb, MemAllocator *out_alloc)
Materializes all appended segments into a single contiguous String8.
static void sb_append_fmt(StringBuilder *sb, MemAllocator *alloc, const char *fmt,...)
Appends a printf-style formatted string to the builder.
static String8 str8(u8 *str, usize size)
Creates a String8 from a raw pointer and explicit byte length.
static String8 str8_cstr(const char *cstr)
Converts a null-terminated C string into a String8.
Generic interface for abstracting memory allocators.
An abstract allocator interface.
A sized UTF-8 string slice.
usize size
A node for building singly-linked lists of strings.
struct String8Node * next
String8 string
An allocator-agnostic string builder.
Definition str_builder.h:56
String8Node * last
Definition str_builder.h:58
String8Node * first
Definition str_builder.h:57