-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrbuilder.c
48 lines (40 loc) · 1.08 KB
/
strbuilder.c
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
#include "common/strbuilder.h"
void sb_init(StringBuilder* sb) {
sb->len = 0;
sb->cap = 256;
sb->buffer = malloc(sb->cap);
}
void sb_destroy(StringBuilder* sb) {
free(sb->buffer);
*sb = (StringBuilder){0};
}
void sb_append(StringBuilder* sb, string s) {
if (sb->len + s.len > sb->cap) {
sb->buffer = realloc(sb->buffer, sb->cap * 2);
sb->cap *= 2;
}
memcpy(&sb->buffer[sb->len], s.raw, s.len);
sb->len += s.len;
}
void sb_append_c(StringBuilder* sb, const char* s) {
if (sb->len + strlen(s) > sb->cap) {
sb->buffer = realloc(sb->buffer, sb->cap * 2);
sb->cap *= 2;
}
memcpy(&sb->buffer[sb->len], s, strlen(s));
sb->len += strlen(s);
}
void sb_printf(StringBuilder* sb, const char* fmt, ...) {
va_list varargs;
va_start(varargs, fmt);
static char buf[1000];
memset(buf, 0, sizeof(buf));
vsprintf(buf, fmt, varargs);
sb_append_c(sb, buf);
}
size_t sb_len(StringBuilder* sb) {
return sb->len;
}
void sb_write(StringBuilder* sb, char* buffer) {
memcpy(buffer, sb->buffer, sb->len);
}