From bb2db0d30d44925f1b039150f8a59cc9e18bd6bc Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Mon, 29 Apr 2024 22:55:40 +0100 Subject: [PATCH] Return StringUpdate from the dstr update functions --- src/strings/dstr/dstr.c | 18 +++++++++--------- src/strings/dstr/dstr.h | 11 +++++++++-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/strings/dstr/dstr.c b/src/strings/dstr/dstr.c index 0667a3f..b7517f4 100644 --- a/src/strings/dstr/dstr.c +++ b/src/strings/dstr/dstr.c @@ -54,9 +54,9 @@ String *wapp_dstr_from_string(const char *str, Arena *arena) { return out; } -String *wapp_dstr_update(String **dst, const char *src, Arena *arena) { +StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena) { if (!dst || !(*dst)) { - return *dst; + return (StringUpdate){.updated = false, .str = *dst}; } u64 length = strlen(src); @@ -65,15 +65,15 @@ String *wapp_dstr_update(String **dst, const char *src, Arena *arena) { if (length >= str->capacity) { if (!arena) { - return *dst; + return (StringUpdate){.updated = false, .str = *dst}; } String *new_str = wapp_dstr_from_string(src, arena); if (!new_str) { - return *dst; + return (StringUpdate){.updated = false, .str = *dst}; } - return new_str; + return (StringUpdate){.updated = true, .str = new_str}; } memset(str->buf, 0, str->capacity); @@ -82,17 +82,17 @@ String *wapp_dstr_update(String **dst, const char *src, Arena *arena) { strncpy(str->buf, src, length + 1); - return *dst; + return (StringUpdate){.updated = true, .str = *dst}; } -String *wapp_dstr_concat(String **dst, const char *src, Arena *arena) { +StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena) { if (!dst || !(*dst)) { - return *dst; + return (StringUpdate){.updated = false, .str = *dst}; } u64 src_length = strlen(src); if (src_length == 0) { - return *dst; + return (StringUpdate){.updated = false, .str = *dst}; } u64 new_length = (*dst)->size + src_length; diff --git a/src/strings/dstr/dstr.h b/src/strings/dstr/dstr.h index 35b9bd0..541c8c4 100644 --- a/src/strings/dstr/dstr.h +++ b/src/strings/dstr/dstr.h @@ -3,6 +3,7 @@ #include "aliases.h" #include "mem_arena.h" +#include #ifdef __cplusplus extern "C" { @@ -10,10 +11,16 @@ extern "C" { typedef struct dstr String; +typedef struct string_update StringUpdate; +struct string_update { + bool updated; + String *str; +}; + String *wapp_dstr_with_capacity(u64 capacity, Arena *arena); String *wapp_dstr_from_string(const char *str, Arena *arena); -String *wapp_dstr_update(String **dst, const char *src, Arena *arena); -String *wapp_dstr_concat(String **dst, const char *src, Arena *arena); +StringUpdate wapp_dstr_update(String **dst, const char *src, Arena *arena); +StringUpdate wapp_dstr_concat(String **dst, const char *src, Arena *arena); void wapp_dstr_clear(String *str); void wapp_dstr_print(const String *str); i64 wapp_dstr_find(const String *str, const char *substr);