From: Timo Heister Date: Sat, 11 Jul 2015 22:44:17 +0000 (-0400) Subject: add Utilities::trim() X-Git-Tag: v8.3.0-rc1~15^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3937ffb1c86ea3d07909d56d2a5ab22aed3c2d82;p=dealii.git add Utilities::trim() --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 5b8a1bf200..d4ecae5fb3 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -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. diff --git a/source/base/utilities.cc b/source/base/utilities.cc index 6625a2bfcc..9617414ac3 100644 --- a/source/base/utilities.cc +++ b/source/base/utilities.cc @@ -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 index 0000000000..7b2014cdcd --- /dev/null +++ b/tests/base/utilities_trim.cc @@ -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 +#include +#include +#include + +#include + + +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 index 0000000000..270fa06572 --- /dev/null +++ b/tests/base/utilities_trim.output @@ -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"