diff options
author | WormHeamer | 2025-02-05 18:45:12 -0500 |
---|---|---|
committer | WormHeamer | 2025-02-05 18:45:12 -0500 |
commit | 18901338effd8179c08a8b927fde231502328509 (patch) | |
tree | 909b4f81b70c03deb32224c444ffba0a298333c3 | |
parent | 504d086994631a55083e05b28b904f9194b1ff8f (diff) |
add str.h
-rw-r--r-- | str.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/str.h b/str.h new file mode 100644 index 0000000..c0fc626 --- /dev/null +++ b/str.h @@ -0,0 +1,56 @@ +#ifndef STR_H +#define STR_H + +#include <string.h> +#include "dynarr.h" + +typedef DYNARR(char) string; + +typedef struct { + const char *s; + size_t n; +} strv_t; + +#define strv(s) (strv_t) { s, strlen(s) } + +string snew(void); +size_t slen(const string *); +void scats(string *, strv_t); +void scatc(string *, char); +void sfree(string *); + +#ifdef STDWRM_STR_IMPL + +string snew(void) { + string s; + DA_INIT(s); + DA_PUSH(s, '\0'); + return s; +} + +size_t slen(const string *s) { + return DA_LEN(*s) - 1; +} + +void scatc(string *s, char c) { + size_t n = DA_LEN(*s) + 1; + DA_FIT(*s, n); + *s[n-2] = c; + *s[n-1] = '\0'; + DA_LEN(*s) = n; +} + +void scats(string *s, strv_t sv) { + size_t n = DA_LEN(*s) + sv.n; + DA_FIT(*s, n); + memcpy(&(*s)[slen(s)], sv.s, sv.n); + (*s)[n-1] = '\0'; + DA_LEN(*s) = n; +} + +void sfree(string *s) { + DA_FREE(*s); +} + +#endif +#endif |