-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmain.c
More file actions
58 lines (45 loc) · 1.68 KB
/
main.c
File metadata and controls
58 lines (45 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#define ARENA_IMPLEMENTATION
#include "arena.h"
// String Builder is any structure that has at least three fields: items, count, and capacity.
// - `items` MUST be a pointer to char and it points to the beginning of the buffer that stores the string.
// - `count` MUST be size_t and it indicates the size of the string.
// - `capacity` MUST be size_t and it indicates how much memory was already allocated for the String Builder.
// String Builder in a sense is just a Dynamic Array of characters
// TODO: maybe arena.h should ship its own String_Builder type?
typedef struct {
char *items;
size_t count;
size_t capacity;
} String_Builder;
int main()
{
Arena a = {0};
// TODO: examples for functions like
// - arena_alloc, arena_realloc
// - arena_strdup, arena_memdup
{
printf("-- sprintf --\n");
printf("%s", arena_sprintf(&a, "Foo, %s, %d, %f\n", "Bar", 69, 420.1337));
}
arena_reset(&a);
{
printf("-- Dynamic Arrays --\n");
String_Builder sb = {0};
char name[] = {'W', 'o', 'r', 'l', 'd'};
// Append NULL-terminated string
arena_sb_append_cstr(&a, &sb, "Hello, ");
// Append sized buffer
arena_sb_append_buf(&a, &sb, name, sizeof(name));
// Append using format strings
arena_sb_append_format(&a, &sb, "\n%d", 5);
arena_sb_append_format(&a, &sb, " %s", "foo");
// Append '\0' to make the built string NULL-terminated
arena_sb_append_null(&a, &sb);
printf("%s\n", sb.items);
// TODO: add more examples for arena_da_append and arena_da_append_many
}
arena_reset(&a);
arena_free(&a);
return 0;
}