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

Examples for using the string builder.

Examples for using the string builder.

#define ALLOC stdalloc
#define NAME string_builder
static void example_basic(DC_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_SCOPED(string_builder) sb = string_builder_new(stdalloc_get_ref());
fprintf(string_builder_stream(&sb), "Hello, world!");
DC_LOG(log, DC_INFO, "built string: %s", 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_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
DC_SCOPED(id_map) map = id_map_new(stdalloc_get_ref());
DC_LOG(log, DC_INFO, "inserting identities");
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));
DC_LOG(log, DC_INFO, "%s", 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_LOGGER* parent) {
DC_SCOPED(DC_LOGGER) log = DC_LOGGER_NEW(parent, "%s", __func__);
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.");
DC_LOG(log, DC_INFO, "hybrid built string: %s", string_builder_hybrid_string(&sb));
}
int main() {
root = NS(DC_LOGGER,
new_global)((NS(DC_LOGGER, global_config)){.stream = stdout, .ansi_colours = true},
(dc_log_id){"string_builder"});
example_basic(&root);
return 0;
}
int main()
Definition alloc.c:120
static void example_basic(DC_LOGGER *parent)
Definition bitset.c:11
#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_hybrid(DC_LOGGER *parent)
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8