Derive-C
Loading...
Searching...
No Matches
string_builder.c File Reference

Go to the source code of this file.

Macros

#define ALLOC   stdalloc
#define NAME   string_builder
#define KEY   uint32_t
#define KEY_HASH   DC_DEFAULT_HASH
#define VALUE   char const*
#define NAME   id_map
#define NAME   dbg
#define CAPACITY   256
#define ALLOC   dbg
#define NAME   hybrid
#define ALLOC   hybrid
#define NAME   string_builder_hybrid

Functions

static void example_basic (DC_LOGGER *parent)
static void example_data_structure (DC_LOGGER *parent)
static void example_hybrid (DC_LOGGER *parent)
int main ()

Macro Definition Documentation

◆ ALLOC [1/3]

#define ALLOC   hybrid

Definition at line 9 of file string_builder.c.

◆ ALLOC [2/3]

#define ALLOC   dbg

Definition at line 9 of file string_builder.c.

◆ ALLOC [3/3]

#define ALLOC   stdalloc

Definition at line 9 of file string_builder.c.

◆ CAPACITY

#define CAPACITY   256

Definition at line 48 of file string_builder.c.

◆ KEY

#define KEY   uint32_t

Definition at line 22 of file string_builder.c.

◆ KEY_HASH

#define KEY_HASH   DC_DEFAULT_HASH

Definition at line 23 of file string_builder.c.

◆ NAME [1/5]

#define NAME   string_builder_hybrid

Definition at line 10 of file string_builder.c.

◆ NAME [2/5]

#define NAME   hybrid

Definition at line 10 of file string_builder.c.

◆ NAME [3/5]

#define NAME   dbg

Definition at line 10 of file string_builder.c.

◆ NAME [4/5]

#define NAME   id_map

Definition at line 10 of file string_builder.c.

◆ NAME [5/5]

#define NAME   string_builder

Definition at line 10 of file string_builder.c.

◆ VALUE

#define VALUE   char const*

Definition at line 24 of file string_builder.c.

Function Documentation

◆ example_basic()

void example_basic ( DC_LOGGER * parent)
static

Definition at line 13 of file string_builder.c.

13 {
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}
#define DC_LOGGER
Definition file.h:168
#define DC_SCOPED(type,...)
RAII in C. Call the destructor when the variable goes out of scope.
Definition scope.h:5
#define DC_LOGGER_NEW(...)
Definition prelude.h:21
#define DC_LOG(...)
Definition prelude.h:20
@ DC_INFO
Definition trait.h:8

◆ example_data_structure()

void example_data_structure ( DC_LOGGER * parent)
static
Examples
utils/string_builder.c.

Definition at line 28 of file string_builder.c.

28 {
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}
static DC_PUBLIC dc_debug_fmt dc_debug_fmt_new()
Definition fmt.h:15

◆ example_hybrid()

void example_hybrid ( DC_LOGGER * parent)
static
Examples
utils/string_builder.c.

Definition at line 57 of file string_builder.c.

57 {
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}

◆ main()

int main ( )

Definition at line 69 of file string_builder.c.

69 {
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 NS(pre, post)
Definition namespace.h:14
static void example_data_structure(DC_LOGGER *parent)
static void example_basic(DC_LOGGER *parent)
static void example_hybrid(DC_LOGGER *parent)