From 16d2d8ebf62d2fa222811528a7fbbdd05accbc68 Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 25 Jan 2024 00:04:21 +0100 Subject: [PATCH] feat: added strcount --- core/src/strutil.c | 7 +++++++ core/src/strutil.h | 1 + 2 files changed, 8 insertions(+) diff --git a/core/src/strutil.c b/core/src/strutil.c index ef74370..ffc698b 100644 --- a/core/src/strutil.c +++ b/core/src/strutil.c @@ -30,6 +30,13 @@ long strfirst(const char* begin, const char* end, char search) { return -1; } +long strcount(const char* begin, const char* end, char search) { + long count = 0; + for(;begin < end && *begin != '\0'; ++begin) + if(*begin == search) ++count; + return count; +} + long strfirst_pred(const char* begin, const char* end, CharPredFn pred) { const char* itr = begin; while(itr != end) diff --git a/core/src/strutil.h b/core/src/strutil.h index 1c43021..ae8592a 100644 --- a/core/src/strutil.h +++ b/core/src/strutil.h @@ -10,6 +10,7 @@ extern uintptr_t strnhash(const char* s, size_t n); extern uintptr_t strhash(const char* s); extern long strlast(const char* rbegin, const char* rend, char search); extern long strfirst(const char* begin, const char* end, char search); +extern long strcount(const char* begin, const char* end, char search); extern long strfirst_pred(const char* begin, const char* end, CharPredFn pred); extern long strlast_pred(const char* rbegin, const char* rend, CharPredFn pred);