diff options
author | WormHeamer | 2025-02-28 05:21:59 -0500 |
---|---|---|
committer | WormHeamer | 2025-02-28 05:21:59 -0500 |
commit | 22ee6c213cc0058ca833b88f67cb938c20bfc740 (patch) | |
tree | 088160f5120cbc83deeb3c60fe55e6665aaf0912 | |
parent | bd797a52219067db26bec4f6b1bd5f3ac4732e4f (diff) |
add sreplace
-rw-r--r-- | str.h | 11 |
1 files changed, 11 insertions, 0 deletions
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 |