-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrings.cpp
More file actions
33 lines (28 loc) · 778 Bytes
/
strings.cpp
File metadata and controls
33 lines (28 loc) · 778 Bytes
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
#include "chibicc.h"
void strarray_push(StringArray *arr, const char *s) {
if (!arr->data) {
arr->data = (char **)calloc(8, sizeof(char *));
arr->capacity = 8;
}
if (arr->capacity == arr->len) {
arr->data = (char **)realloc(arr->data, sizeof(char *) * arr->capacity * 2);
arr->capacity *= 2;
for (int i = arr->len; i < arr->capacity; i++)
arr->data[i] = NULL;
}
arr->data[arr->len] = new char[strlen(s)];
strcpy(arr->data[arr->len], s);
arr->len++;
}
// Takes a printf-style format string and returns a formatted string.
char *format(char *fmt, ...) {
char *buf;
size_t buflen;
FILE *out = open_memstream(&buf, &buflen);
va_list ap;
va_start(ap, fmt);
vfprintf(out, fmt, ap);
va_end(ap);
fclose(out);
return buf;
}