diff options
author | WormHeamer | 2025-03-10 15:14:43 -0400 |
---|---|---|
committer | WormHeamer | 2025-03-10 15:14:43 -0400 |
commit | 1f36f42f01da3c8f5733f8eb54624fdc3e26843b (patch) | |
tree | 213539111b6bfe4126abb6f98dd166517ab16bf5 | |
parent | f30af4d2a2e313a53a447b6dd6918b43635caf3d (diff) |
add str_findc, str_find, and str_contains
-rw-r--r-- | str.h | 20 |
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 |