From 25e4498e865857d8da8a9af057904f0af138ed1a Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Fri, 4 Feb 2022 20:06:47 -0500 Subject: [PATCH] add a utilCStrMatch helper this checks both the length of a cstr, and if the strings match. an improvement over checking with std::strncmp(), which could match two strings where one matched the prefix of the other --- src/util.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/util.h b/src/util.h index 9581f9b..18616dd 100644 --- a/src/util.h +++ b/src/util.h @@ -55,6 +55,12 @@ char* utilAllocateCStr(const char* str, u32 max_len = 256); void utilSafeFree(void* p); +//--------------- +// C string utils + +bool utilCStrMatch(const char* str1, const char* str2); + + #endif // UTIL_H @@ -170,4 +176,18 @@ utilSafeFree(void* p) LOGF(Error, "free called on nullptr\n"); } +//--------------- +// C string utils + +bool +utilCStrMatch(const char* str1, const char* str2) +{ + assert(str1 != nullptr && str2 != nullptr); + u32 l1 = strlen(str1); + u32 l2 = strlen(str2); + + return (l1 == l2) + && (strncmp(str1, str2, l1) == 0); +} + #endif