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