From: David Wells Date: Thu, 3 Dec 2015 03:53:08 +0000 (-0500) Subject: Modify Utilities::trim to remove all whitespace. X-Git-Tag: v8.4.0-rc2~179^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84054894a9e53786e595ad0895256946f19ef7ab;p=dealii.git Modify Utilities::trim to remove all whitespace. This fixes issue #1691, where a ParameterHandler could not read files with DOS line endings because Utilities::trim did not discard the '\r' characters. --- diff --git a/doc/news/changes.h b/doc/news/changes.h index e8afbbf7ea..bb86ebe937 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -38,6 +38,13 @@ inconvenience this causes.

    +
  1. Changed: The function Utilities::trim now removes general + white space characters, such as '\\r' and '\\n', as well as + space characters. +
    + (David Wells, 2015/12/05) +
  2. +
  3. Removed: Previously deprecated global instance multithread_info of MultithreadInfo has been removed (all members of this class are static so there is no reason to use/create an instance). The deprecated diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 582eb6e0dc..1ac3b2ec70 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -199,9 +199,12 @@ namespace Utilities const std::string &to); /** - * Return a string with all spaces at the beginning and end of @p input removed. + * Return a string with all standard whitespace characters (including + * '\\t', '\\n', and '\\r') at the beginning and + * end of @p input removed. */ - std::string trim(const std::string &input); + std::string + trim(const std::string &input); /** * Generate a random number from a normalized Gaussian probability diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 4c2961b94a..9de4a062c6 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -25,6 +25,7 @@ DEAL_II_DISABLE_EXTRA_DIAGNOSTICS DEAL_II_ENABLE_EXTRA_DIAGNOSTICS #include +#include #include #include #include @@ -115,14 +116,30 @@ namespace Utilities std::string trim(const std::string &input) { - std::string::size_type start_idx = input.find_first_not_of(" "); - if (start_idx == std::string::npos) - return ""; + std::string::size_type left = 0; + std::string::size_type right = input.size() - 1; - std::string::size_type end_idx = input.find_last_not_of(" "); - return std::string( input, start_idx, end_idx+1-start_idx); + for (; left < input.size(); ++left) + { + if (!std::isspace(input[left])) + { + break; + } + } + + for (; right >= left; --right) + { + if (!std::isspace(input[right])) + { + break; + } + } + + return std::string(input, left, right - left + 1); } + + std::string dim_string(const int dim, const int spacedim) { diff --git a/tests/base/utilities_trim.cc b/tests/base/utilities_trim.cc index 4a0a0972ee..09e66c5022 100644 --- a/tests/base/utilities_trim.cc +++ b/tests/base/utilities_trim.cc @@ -14,35 +14,36 @@ // --------------------------------------------------------------------- -// test Utilities::trim - -#include "../tests.h" -#include -#include -#include -#include +// test Utilities::trim. Note that deallog does not like being given '\n's in +// the middle of strings, so the output file is almost empty. +#include #include +#include + +using namespace dealii; void check(const std::string &input, const std::string &expected) { - deallog << "trim(\"" << input << "\") = \"" << Utilities::trim(input) << "\"" << std::endl; - Assert(Utilities::trim(input) == expected, ExcInternalError()); + AssertThrow(Utilities::trim(input) == expected, ExcInternalError()); } void test () { - check("Hello World", "Hello World"); + check("\r\nHello World\r\n\r", "Hello World"); check("", ""); check(" ", ""); check(" ", ""); - check(" middle ", "middle"); - check("left ", "left"); - check(" right", "right"); - check(" multiple words with spaces ", "multiple words with spaces"); + check(" \r\n\v\t\r\n\f ", ""); + check(" \rmiddle\r\n ", "middle"); + check("left\v\t\r\n ", "left"); + check(" \n\v\f\r\nright", "right"); + check(" \t\v\f\t\r\n\r multiple words with spaces \v\f\n", "multiple words with spaces"); + + deallog << "OK" << std::endl; } diff --git a/tests/base/utilities_trim.output b/tests/base/utilities_trim.output index 270fa06572..0fd8fc12f0 100644 --- a/tests/base/utilities_trim.output +++ b/tests/base/utilities_trim.output @@ -1,9 +1,2 @@ -DEAL::trim("Hello World") = "Hello World" -DEAL::trim("") = "" -DEAL::trim(" ") = "" -DEAL::trim(" ") = "" -DEAL::trim(" middle ") = "middle" -DEAL::trim("left ") = "left" -DEAL::trim(" right") = "right" -DEAL::trim(" multiple words with spaces ") = "multiple words with spaces" +DEAL::OK