Commit 7bbcef95 authored by Georgios Bitzes's avatar Georgios Bitzes
Browse files

Implement StringUtils::endWith utility function

parent c3a7083e
Pipeline #1432317 passed with stages
in 63 minutes and 39 seconds
......@@ -52,6 +52,16 @@ inline bool startsWith(std::string_view str, std::string_view prefix) {
return true;
}
inline bool endsWith(std::string_view str, std::string_view suffix) {
if(suffix.size() > str.size()) return false;
for(size_t i = 0; i < suffix.size(); i++) {
if(str[str.size()-suffix.size()+i] != suffix[i]) return false;
}
return true;
}
inline bool isPrefix(const std::string &prefix, const char *buff, size_t n) {
if(n < prefix.size()) return false;
......
......@@ -1103,4 +1103,16 @@ TEST(Synchronized, String) {
ASSERT_EQ(syncstr.get(), "");
syncstr.set("test");
ASSERT_EQ(syncstr.get(), "test");
}
TEST(StringUtils, endsWith) {
ASSERT_TRUE(StringUtils::endsWith("some-string-123", "3"));
ASSERT_TRUE(StringUtils::endsWith("some-string-123", "123"));
ASSERT_TRUE(StringUtils::endsWith("some-string-123", "-123"));
ASSERT_TRUE(StringUtils::endsWith("some-string-123", "string-123"));
ASSERT_FALSE(StringUtils::endsWith("some-string-123", "4"));
ASSERT_FALSE(StringUtils::endsWith("some-string-123", "124"));
ASSERT_FALSE(StringUtils::endsWith("some-string-123", "-124"));
ASSERT_FALSE(StringUtils::endsWith("some-string-123", "strin4-123"));
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment