aboutsummaryrefslogtreecommitdiff
path: root/src/common/better_string.c
diff options
context:
space:
mode:
authorPreston Pan <ret2pop@gmail.com>2025-01-09 16:32:55 -0800
committerPreston Pan <ret2pop@gmail.com>2025-01-09 16:32:55 -0800
commitef9ab1fd141f4057d41f2d6ed8ab8d67c44894d5 (patch)
treee4005b7a641303b021eb54c2aae5676b5f92a72d /src/common/better_string.c
parent1fd608288ee47c2c560817f12f14b21069fed2f6 (diff)
save stateHEADmain
Diffstat (limited to 'src/common/better_string.c')
-rw-r--r--src/common/better_string.c10
1 files changed, 7 insertions, 3 deletions
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);
}