diff options
author | WormHeamer | 2025-02-27 17:38:02 -0500 |
---|---|---|
committer | WormHeamer | 2025-02-27 17:38:02 -0500 |
commit | bd797a52219067db26bec4f6b1bd5f3ac4732e4f (patch) | |
tree | f6aa86cadb751161208df15232484d5f04675032 | |
parent | bc2d3d1c59a521a6d0aea36a30f9e24b8288a1e5 (diff) |
add sfmt() and sdup()
-rw-r--r-- | str.h | 27 |
1 files changed, 27 insertions, 0 deletions
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 <stdarg.h> + 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 |