diff options
author | WormHeamer | 2025-02-28 14:54:00 -0500 |
---|---|---|
committer | WormHeamer | 2025-02-28 14:54:00 -0500 |
commit | 93608e2f90689c0eddd4714debcab9892f1dc7d7 (patch) | |
tree | dc82450cb45ea5df205257e84ca299cd0fc95077 /str.h | |
parent | 528460889a2ea771e5ee81d6573a0e17d584ab6e (diff) | |
parent | 22ee6c213cc0058ca833b88f67cb938c20bfc740 (diff) |
Merge remote-tracking branch 'refs/remotes/origin/master'
Diffstat (limited to 'str.h')
-rw-r--r-- | str.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/str.h b/str.h index 47ff676..bafec19 100644 --- a/str.h +++ b/str.h @@ -14,15 +14,21 @@ 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); +void sreplace(string *str, size_t i, size_t n, strv_t with); +string sfmt(const char *fmt, ...); +string sdup(const char *); #ifdef STDWRM_IMPL_STR +#include <stdarg.h> + string snew(void) { string s; DA_INIT(s); @@ -54,5 +60,37 @@ 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; +} + +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 |