From 0496473cf5adce1afe751f26233c9c6e2d1c4582 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Thu, 22 Feb 2024 02:50:06 -0700 Subject: [PATCH] added find back cstring to ARC_String --- include/arc/std/string.h | 11 +++++++++++ src/std/string.c | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/arc/std/string.h b/include/arc/std/string.h index bc7b02d..be53e27 100644 --- a/include/arc/std/string.h +++ b/include/arc/std/string.h @@ -166,6 +166,17 @@ uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_ */ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring); +/** + * @brief takes a given string, and assigns index and length for position of last matching substring + * + * @param string the string that will be searched + * @param cstring the cstring to find within string + * @param length the length of cstring + * + * @return ~(uint64_t)0 on error, anything else on success + */ +uint64_t ARC_String_FindBackCString(ARC_String *string, const char *cstring, uint64_t length); + /** * @brief strips the ends based on a given char * diff --git a/src/std/string.c b/src/std/string.c index bee9680..9fe85a6 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -204,6 +204,27 @@ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring){ return ~(uint64_t)0; } +uint64_t ARC_String_FindBackCString(ARC_String *string, const char *cstring, uint64_t length){ + if(!string || !cstring){ + arc_errno = ARC_ERRNO_NULL; + ARC_DEBUG_ERR("ARC_String_FindBack(string, substring), string or substring was null"); + return ~(uint64_t)0; + } + + if(length > string->length){ + return ~(uint64_t)0; + } + + uint64_t max = string->length - (length - 1); + for(; max; max--){ + if(!strncmp(string->data + (max - 1), cstring, length)){ + return max; + } + } + + return ~(uint64_t)0; +} + void ARC_String_StripEnds(ARC_String **stripped, ARC_String *original, char charToStrip){ if(!original){ arc_errno = ARC_ERRNO_NULL;