Derive-C
Loading...
Searching...
No Matches
string_builder.c
Go to the documentation of this file.
1
4
7#include <derive-c/prelude.h>
8#include <stdio.h>
9
10#define ALLOC stdalloc
11#define NAME string_builder
13
14static void example_basic() {
16 DC_SCOPED(string_builder) sb = string_builder_new(stdalloc_get_ref());
17
18 fprintf(string_builder_stream(&sb), "Hello, world!");
19
20 printf("%s\n", string_builder_string(&sb));
21}
22
23#define KEY uint32_t
24#define KEY_HASH DC_DEFAULT_HASH
25#define VALUE char const*
26#define NAME id_map
28
31 DC_SCOPED(id_map) map = id_map_new(stdalloc_get_ref());
32
33 id_map_insert(&map, 1, "Alice");
34 id_map_insert(&map, 2, "Bob");
35 id_map_insert(&map, 3, "Charlie");
36
37 DC_SCOPED(string_builder) sb = string_builder_new(stdalloc_get_ref());
38
39 fprintf(string_builder_stream(&sb), "the identities are: ");
40 id_map_debug(&map, dc_debug_fmt_new(), string_builder_stream(&sb));
41
42 printf("%s\n", string_builder_string(&sb));
43}
44
45#define NAME dbg
47
48#define CAPACITY 256
49#define ALLOC dbg
50#define NAME hybrid
52
53#define ALLOC hybrid
54#define NAME string_builder_hybrid
56
57static void example_hybrid() {
59 DC_SCOPED(dbg) debug_alloc = dbg_new("hybrid_example", stdout, stdalloc_get_ref());
60 hybrid_buffer buf;
61 DC_SCOPED(hybrid) hybrid_alloc = hybrid_new(&buf, &debug_alloc);
62 DC_SCOPED(string_builder_hybrid) sb = string_builder_hybrid_new(&hybrid_alloc);
63
64 fprintf(string_builder_hybrid_stream(&sb), "This is a small sentence.");
65
66 printf("%s\n", string_builder_hybrid_string(&sb));
67}
68
69int main() {
73 return 0;
74}
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_basic()
static void example_hybrid()
static void example_data_structure()
int main()
#define DC_DEBUG_TRACE
Definition debug.h:17