From 7bbcef95f3439e6b198eaa4ea804c9ab33ce9d30 Mon Sep 17 00:00:00 2001
From: Georgios Bitzes <georgios.bitzes@cern.ch>
Date: Fri, 21 Feb 2020 14:13:54 +0100
Subject: [PATCH] Implement StringUtils::endWith utility function

---
 src/utils/StringUtils.hh | 10 ++++++++++
 test/utils.cc            | 12 ++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/src/utils/StringUtils.hh b/src/utils/StringUtils.hh
index badcd8c8..70029396 100644
--- a/src/utils/StringUtils.hh
+++ b/src/utils/StringUtils.hh
@@ -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;
 
diff --git a/test/utils.cc b/test/utils.cc
index 2f3f61b9..f6bad1c7 100644
--- a/test/utils.cc
+++ b/test/utils.cc
@@ -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
-- 
GitLab