From ef9ab1fd141f4057d41f2d6ed8ab8d67c44894d5 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 9 Jan 2025 16:32:55 -0800 Subject: save state --- src/common/better_string.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/common/better_string.c') diff --git a/src/common/better_string.c b/src/common/better_string.c index 366dda6..7863da0 100644 --- a/src/common/better_string.c +++ b/src/common/better_string.c @@ -18,11 +18,12 @@ string_t *init_string(const char *src) { } void string_push(string_t *s, char c) { - if (s->len >= s->size - 2) { + if (s->len >= s->size - 3) { s->size *= 2; s->buf = safe_realloc(s->buf, s->size); } s->buf[s->len] = c; + s->buf[s->len + 1] = '\0'; s->len++; } @@ -33,9 +34,8 @@ char string_pop(string_t *s) { } void string_concat_const(string_t *s1, const char *s2) { - for (int i = 0; i < strlen(s2); i++) { + for (int i = 0; i < strlen(s2); i++) string_push(s1, s2[i]); - } } void string_concat(string_t *s1, string_t *s2) { @@ -46,6 +46,10 @@ void string_concat(string_t *s1, string_t *s2) { void string_free(void *x) { string_t *s = x; + + if (!x) + return; + free(s->buf); free(s); } -- cgit