<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>
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.
}
+ 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)
{
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// 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 ();
+}
--- /dev/null
+
+DEAL::OK
}
-/**
- * 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;
-}
/*
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;
}