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
9#define ALLOC stdalloc
10#define NAME string_builder
12
13static void example_basic(DC_LOGGER* parent) {
14 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
15 DC_SCOPED(string_builder) sb = string_builder_new(stdalloc_get_ref());
16
17 fprintf(string_builder_stream(&sb), "Hello, world!");
18
19 DC_LOG(log, DC_INFO, "built string: %s", string_builder_string(&sb));
20}
21
22#define KEY uint32_t
23#define KEY_HASH DC_DEFAULT_HASH
24#define VALUE char const*
25#define NAME id_map
27
28static void example_data_structure(DC_LOGGER* parent) {
29 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
30 DC_SCOPED(id_map) map = id_map_new(stdalloc_get_ref());
31
32 DC_LOG(log, DC_INFO, "inserting identities");
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 DC_LOG(log, DC_INFO, "%s", 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(DC_LOGGER* parent) {
58 DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
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 DC_LOG(log, DC_INFO, "hybrid built string: %s", string_builder_hybrid_string(&sb));
67}
68
69int main() {
71 root = NS(DC_LOGGER,
72 new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
73 (dc_log_id){"string_builder"});
74
75 example_basic(&root);
77 example_hybrid(&root);
78 return 0;
79}
#define DC_LOGGER
Definition file.h:168
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15
#define NS(pre, post)
Definition namespace.h:14
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
static void example_data_structure(DC_LOGGER *parent)
static void example_basic(DC_LOGGER *parent)
static void example_hybrid(DC_LOGGER *parent)
int main()
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8