From 4d1abb6d350a3d898a5aedfcb912bc28eef46d45 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 8 Sep 2024 17:55:18 +0200 Subject: Start working on custom formatting --- src/util.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index 044ce70..0a2ade8 100644 --- a/src/util.c +++ b/src/util.c @@ -108,7 +108,45 @@ char *strcopy(const char *string) return strslice(string, 0, strlen(string)); } -void unreachable(void) +bool strfind(const char *string, const char *cases[]) { - log_panic("Unreachable"); + for (int i = 0; cases[i] != NULL; i++) { + if (strstr(string, cases[i]) != NULL) + return true; + } + return false; +} + +size_t strprefix(const char *string, const char *prefix) +{ + size_t i = 0; + for ( ; prefix[i] != '\0'; i++) { + if (string[i] != prefix[i]) + return 0; + } + return i; +} + +size_t strcount(const char *string, const char *subs[]) +{ + size_t count = 0; + for (int i = 0; string[i] != '\0'; ) { +next: + for (int j = 0; subs[j] != NULL; j++) { + size_t pl = strprefix(string + i, subs[j]); + if (pl) { + i += pl; + count++; + goto next; + } + } + ++i; + } + return count; +} + +char *strformat(const char *string, const char *keys[], const char *values[]) +{ + // TODO + return NULL; } -- cgit v1.2.3