Derive-C
Loading...
Searching...
No Matches
utils/string_builder.c

Examples for using the string builder.

Examples for using the string builder.

#include <stdio.h>
#define ALLOC stdalloc
#define NAME string_builder
static void example_basic() {
DC_SCOPED(string_builder) sb = string_builder_new(stdalloc_get_ref());
fprintf(string_builder_stream(&sb), "Hello, world!");
printf("%s\n", string_builder_string(&sb));
}
#define KEY uint32_t
#define KEY_HASH DC_DEFAULT_HASH
#define VALUE char const*
#define NAME id_map
static void example_data_structure() {
DC_SCOPED(id_map) map = id_map_new(stdalloc_get_ref());
id_map_insert(&map, 1, "Alice");
id_map_insert(&map, 2, "Bob");
id_map_insert(&map, 3, "Charlie");
DC_SCOPED(string_builder) sb = string_builder_new(stdalloc_get_ref());
fprintf(string_builder_stream(&sb), "the identities are: ");
id_map_debug(&map, dc_debug_fmt_new(), string_builder_stream(&sb));
printf("%s\n", string_builder_string(&sb));
}
#define NAME dbg
#define CAPACITY 256
#define ALLOC dbg
#define NAME hybrid
#define ALLOC hybrid
#define NAME string_builder_hybrid
static void example_hybrid() {
DC_SCOPED(dbg) debug_alloc = dbg_new("hybrid_example", stdout, stdalloc_get_ref());
hybrid_buffer buf;
DC_SCOPED(hybrid) hybrid_alloc = hybrid_new(&buf, &debug_alloc);
DC_SCOPED(string_builder_hybrid) sb = string_builder_hybrid_new(&hybrid_alloc);
fprintf(string_builder_hybrid_stream(&sb), "This is a small sentence.");
printf("%s\n", string_builder_hybrid_string(&sb));
}
int main() {
return 0;
}
int main()
Definition alloc.c:109
static void example_basic()
Definition bitset.c:12
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
static void example_hybrid()
static void example_data_structure()
#define DC_DEBUG_TRACE
Definition debug.h:17