]> https://gitweb.dealii.org/ - dealii.git/commitdiff
new Utilities::replace_in_string() 1082/head
authorTimo Heister <timo.heister@gmail.com>
Sun, 5 Jul 2015 15:16:07 +0000 (11:16 -0400)
committerTimo Heister <timo.heister@gmail.com>
Sun, 5 Jul 2015 15:37:06 +0000 (11:37 -0400)
doc/news/changes.h
include/deal.II/base/utilities.h
source/base/utilities.cc
tests/base/replace_in_string_01.cc [new file with mode: 0644]
tests/base/replace_in_string_01.output [new file with mode: 0644]
tests/tests.h

index a3b6646cac9b5183f24cc18d1513dd58be14eb2f..ff24696ea40648e420490c41254f2f87fc1b4dc1 100644 (file)
@@ -541,6 +541,11 @@ inconvenience this causes.
 
 <ol>
 
+  <li>New: Utilities::replace_in_string().
+  <br>
+  (Timo Heister, 2015/07/05)
+  </li>
+
   <li>Improved: GridOut::write_vtk() and GridOut::write_vtu() now
   output material id, level and subdomain ids of the cells.
   <br>
index f73a4566efaff4d4d659b2ee0cd749e1574722f9..0bfa89d6bcd69bfd038e419ed4ef4458e20a53ee 100644 (file)
@@ -190,6 +190,14 @@ namespace Utilities
   get_integer_at_position (const std::string &name,
                            const unsigned int position);
 
+  /**
+   * Return a string with all occurrences of @p from in @p input replaced by
+   * @p to.
+   */
+  std::string replace_in_string(const std::string &input,
+                                const std::string &from,
+                                const std::string &to);
+
   /**
    * Generate a random number from a normalized Gaussian probability
    * distribution centered around @p a and with standard deviation @p sigma.
index c8bd42a2c549780d967e887570cd9961d8320e3d..b54b5e8f5b14ab2f981362f31fdc7b027e1b9ac7 100644 (file)
@@ -93,6 +93,26 @@ namespace Utilities
   }
 
 
+  std::string
+  replace_in_string(const std::string &input,
+                    const std::string &from,
+                    const std::string &to)
+  {
+    if (from.empty())
+      return input;
+
+    std::string out = input;
+    std::string::size_type pos = out.find(from);
+
+    while (pos != std::string::npos)
+      {
+        out.replace(pos, from.size(), to);
+        pos = out.find(from, pos + to.size());
+      }
+    return out;
+  }
+
+
   std::string
   dim_string(const int dim, const int spacedim)
   {
diff --git a/tests/base/replace_in_string_01.cc b/tests/base/replace_in_string_01.cc
new file mode 100644 (file)
index 0000000..f6f57a0
--- /dev/null
@@ -0,0 +1,66 @@
+// ---------------------------------------------------------------------
+//
+// 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::replace_in_string
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/utilities.h>
+
+void check(const std::string in, const std::string from, const std::string to, std::string out)
+{
+  std::string result = Utilities::replace_in_string(in, from, to);
+  if (result != out)
+    {
+      deallog << "in='" << in
+             << "' from='" << from
+             << "' to='" << to
+             << "' result='" << result
+             << "' != '" << out << "'"
+             << std::endl;
+    }
+}
+
+
+void test ()
+{
+  check("wie geht es dir?","dir","euch","wie geht es euch?");
+  check("empty from","","abc","empty from");
+  check("eins zwei drei","ei","","ns zw dr");
+  check("eins zwei drei","zwei","zweiundvierzig","eins zweiundvierzig drei");
+  check("wer das liest ist doof","das liest ","","wer ist doof");
+  check("string string","string",""," ");
+  check(" same is same"," same"," same"," same is same");
+  check("  "," ","","");
+  check(""," ","abc","");
+  check("small SMALL","LL","ll","small SMAll");
+  deallog << "OK" << std::endl;
+}
+
+
+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/replace_in_string_01.output b/tests/base/replace_in_string_01.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK
index d4be9aa6ae16fe230033055f42855ee17d56f800..5b3a6d46193eb59324195419caff80d6616d1269 100644 (file)
@@ -133,28 +133,6 @@ void sort_file_contents (const std::string &filename)
 }
 
 
-/**
- * Replace all occurrences of @p from in @p input by @p to and return the new
- * string.
- * TODO: move this to Utilities.
- */
-std::string replace(const std::string& input,
-                   const std::string& from,
-                   const std::string& to)
-{
-  if (from.empty())
-    return input;
-
-  std::string out = input;
-  std::string::size_type pos = out.find(from);
-  
-  while (pos != std::string::npos)
-    {
-      out.replace(pos, from.size(), to);
-      pos = out.find(from, pos + to.size());
-    }
-  return out;
-}
 
 
 /*
@@ -167,10 +145,10 @@ std::string replace(const std::string& input,
 std::string unify_pretty_function (const std::string &text)
 {
   std::string t=text;
-  t=replace(t, " &", "& ");
-  t=replace(t, " & ,", "&,");
-  t=replace(t, " & ", "& ");
-  t=replace(t, "virtual ", "");
+  t=Utilities::replace_in_string(t, " &", "& ");
+  t=Utilities::replace_in_string(t, " & ,", "&,");
+  t=Utilities::replace_in_string(t, " & ", "& ");
+  t=Utilities::replace_in_string(t, "virtual ", "");
   return t;
 }
 

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.