summaryrefslogtreecommitdiff
path: root/str.h
diff options
context:
space:
mode:
Diffstat (limited to 'str.h')
-rw-r--r--str.h45
1 files changed, 27 insertions, 18 deletions
diff --git a/str.h b/str.h
index b5f2889..1969d6b 100644
--- a/str.h
+++ b/str.h
@@ -17,36 +17,34 @@ typedef struct {
#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, ptrdiff_t n);
+Str str_trunc(Str a, ptrdiff_t n);
+
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);
+
+char *str_to_cstr(Str s, Arena *a);
void str_cat(Str *a, Str b, Arena *m);
+void str_catc(Str *a, char b, Arena *m);
+Str str_dup(Str a, Arena *m);
Str str_replace_end(Str s, Str a, Str b, Arena *m);
#ifdef STR_IMPL
/* conversions */
-char *str_to_cstr(Str s, Arena *a) {
- if (!s.n) return "";
- char *r = new_arr(a, char, s.n + 1);
- memcpy(r, s.s, s.n);
- r[s.n] = 0;
- return r;
-}
-
Str str_from_cstr(const char *s) {
return (Str) { (char*)s, strlen(s) };
}
@@ -65,15 +63,14 @@ int str_ends(Str a, Str b) {
return a.n >= b.n && !memcmp(&a.s[a.n - b.n], b.s, b.n);
}
-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;
-}
-
Str str_skip(Str a, ptrdiff_t n) {
return (Str) { a.s + n, a.n - n };
}
+Str str_trunc(Str a, ptrdiff_t n) {
+ return (Str) { a.s, n < a.n ? n : a.n };
+}
+
static inline int str_is_space(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
@@ -127,6 +124,14 @@ int str_contains(Str a, Str b) {
/* allocating */
+char *str_to_cstr(Str s, Arena *a) {
+ if (!s.n) return "";
+ char *r = new_arr(a, char, s.n + 1);
+ memcpy(r, s.s, s.n);
+ r[s.n] = 0;
+ return r;
+}
+
Str str_dup(Str a, Arena *m) {
char *s = new_arr(m, char, a.n);
memcpy(s, a.s, a.n);
@@ -135,11 +140,15 @@ Str str_dup(Str a, 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->s = arena_concat(m, a->s, a->n, b.s, b.n, _Alignof(char));
a->n += b.n;
}
+void str_catc(Str *a, char b, Arena *m) {
+ a->s = arena_concat(m, a->s, a->n, &b, 1, _Alignof(char));
+ a->n++;
+}
+
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);