changeset 5373:123a94dd57df

Toolbox::SplitString
author Alain Mazy <am@osimis.io>
date Thu, 17 Aug 2023 17:25:08 +0200
parents 94a52168480b
children b216b57bbe6a
files OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/FrameworkTests.cpp
diffstat 3 files changed, 125 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Toolbox.cpp	Thu Aug 17 11:25:59 2023 +0200
+++ b/OrthancFramework/Sources/Toolbox.cpp	Thu Aug 17 17:25:08 2023 +0200
@@ -1035,10 +1035,10 @@
     return result;
   }
 
-
-  void Toolbox::TokenizeString(std::vector<std::string>& result,
+  static void TokenizeStringInternal(std::vector<std::string>& result,
                                const std::string& value,
-                               char separator)
+                               char separator,
+                               bool includeEmptyStrings)
   {
     size_t countSeparators = 0;
     
@@ -1068,7 +1068,41 @@
       }
     }
 
-    result.push_back(currentItem);
+    if (includeEmptyStrings || !currentItem.empty())
+    {
+      result.push_back(currentItem);
+    }
+  }
+
+
+  void Toolbox::TokenizeString(std::vector<std::string>& result,
+                               const std::string& value,
+                               char separator)
+  {
+    TokenizeStringInternal(result, value, separator, true);
+  }
+
+
+  void Toolbox::SplitString(std::set<std::string>& result,
+                            const std::string& value,
+                            char separator)
+  {
+    result.clear();
+
+    std::vector<std::string> temp;
+    TokenizeStringInternal(temp, value, separator, false);
+    for (size_t i = 0; i < temp.size(); ++i)
+    {
+      result.insert(temp[i]);
+    }
+  }
+
+
+  void Toolbox::SplitString(std::vector<std::string>& result,
+                            const std::string& value,
+                            char separator)
+  {
+    TokenizeStringInternal(result, value, separator, false);
   }
 
 
--- a/OrthancFramework/Sources/Toolbox.h	Thu Aug 17 11:25:59 2023 +0200
+++ b/OrthancFramework/Sources/Toolbox.h	Thu Aug 17 17:25:08 2023 +0200
@@ -183,10 +183,21 @@
 
     static std::string WildcardToRegularExpression(const std::string& s);
 
+    // TokenizeString result might contain empty strings (not SplitString)
     static void TokenizeString(std::vector<std::string>& result,
                                const std::string& source,
                                char separator);
 
+    // SplitString result won't contain empty strings (compared to TokenizeString)
+    static void SplitString(std::vector<std::string>& result,
+                            const std::string& source,
+                            char separator);
+
+    // SplitString result won't contain empty strings (compared to TokenizeString)
+    static void SplitString(std::set<std::string>& result,
+                            const std::string& source,
+                            char separator);
+
     static void JoinStrings(std::string& result,
                             const std::set<std::string>& source,
                             const char* separator);
--- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Thu Aug 17 11:25:59 2023 +0200
+++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Thu Aug 17 17:25:08 2023 +0200
@@ -706,6 +706,82 @@
   ASSERT_EQ("", t[3]);
 }
 
+TEST(Toolbox, SplitString)
+{
+  {
+    std::set<std::string> result;
+    Toolbox::SplitString(result, "", ';');
+    ASSERT_EQ(0u, result.size());
+  }
+
+  {
+    std::set<std::string> result;
+    Toolbox::SplitString(result, "a", ';');
+    ASSERT_EQ(1u, result.size());
+    ASSERT_TRUE(result.end() != result.find("a"));
+  }
+
+  {
+    std::set<std::string> result;
+    Toolbox::SplitString(result, "a;b", ';');
+    ASSERT_EQ(2u, result.size());
+    ASSERT_TRUE(result.end() != result.find("a"));
+    ASSERT_TRUE(result.end() != result.find("b"));
+  }
+
+  {
+    std::set<std::string> result;
+    Toolbox::SplitString(result, "a;b;", ';');
+    ASSERT_EQ(2u, result.size());
+    ASSERT_TRUE(result.end() != result.find("a"));
+    ASSERT_TRUE(result.end() != result.find("b"));
+  }
+
+  {
+    std::set<std::string> result;
+    Toolbox::SplitString(result, "a;a", ';');
+    ASSERT_EQ(1u, result.size());
+    ASSERT_TRUE(result.end() != result.find("a"));
+  }
+
+  {
+    std::vector<std::string> result;
+    Toolbox::SplitString(result, "", ';');
+    ASSERT_EQ(0u, result.size());
+  }
+
+  {
+    std::vector<std::string> result;
+    Toolbox::SplitString(result, "a", ';');
+    ASSERT_EQ(1u, result.size());
+    ASSERT_EQ("a", result[0]);
+  }
+
+  {
+    std::vector<std::string> result;
+    Toolbox::SplitString(result, "a;b", ';');
+    ASSERT_EQ(2u, result.size());
+    ASSERT_EQ("a", result[0]);
+    ASSERT_EQ("b", result[1]);
+  }
+
+  {
+    std::vector<std::string> result;
+    Toolbox::SplitString(result, "a;b;", ';');
+    ASSERT_EQ(2u, result.size());
+    ASSERT_EQ("a", result[0]);
+    ASSERT_EQ("b", result[1]);
+  }
+
+  {
+    std::vector<std::string> result;
+    Toolbox::TokenizeString(result, "a;a", ';');
+    ASSERT_EQ(2u, result.size());
+    ASSERT_EQ("a", result[0]);
+    ASSERT_EQ("a", result[1]);
+  }
+}
+
 TEST(Toolbox, Enumerations)
 {
   ASSERT_EQ(Encoding_Utf8, StringToEncoding(EnumerationToString(Encoding_Utf8)));