From bc2d3d1c59a521a6d0aea36a30f9e24b8288a1e5 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Thu, 27 Feb 2025 17:37:40 -0500 Subject: use fprintf() + abort() instead of err() --- dynarr.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dynarr.h b/dynarr.h index f8e5f20..fe3c84b 100644 --- a/dynarr.h +++ b/dynarr.h @@ -4,7 +4,6 @@ #include #include #include -#include #include "stdwrm.h" @@ -14,12 +13,13 @@ typedef struct { size_t count, capacity; } DynArrHeader; #define DA_HEADER(da) ((DynArrHeader*)(da) - 1) #define DA_INIT_CAP 16 -#define DA_INIT(da) do {\ - char *da_init_ptr = ((char*)malloc(sizeof(DynArrHeader) + DA_INIT_CAP * sizeof(*da)));\ - if (!da_init_ptr) err(1, "dynamic array allocation failed");\ +#define DA_INIT_SZ(da, cap) do {\ + char *da_init_ptr = ((char*)malloc(sizeof(DynArrHeader) + cap * sizeof(*da)));\ + if (!da_init_ptr) { fprintf(stderr, "dynamic array allocation failed\n"); abort(); }\ da = (void *)(da_init_ptr + sizeof(DynArrHeader));\ - *DA_HEADER(da) = (DynArrHeader) { 0, DA_INIT_CAP };\ + *DA_HEADER(da) = (DynArrHeader) { 0, cap };\ } while(0) +#define DA_INIT(da) DA_INIT_SZ(da, DA_INIT_CAP) #define DA_FREE(da)\ free(DA_HEADER(da)) @@ -29,7 +29,7 @@ typedef struct { size_t count, capacity; } DynArrHeader; if (count >= DA_HEADER(da)->capacity) {\ while (count >= DA_HEADER(da)->capacity) DA_HEADER(da)->capacity <<= 1;\ char *da_fit_ptr = realloc(DA_HEADER(da), sizeof(DynArrHeader) + DA_HEADER(da)->capacity * sizeof(*da));\ - if (!da_fit_ptr) err(1, "dynamic array reallocation failed");\ + if (!da_fit_ptr) { fprintf(stderr, "dynamic array reallocation failed\n"); abort(); }\ (da) = (void *)(da_fit_ptr + sizeof(DynArrHeader));\ }\ } while(0) -- cgit 1.4.1-2-gfad0 From bd797a52219067db26bec4f6b1bd5f3ac4732e4f Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Thu, 27 Feb 2025 17:38:02 -0500 Subject: add sfmt() and sdup() --- str.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/str.h b/str.h index 47ff676..3f2028d 100644 --- a/str.h +++ b/str.h @@ -14,15 +14,20 @@ typedef struct { } strv_t; #define strv(s) (strv_t) { s, strlen(s) } +#define strvs(s) (strv_t) { s, slen(s) } string snew(void); size_t slen(const string); void scats(string *, strv_t); void scatc(string *, char); void sfree(string); +string sfmt(const char *fmt, ...); +string sdup(const char *); #ifdef STDWRM_IMPL_STR +#include + string snew(void) { string s; DA_INIT(s); @@ -54,5 +59,27 @@ void sfree(string s) { DA_FREE(s); } +string sfmt(const char *fmt, ...) { + va_list count, print; + string s; + FILE *f = fopen("/dev/null", "w/o"); + va_start(print, fmt); + va_copy(count, print); + size_t n = vfprintf(f, fmt, count) + 1; + DA_INIT_SZ(s, n); + vsprintf(s, fmt, print); + va_end(print); + fclose(f); + return s; +} + +string sdup(const char *s) { + string d; + size_t n = strlen(s) + 1; + DA_INIT_SZ(d, n); + memcpy(d, s, n); + return d; +} + #endif #endif -- cgit 1.4.1-2-gfad0 From 22ee6c213cc0058ca833b88f67cb938c20bfc740 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Fri, 28 Feb 2025 05:21:59 -0500 Subject: add sreplace --- str.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/str.h b/str.h index 3f2028d..bafec19 100644 --- a/str.h +++ b/str.h @@ -21,6 +21,7 @@ size_t slen(const string); void scats(string *, strv_t); void scatc(string *, char); void sfree(string); +void sreplace(string *str, size_t i, size_t n, strv_t with); string sfmt(const char *fmt, ...); string sdup(const char *); @@ -81,5 +82,15 @@ string sdup(const char *s) { return d; } +void sreplace(string *str, size_t i, size_t n, strv_t with) { + string s = *str; + size_t new_n = DA_LEN(s) + with.n - n; + DA_FIT(s, new_n); + memmove(&s[i + with.n], &s[i + n], slen(s) - (i + n)); + memcpy(&s[i], with.s, with.n); + s[new_n - 1] = 0; + DA_LEN(s) = new_n; +} + #endif #endif -- cgit 1.4.1-2-gfad0