</p>
<ol>
+ <li> Changed: The function <code>Utilities::trim</code> now removes general
+ white space characters, such as '<tt>\\r</tt>' and '<tt>\\n</tt>', as well as
+ space characters.
+ <br>
+ (David Wells, 2015/12/05)
+ </li>
+
<li> 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
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
+ * '<tt>\\t</tt>', '<tt>\\n</tt>', and '<tt>\\r</tt>') 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
DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
#include <algorithm>
+#include <cctype>
#include <cerrno>
#include <cmath>
#include <cstddef>
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)
{
// ---------------------------------------------------------------------
-// test Utilities::trim
-
-#include "../tests.h"
-#include <iomanip>
-#include <iomanip>
-#include <fstream>
-#include <cmath>
+// 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 <deal.II/base/logstream.h>
#include <deal.II/base/utilities.h>
+#include <fstream>
+
+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;
}
-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