From 7d8ba6fc28487901a6f73d75eb59dabe416fe391 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Tue, 1 Oct 2024 11:06:04 +0200 Subject: [PATCH] More functional-style stringview functions --- src/StringView/StringView.c | 28 ++++++++++++++++++++++++++++ src/StringView/StringView.h | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/src/StringView/StringView.c b/src/StringView/StringView.c index 95f2e88..aace3d5 100644 --- a/src/StringView/StringView.c +++ b/src/StringView/StringView.c @@ -119,6 +119,24 @@ StringView StringView_FindString(StringView haystack, StringView needle) return (StringView) {NULL, 0}; } +StringView StringView_Drop(StringView string, size_t length) +{ + return StringView_Slice(string, length, string.length); +} + +StringView StringView_TakeWhile(StringView string, StringViewContinue function) +{ + size_t length = 0; + for (length = 0; string.length != length && function(string.source[length]); length++); + + return StringView_Take(string, length); +} + +StringView StringView_Take(StringView string, size_t length) +{ + return StringView_Slice(string, 0, length); +} + StringView StringView_Slice(StringView string, size_t start, size_t end) { if (end > string.length || start > string.length) { @@ -200,6 +218,16 @@ StringView StringView_StripRight(StringView sv, StringView strip) return sv; } +void* StringView_FoldLeft(StringView string, void* initial, StringViewFoldFunction function) +{ + void* value = initial; + while (string.length != 0) { + value = function(string.source[0], value); + string = StringView_Drop(string, 1); + } + + return value; +} void StringView_Paste(char* destination, StringView source) { diff --git a/src/StringView/StringView.h b/src/StringView/StringView.h index 3c57de9..98e1f79 100644 --- a/src/StringView/StringView.h +++ b/src/StringView/StringView.h @@ -44,7 +44,12 @@ size_t StringView_Count(StringView string, StringView find); size_t StringView_FindStringOffset(StringView haystack, StringView needle); StringView StringView_FindString(StringView haystack, StringView needle); + +typedef bool (*StringViewContinue) (char); StringView StringView_Slice(StringView string, size_t start, size_t end); // start and end are offsets +StringView StringView_Drop(StringView string, size_t length); +StringView StringView_Take(StringView string, size_t length); +StringView StringView_TakeWhile(StringView string, StringViewContinue function); bool StringView_Partition(StringView* left, StringView* right, StringView source, StringView delim); bool StringView_NextSplit(StringView* dest, StringView* source, StringView delim); @@ -53,6 +58,9 @@ bool StringView_LastSplit(StringView* dest, StringView* source, StringView delim StringView StringView_StripLeft(StringView sv, StringView strip); StringView StringView_StripRight(StringView sv, StringView strip); +typedef void* (*StringViewFoldFunction) (char, void*); +void* StringView_FoldLeft(StringView string, void* initial, StringViewFoldFunction function); + void StringView_Paste(char* destination, StringView source); int StringView_ParseInt(StringView source);