From 03eb1330ba802b70da7926ebefb033026d6e7e7d Mon Sep 17 00:00:00 2001 From: wolf Date: Thu, 11 Aug 2005 15:23:15 +0000 Subject: [PATCH] Use a general delimiter for splitting strings. git-svn-id: https://svn.dealii.org/trunk@11287 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/base/include/base/utilities.h | 13 +++++++---- deal.II/base/source/utilities.cc | 11 ++++----- tests/base/utilities_01.cc | 32 +++++++++++++++++++-------- 3 files changed, 38 insertions(+), 18 deletions(-) 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) << ' '; -- 2.39.5