chibicc/strings.c

32 lines
690 B
C
Raw Normal View History

2020-10-08 08:30:04 +03:00
#include "chibicc.h"
2020-10-08 08:34:23 +03:00
void strarray_push(StringArray *arr, char *s) {
if (!arr->data) {
arr->data = calloc(8, sizeof(char *));
arr->capacity = 8;
}
if (arr->capacity == arr->len) {
arr->data = 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++] = s;
}
2020-10-08 08:30:04 +03:00
// 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;
}