From: Timo Heister Date: Sun, 5 Jul 2015 15:16:07 +0000 (-0400) Subject: new Utilities::replace_in_string() X-Git-Tag: v8.3.0-rc1~56^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1082%2Fhead;p=dealii.git new Utilities::replace_in_string() --- diff --git a/doc/news/changes.h b/doc/news/changes.h index a3b6646cac..ff24696ea4 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -541,6 +541,11 @@ inconvenience this causes.
    +
  1. New: Utilities::replace_in_string(). +
    + (Timo Heister, 2015/07/05) +
  2. +
  3. Improved: GridOut::write_vtk() and GridOut::write_vtu() now output material id, level and subdomain ids of the cells.
    diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index f73a4566ef..0bfa89d6bc 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -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. diff --git a/source/base/utilities.cc b/source/base/utilities.cc index c8bd42a2c5..b54b5e8f5b 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -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 index 0000000000..f6f57a0f3a --- /dev/null +++ b/tests/base/replace_in_string_01.cc @@ -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 +#include +#include +#include + +#include + +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 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/replace_in_string_01.output @@ -0,0 +1,2 @@ + +DEAL::OK diff --git a/tests/tests.h b/tests/tests.h index d4be9aa6ae..5b3a6d4619 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -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; }