Browse Source

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
main
cinnaboot 4 years ago
parent
commit
25e4498e86
  1. 20
      src/util.h

20
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

Loading…
Cancel
Save