From 1f36f42f01da3c8f5733f8eb54624fdc3e26843b Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Mon, 10 Mar 2025 15:14:43 -0400 Subject: add str_findc, str_find, and str_contains --- str.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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 -- cgit 1.4.1-2-gfad0