]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Use a general delimiter for splitting strings.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 11 Aug 2005 15:23:15 +0000 (15:23 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 11 Aug 2005 15:23:15 +0000 (15:23 +0000)
git-svn-id: https://svn.dealii.org/trunk@11287 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/utilities.h
deal.II/base/source/utilities.cc
tests/base/utilities_01.cc

index fca71b4a05d018bfc413d7f5f4bef62de19b9400..713190d4a3ab17f2b520c775495cd45df7219053 100644 (file)
@@ -66,12 +66,17 @@ namespace Utilities
 
                                    /**
                                     * Given a string that contains text
-                                    * separated by commas, split it into its
-                                    * components; for each component, remove
-                                    * leading and trailing spaces.
+                                    * separated by a #delimiter, split it into
+                                    * its components; for each component,
+                                    * remove leading and trailing spaces.
+                                    *
+                                    * The default value of the delimiter is a
+                                    * comma, so that the function splits comma
+                                    * separated lists of strings.
                                     */
   std::vector<std::string>
-  split_comma_separated_list (const std::string &s);
+  split_string_list (const std::string &s,
+                     const char         delimiter = ',');
 
                                    /**
                                     * Generate a random number from a
index fb63d133475c9313c7ff5d2ddc22ac653b190499..14edb24fbd7869d1a2d86d695cae223c48cdde9f 100644 (file)
@@ -134,11 +134,12 @@ namespace Utilities
 
 
   std::vector<std::string>
-  split_comma_separated_list (const std::string &s)
+  split_string_list (const std::string &s,
+                     const char         delimiter)
   {
     std::string tmp = s;
     std::vector<std::string> split_list;
-    split_list.reserve (std::count (tmp.begin(), tmp.end(), ',')+1);
+    split_list.reserve (std::count (tmp.begin(), tmp.end(), delimiter)+1);
 
                                     // split the input list
     while (tmp.length() != 0)
@@ -146,10 +147,10 @@ namespace Utilities
         std::string name;
        name = tmp;
 
-       if (name.find(",") != std::string::npos)
+       if (name.find(delimiter) != std::string::npos)
          {
-           name.erase (name.find(","), std::string::npos);
-           tmp.erase (0, tmp.find(",")+1);
+           name.erase (name.find(delimiter), std::string::npos);
+           tmp.erase (0, tmp.find(delimiter)+1);
          }
        else
          tmp = "";
index 16bee34bf5baaeb74585ebc8aaae1930ebb24acb..a51dd23346c371b5e0d6a2d15a14a8a410c8a97f 100644 (file)
@@ -37,15 +37,29 @@ void test ()
   deallog << Utilities::string_to_int (v)[1] << std::endl;
   deallog << Utilities::string_to_int (v)[2] << std::endl;
 
-  const char *p = "alpha, beta, gamma ";
-  Assert (Utilities::split_comma_separated_list (p).size() == 3,
-          ExcInternalError());
-  Assert (Utilities::split_comma_separated_list (p)[0] == "alpha",
-          ExcInternalError());  
-  Assert (Utilities::split_comma_separated_list (p)[1] == "beta",
-          ExcInternalError());
-  Assert (Utilities::split_comma_separated_list (p)[2] == "gamma",
-          ExcInternalError());
+  {
+    const char *p = "alpha, beta, gamma ";
+    Assert (Utilities::split_string_list (p).size() == 3,
+            ExcInternalError());
+    Assert (Utilities::split_string_list (p)[0] == "alpha",
+            ExcInternalError());  
+    Assert (Utilities::split_string_list (p)[1] == "beta",
+            ExcInternalError());
+    Assert (Utilities::split_string_list (p)[2] == "gamma",
+            ExcInternalError());
+  }
+  
+  {
+    const char *p = "alpha; beta; gamma ";
+    Assert (Utilities::split_string_list (p, ';').size() == 3,
+            ExcInternalError());
+    Assert (Utilities::split_string_list (p, ';')[0] == "alpha",
+            ExcInternalError());  
+    Assert (Utilities::split_string_list (p, ';')[1] == "beta",
+            ExcInternalError());
+    Assert (Utilities::split_string_list (p, ';')[2] == "gamma",
+            ExcInternalError());
+  }
 
   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.