summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--str.h20
1 files changed, 20 insertions, 0 deletions
diff --git a/str.h b/str.h
index 22a0d31..3cc3f38 100644
--- a/str.h
+++ b/str.h
@@ -83,6 +83,8 @@ static inline Str str_trim(Str a) {
return str_trim_left(str_trim_right(a));
}
+/* splitting, searching */
+
typedef struct {
Str head, tail;
} Cut;
@@ -99,4 +101,22 @@ static inline Cut str_cut(Str s, char c) {
}
}
+static inline 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) {
+ if (needle.n < 1) return haystack;
+ while (haystack.n > 0) {
+ haystack = str_findc(haystack, needle.s[0]);
+ if (str_starts(haystack, needle)) break;
+ }
+ return haystack;
+}
+
+static inline int str_contains(Str a, Str b) {
+ return str_find(a, b).n > 0;
+}
+
#endif