From 8c5cbf0a3c3eec140136ca5b81d491dab7d2d3c9 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Wed, 5 Feb 2025 18:57:19 -0500 Subject: add stdwrm.h; fix scatc segfault; slen/sfree string not string* --- dynarr.h | 2 ++ stdwrm.h | 8 ++++++++ str.h | 20 +++++++++++--------- 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 stdwrm.h diff --git a/dynarr.h b/dynarr.h index c9ce637..31b63a6 100644 --- a/dynarr.h +++ b/dynarr.h @@ -6,6 +6,8 @@ #include #include +#include "stdwrm.h" + typedef struct { size_t count, capacity; } DynArrHeader; #define DYNARR(type) type * diff --git a/stdwrm.h b/stdwrm.h new file mode 100644 index 0000000..0b44a2b --- /dev/null +++ b/stdwrm.h @@ -0,0 +1,8 @@ +#ifndef STDWRM_H +#define STDWRM_H + +#ifdef STDWRM_IMPL +#define STDWRM_STR_IMPL +#endif + +#endif diff --git a/str.h b/str.h index c0fc626..7959488 100644 --- a/str.h +++ b/str.h @@ -2,6 +2,8 @@ #define STR_H #include + +#include "stdwrm.h" #include "dynarr.h" typedef DYNARR(char) string; @@ -14,10 +16,10 @@ typedef struct { #define strv(s) (strv_t) { s, strlen(s) } string snew(void); -size_t slen(const string *); +size_t slen(const string); void scats(string *, strv_t); void scatc(string *, char); -void sfree(string *); +void sfree(string); #ifdef STDWRM_STR_IMPL @@ -28,28 +30,28 @@ string snew(void) { return s; } -size_t slen(const string *s) { - return DA_LEN(*s) - 1; +size_t slen(const string s) { + return DA_LEN(s) - 1; } void scatc(string *s, char c) { size_t n = DA_LEN(*s) + 1; DA_FIT(*s, n); - *s[n-2] = c; - *s[n-1] = '\0'; + (*s)[n-2] = c; + (*s)[n-1] = '\0'; DA_LEN(*s) = n; } void scats(string *s, strv_t sv) { size_t n = DA_LEN(*s) + sv.n; DA_FIT(*s, n); - memcpy(&(*s)[slen(s)], sv.s, sv.n); + memcpy(&(*s)[slen(*s)], sv.s, sv.n); (*s)[n-1] = '\0'; DA_LEN(*s) = n; } -void sfree(string *s) { - DA_FREE(*s); +void sfree(string s) { + DA_FREE(s); } #endif -- cgit 1.4.1-2-gfad0