Skip to content

Commit f44cac4

Browse files
committed
introduce dstring::starts_with(s)
This prepares for the transition to C++20, which adds std::string::starts_with(...). This will allow us to drop has_prefix(s, t).
1 parent 74c7764 commit f44cac4

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/util/dstring.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ class dstringt final
9090
return no==0; // string 0 is exactly the empty string
9191
}
9292

93+
/// equivalent of as_string().starts_with(s)
94+
bool starts_with(const char *s)
95+
{
96+
for(const char *t = c_str(); *s != 0; s++, t++)
97+
if(*t != *s)
98+
return false;
99+
100+
return true;
101+
}
102+
103+
/// equivalent of as_string().starts_with(s)
104+
bool starts_with(const std::string &prefix)
105+
{
106+
return as_string().compare(0, prefix.size(), prefix) == 0;
107+
}
108+
93109
char operator[](size_t i) const
94110
{
95111
return as_string()[i];

0 commit comments

Comments
 (0)