From 062b3276b1583ffdf32129634a984dce2ce28866 Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Wed, 14 Jun 2017 15:50:05 +0200 Subject: [PATCH] Utilities::split_string_list take a string instead of char --- include/deal.II/base/utilities.h | 13 ++++++++++++- source/base/utilities.cc | 14 +++++++++++--- tests/base/utilities_01.cc | 12 ++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 4ea4ace924..867a75f78a 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -132,6 +132,7 @@ namespace Utilities std::vector string_to_double (const std::vector &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 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 + split_string_list (const std::string &s, + const char delimiter); + /** * Take a text, usually a documentation or something, and try to break it diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 6b4c9ff6c8..b4dfb30990 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -274,7 +274,7 @@ namespace Utilities std::vector 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 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 + split_string_list (const std::string &s, + const char delimiter) + { + std::string d = ","; + d[0] = delimiter; + return split_string_list(s,d); + } + std::vector break_text_into_lines (const std::string &original_text, diff --git a/tests/base/utilities_01.cc b/tests/base/utilities_01.cc index 4f68b3afba..bf64b683d1 100644 --- a/tests/base/utilities_01.cc +++ b/tests/base/utilities_01.cc @@ -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) << ' '; -- 2.39.5