From 4d7ba1cf4e1933e224d24a573cba6dbf80885eac Mon Sep 17 00:00:00 2001 From: herbglitch Date: Wed, 12 Jun 2024 02:54:54 -0600 Subject: [PATCH] added more cstring find and equals functions --- include/arc/std/string.h | 40 ++++++++++++++++++++++++++++++++++++++-- src/std/string.c | 16 +++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/include/arc/std/string.h b/include/arc/std/string.h index be53e27..9cee4b6 100644 --- a/include/arc/std/string.h +++ b/include/arc/std/string.h @@ -87,6 +87,18 @@ uint8_t ARC_String_Equals(ARC_String *first, ARC_String *second); */ uint8_t ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64_t length); +/** + * @brief check if ARC_String and cstring match + * + * @note will use strlen to get the length of the cstring + * + * @param string ARC_string to check + * @param cstring cstring to check + * + * @return 1 if match, 0 if they don't match +*/ +uint8_t ARC_String_EqualsCStringWithStrlen(ARC_String *string, const char *cstring); + /** * @brief check if ARC_String and cstring match * @@ -156,6 +168,18 @@ uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring); */ uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_t length); +/** + * @brief takes given cstring and gives position of first matching + * + * @note will use strlen to get the length of the cstring + * + * @param string the string that will be searched + * @param cstring the cstring to find within string + * + * @return ~(uint64_t)0 on error, anything else on success +*/ +uint64_t ARC_String_FindCStringWithStrlen(ARC_String *string, const char *cstring); + /** * @brief takes a given string, and assigns index and length for position of last matching substring * @@ -167,9 +191,9 @@ 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 + * @brief takes a given cstring and give position of last matching * - * @param string the string that will be searched + * @param string the string that will be searched * @param cstring the cstring to find within string * @param length the length of cstring * @@ -177,6 +201,18 @@ uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring); */ uint64_t ARC_String_FindBackCString(ARC_String *string, const char *cstring, uint64_t length); +/** + * @brief takes a given cstring and give position of last matching + * + * @note will use strlen to get the length of the cstring + * + * @param string the string that will be searched + * @param cstring the cstring to find within string + * + * @return ~(uint64_t)0 on error, anything else on success + */ +uint64_t ARC_String_FindBackCStringWithStrlen(ARC_String *string, const char *cstring); + /** * @brief strips the ends based on a given char * diff --git a/src/std/string.c b/src/std/string.c index 9fe85a6..243d30d 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -24,7 +24,9 @@ void ARC_String_CreateWithStrlen(ARC_String **string, char *data){ } void ARC_String_Destroy(ARC_String *string){ - free(string->data); + if(string->data){ + free(string->data); + } free(string); } @@ -100,6 +102,10 @@ uint8_t ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64 return 1; } +uint8_t ARC_String_EqualsCStringWithStrlen(ARC_String *string, const char *cstring){ + return ARC_String_EqualsCString(string, cstring, strlen(cstring)); +} + uint8_t ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, const char *cstring, uint64_t length){ if(string->length - offset < length){ return 0; @@ -183,6 +189,10 @@ uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_ return ~(uint64_t)0; } +uint64_t ARC_String_FindCStringWithStrlen(ARC_String *string, const char *cstring){ + return ARC_String_FindCString(string, cstring, strlen(cstring)); +} + uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring){ if(!string || !substring){ arc_errno = ARC_ERRNO_NULL; @@ -225,6 +235,10 @@ uint64_t ARC_String_FindBackCString(ARC_String *string, const char *cstring, uin return ~(uint64_t)0; } +uint64_t ARC_String_FindBackCStringWithStrlen(ARC_String *string, const char *cstring){ + return ARC_String_FindBackCString(string, cstring, strlen(cstring)); +} + void ARC_String_StripEnds(ARC_String **stripped, ARC_String *original, char charToStrip){ if(!original){ arc_errno = ARC_ERRNO_NULL;