]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Utilities::split_string_list take a string instead of char
authorLuca Heltai <luca.heltai@sissa.it>
Wed, 14 Jun 2017 13:50:05 +0000 (15:50 +0200)
committerLuca Heltai <luca.heltai@sissa.it>
Wed, 14 Jun 2017 13:50:05 +0000 (15:50 +0200)
include/deal.II/base/utilities.h
source/base/utilities.cc
tests/base/utilities_01.cc

index 4ea4ace924a1ab5652c3a0097d301a1ff66f2efb..867a75f78a29a34831641cb5aea9f2dccfa988f7 100644 (file)
@@ -132,6 +132,7 @@ namespace Utilities
   std::vector<double>
   string_to_double (const std::vector<std::string> &s);
 
+
   /**
    * Given a string that contains text separated by a @p delimiter, split it
    * into its components; for each component, remove leading and trailing
@@ -176,7 +177,17 @@ namespace Utilities
    */
   std::vector<std::string>
   split_string_list (const std::string &s,
-                     const char         delimiter = ',');
+                     const std::string &delimiter = ",");
+
+
+  /**
+   * Specializatin of split_string_list() for the case where the delimiter
+   * is a single char.
+   */
+  std::vector<std::string>
+  split_string_list (const std::string &s,
+                     const char delimiter);
+
 
   /**
    * Take a text, usually a documentation or something, and try to break it
index 6b4c9ff6c8ab2264472016873c0a5b78e5f18de2..b4dfb3099064bf8f040147a0ea6a3695165d70e3 100644 (file)
@@ -274,7 +274,7 @@ namespace Utilities
 
   std::vector<std::string>
   split_string_list (const std::string &s,
-                     const char         delimiter)
+                     const std::string &delimiter)
   {
     // keep the currently remaining part of the input string in 'tmp' and
     // keep chopping elements of the list off the front
@@ -292,7 +292,6 @@ namespace Utilities
     // there was space after the last delimiter. this matches what's
     // discussed in the documentation
     std::vector<std::string> split_list;
-    split_list.reserve (std::count (tmp.begin(), tmp.end(), delimiter)+1);
     while (tmp.length() != 0)
       {
         std::string name;
@@ -301,7 +300,7 @@ namespace Utilities
         if (name.find(delimiter) != std::string::npos)
           {
             name.erase (name.find(delimiter), std::string::npos);
-            tmp.erase (0, tmp.find(delimiter)+1);
+            tmp.erase (0, tmp.find(delimiter)+delimiter.size());
           }
         else
           tmp = "";
@@ -319,6 +318,15 @@ namespace Utilities
   }
 
 
+  std::vector<std::string>
+  split_string_list (const std::string &s,
+                     const char delimiter)
+  {
+    std::string d = ",";
+    d[0] = delimiter;
+    return split_string_list(s,d);
+  }
+
 
   std::vector<std::string>
   break_text_into_lines (const std::string &original_text,
index 4f68b3afba8a440c0bd24daad418409b730e405d..bf64b683d11922d607ccbcf4698ce14c7287e234 100644 (file)
@@ -65,6 +65,18 @@ void test ()
                  ExcInternalError());
   }
 
+  {
+    const char *p = "alpha;; beta;; gamma ";
+    AssertThrow (Utilities::split_string_list (p, ";;").size() == 3,
+                 ExcInternalError());
+    AssertThrow (Utilities::split_string_list (p, ";;")[0] == "alpha",
+                 ExcInternalError());
+    AssertThrow (Utilities::split_string_list (p, ";;")[1] == "beta",
+                 ExcInternalError());
+    AssertThrow (Utilities::split_string_list (p, ";;")[2] == "gamma",
+                 ExcInternalError());
+  }
+
   deallog << Utilities::generate_normal_random_number (13, 44) << ' ';
   deallog << Utilities::generate_normal_random_number (13, 44) << ' ';
   deallog << Utilities::generate_normal_random_number (13, 44) << ' ';

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.