]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add Utilities::trim()
authorTimo Heister <timo.heister@gmail.com>
Sat, 11 Jul 2015 22:44:17 +0000 (18:44 -0400)
committerTimo Heister <timo.heister@gmail.com>
Sun, 12 Jul 2015 18:52:23 +0000 (14:52 -0400)
include/deal.II/base/utilities.h
source/base/utilities.cc
tests/base/utilities_trim.cc [new file with mode: 0644]
tests/base/utilities_trim.output [new file with mode: 0644]

index 5b8a1bf2000b75cf70a41dc3177c0483895bb878..d4ecae5fb301cc339902b6ed565b1c6764d07315 100644 (file)
@@ -198,6 +198,11 @@ namespace Utilities
                                 const std::string &from,
                                 const std::string &to);
 
+  /**
+   * Return a string with all spaces at the beginning and end of @p input removed.
+   */
+  std::string trim(const std::string &input);
+
   /**
    * Generate a random number from a normalized Gaussian probability
    * distribution centered around @p a and with standard deviation @p sigma.
index 6625a2bfcc3689d0f24046e005e73285e68efe8c..9617414ac3d931978dda442b7bd6a8e12187a6b2 100644 (file)
@@ -112,6 +112,16 @@ namespace Utilities
     return out;
   }
 
+  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 end_idx = input.find_last_not_of(" ");
+    return std::string( input, start_idx, end_idx+1-start_idx);
+  }
 
   std::string
   dim_string(const int dim, const int spacedim)
diff --git a/tests/base/utilities_trim.cc b/tests/base/utilities_trim.cc
new file mode 100644 (file)
index 0000000..7b2014c
--- /dev/null
@@ -0,0 +1,57 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2005 - 2015 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+// test Utilities::trim
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/utilities.h>
+
+
+void check(const std::string & input, const std::string & expected)
+{
+  deallog << "trim(\"" << input << "\") = \"" << Utilities::trim(input) << "\"" << std::endl;
+  Assert(Utilities::trim(input) == expected, ExcInternalError());
+}
+
+
+
+void test ()
+{
+  check("Hello World", "Hello World");
+  check("", "");
+  check(" ", "");
+  check("    ", "");
+  check("  middle   ", "middle");
+  check("left   ", "left");
+  check("  right", "right");
+  check("  multiple  words with spaces  ", "multiple  words with spaces");
+}
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.depth_console(0);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/utilities_trim.output b/tests/base/utilities_trim.output
new file mode 100644 (file)
index 0000000..270fa06
--- /dev/null
@@ -0,0 +1,9 @@
+
+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"

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.