diff options
author | WormHeamer | 2025-03-10 15:19:34 -0400 |
---|---|---|
committer | WormHeamer | 2025-03-10 15:19:34 -0400 |
commit | ec6c4cdbc7208b3e0d72278615205aee5068350a (patch) | |
tree | 4b2dcc57fe82a46167d1f2d15b68d4b9b90d6ef1 | |
parent | ad8e877bf9145de708f12d614f3c727867ee62a3 (diff) |
add STR_IMPL
-rw-r--r-- | str.h | 66 |
1 files changed, 44 insertions, 22 deletions
diff --git a/str.h b/str.h index 9b1aafc..f29cd41 100644 --- a/str.h +++ b/str.h @@ -12,69 +12,90 @@ typedef struct { isize n; } Str; +typedef struct { + Str head, tail; +} Cut; + #define S(s) (Str){s,sizeof(s)-1} +char *str_to_cstr(Str s, Arena *a); +Str str_from_cstr(const char *s); +int str_eql(Str a, Str b); +int str_starts(Str a, Str b); +int str_ends(Str a, Str b); +void str_catc(Str *a, char b, Arena *m); +Str str_skip(Str a, isize n); +int is_space(char c); +Str str_trim_left(Str a); +Str str_trim_right(Str a); +Str str_trim(Str a); +Cut str_cut(Str s, char c); +Str str_findc(Str s, char c); +Str str_find(Str haystack, Str needle); +int str_contains(Str a, Str b); +Str str_dup(Str a, Arena *m); +void str_cat(Str *a, Str b, Arena *m); +Str str_replace_end(Str s, Str a, Str b, Arena *m); + +#ifdef STR_IMPL + /* conversions */ -static inline char *str_to_cstr(Str s, Arena *a) { +char *str_to_cstr(Str s, Arena *a) { char *r = new_arr(a, char, s.n + 1); memcpy(r, s.s, s.n); r[s.n] = 0; return r; } -static inline Str str_from_cstr(const char *s) { +Str str_from_cstr(const char *s) { return (Str) { (char*)s, strlen(s) }; } /* pure functions */ -static inline int str_eql(Str a, Str b) { +int str_eql(Str a, Str b) { return a.n == b.n && !memcmp(a.s, b.s, b.n); } -static inline int str_starts(Str a, Str b) { +int str_starts(Str a, Str b) { return a.n >= b.n && !memcmp(a.s, b.s, b.n); } -static inline int str_ends(Str a, Str b) { +int str_ends(Str a, Str b) { return a.n >= b.n && !memcmp(&a.s[a.n - b.n], b.s, b.n); } -static inline void str_catc(Str *a, char b, Arena *m) { +void str_catc(Str *a, char b, Arena *m) { a->s = resize(m, a->s, a->n, a->n + 1); a->s[a->n++] = b; } -static inline Str str_skip(Str a, isize n) { +Str str_skip(Str a, isize n) { return (Str) { a.s + n, a.n - n }; } -static inline int is_space(char c) { +int is_space(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } -static inline Str str_trim_left(Str a) { +Str str_trim_left(Str a) { while (a.n > 0 && is_space(a.s[0])) a.s++, a.n--; return a; } -static inline Str str_trim_right(Str a) { +Str str_trim_right(Str a) { while (a.n > 0 && is_space(a.s[a.n - 1])) a.n--; return a; } -static inline Str str_trim(Str a) { +Str str_trim(Str a) { return str_trim_left(str_trim_right(a)); } /* splitting, searching */ -typedef struct { - Str head, tail; -} Cut; - -static inline Cut str_cut(Str s, char c) { +Cut str_cut(Str s, char c) { char *p = memchr(s.s, c, s.n); if (!p) { return (Cut) { s, { &s.s[s.n], 0 } }; @@ -86,12 +107,12 @@ static inline Cut str_cut(Str s, char c) { } } -static inline Str str_findc(Str s, char c) { +Str str_findc(Str s, char c) { char *p = memchr(s.s, c, s.n); return p ? (Str) { p, s.n - (p - s.s) } : (Str) { &s.s[s.n], 0 }; } -static inline Str str_find(Str haystack, Str needle) { +Str str_find(Str haystack, Str needle) { if (needle.n < 1) return haystack; while (haystack.n > 0) { haystack = str_findc(haystack, needle.s[0]); @@ -100,26 +121,26 @@ static inline Str str_find(Str haystack, Str needle) { return haystack; } -static inline int str_contains(Str a, Str b) { +int str_contains(Str a, Str b) { return str_find(a, b).n > 0; } /* allocating */ -static inline Str str_dup(Str a, Arena *m) { +Str str_dup(Str a, Arena *m) { char *s = new_arr(m, char, a.n); memcpy(s, a.s, a.n); a.s = s; return a; } -static inline void str_cat(Str *a, Str b, Arena *m) { +void str_cat(Str *a, Str b, Arena *m) { a->s = resize(m, a->s, a->n, a->n + b.n); memcpy(&a->s[a->n], b.s, b.n); a->n += b.n; } -static inline Str str_replace_end(Str s, Str a, Str b, Arena *m) { +Str str_replace_end(Str s, Str a, Str b, Arena *m) { if (!str_ends(s, a)) return s; char *p = new_arr(m, char, s.n + b.n - a.n); memcpy(p, s.s, s.n - a.n); @@ -128,3 +149,4 @@ static inline Str str_replace_end(Str s, Str a, Str b, Arena *m) { } #endif +#endif |