Add beginsWith helper to rdcinflexiblestr

This commit is contained in:
baldurk
2026-01-21 14:42:22 +00:00
parent 52c54ef476
commit 8f7208fa02
2 changed files with 42 additions and 0 deletions
+36
View File
@@ -1121,6 +1121,42 @@ public:
bool empty() const { return c_str()[0] == 0; }
const char *c_str() const { return (const char *)(uintptr_t)pointer; }
size_t size() const { return strlen(c_str()); }
bool beginsWith(const char *beginning) const
{
if(!beginning)
return false;
const char *a = c_str();
const char *b = beginning;
// empty string we're checking against, degenerate case but true
if(*b == 0)
return true;
// if we are an empty string we don't match any non-empty prefix
if(*a == 0)
return false;
while(*a && *b)
{
if(*a != *b)
return false;
a++;
b++;
// if we reached the end of beginning before hitting a difference, we do begin with it
if(*b == 0)
return true;
// if we reached the end of our own string but beginning still has something left, we don't begin with it
if(*a == 0)
return false;
}
// should never reach here
return false;
}
operator rdcstr() const
{
if(is_literal == 0)
+6
View File
@@ -2112,6 +2112,12 @@ TEST_CASE("Test string type", "[basictypes][string]")
CHECK_FALSE(test == rdcinflexiblestr("Hello, World!"));
CHECK_FALSE(test.empty());
CHECK(test.beginsWith("Hello"));
CHECK(test.beginsWith(""));
CHECK_FALSE(test.beginsWith(NULL));
CHECK_FALSE(test.beginsWith("Hello!"));
CHECK_FALSE(test.beginsWith("Hello, World!!"));
rdcstr str = test;
CHECK(str == "Hello, World");