From: wolf Date: Thu, 11 Aug 2005 15:23:15 +0000 (+0000) Subject: Use a general delimiter for splitting strings. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03eb1330ba802b70da7926ebefb033026d6e7e7d;p=dealii-svn.git Use a general delimiter for splitting strings. git-svn-id: https://svn.dealii.org/trunk@11287 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/utilities.h b/deal.II/base/include/base/utilities.h index fca71b4a05..713190d4a3 100644 --- a/deal.II/base/include/base/utilities.h +++ b/deal.II/base/include/base/utilities.h @@ -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 - split_comma_separated_list (const std::string &s); + split_string_list (const std::string &s, + const char delimiter = ','); /** * Generate a random number from a diff --git a/deal.II/base/source/utilities.cc b/deal.II/base/source/utilities.cc index fb63d13347..14edb24fbd 100644 --- a/deal.II/base/source/utilities.cc +++ b/deal.II/base/source/utilities.cc @@ -134,11 +134,12 @@ namespace Utilities std::vector - split_comma_separated_list (const std::string &s) + split_string_list (const std::string &s, + const char delimiter) { std::string tmp = s; std::vector 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 = ""; diff --git a/tests/base/utilities_01.cc b/tests/base/utilities_01.cc index 16bee34bf5..a51dd23346 100644 --- a/tests/base/utilities_01.cc +++ b/tests/base/utilities_01.cc @@ -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) << ' ';