From 71d21f6de4f7a701dfb1361997899e922c9959a1 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Wed, 23 Feb 2011 10:13:10 +0100 Subject: [PATCH] ENH: implement stringOps - trim, trimRight, inplaceTrim, inplaceTrimRight --- applications/test/string/Test-string.C | 13 +++++- .../primitives/strings/stringOps/stringOps.C | 42 +++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index 5a182377df8..d117f50c41c 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd. + \\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -26,6 +26,7 @@ Description \*---------------------------------------------------------------------------*/ #include "string.H" +#include "stringOps.H" #include "IOstreams.H" using namespace Foam; @@ -35,11 +36,19 @@ using namespace Foam; int main(int argc, char *argv[]) { - string test("$HOME kjhkjhkjh \" \\$HOME/tyetyery ${FOAM_RUN} \n ; hkjh ;$"); + string test + ( + " $HOME kjhkjhkjh \" \\$HOME/tyetyery ${FOAM_RUN} \n ; hkjh ;$ " + ); Info<< "string:" << test << nl << "hash:" << unsigned(string::hash()(test)) << endl; + Info<<"trimLeft: " << stringOps::trimLeft(test) << endl; + Info<<"trimRight: " << stringOps::trimRight(test) << endl; + Info<<"trim: " << stringOps::trim(test) << endl; + + // test sub-strings via iterators string::const_iterator iter = test.end(); string::const_iterator iter2 = test.end(); diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index e3a4d4c8db6..49142b10ecb 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -36,8 +36,8 @@ Foam::string Foam::stringOps::expand const char sigil ) { - string str(original); - return inplaceExpand(str, mapping); + string s(original); + return inplaceExpand(s, mapping); } @@ -137,8 +137,8 @@ Foam::string Foam::stringOps::expandEnv const bool allowEmptyVar ) { - string str(original); - return inplaceExpandEnv(str, recurse, allowEmptyVar); + string s(original); + return inplaceExpandEnv(s, recurse, allowEmptyVar); } @@ -331,28 +331,52 @@ Foam::string& Foam::stringOps::inplaceTrimLeft(string& s) Foam::string Foam::stringOps::trimRight(const string& s) { - notImplemented("string stringOps::trimRight(const string&)"); + if (!s.empty()) + { + string::size_type sz = s.size(); + while (sz && isspace(s[sz-1])) + { + --sz; + } + + if (sz < s.size()) + { + return s.substr(0, sz); + } + } + return s; } Foam::string& Foam::stringOps::inplaceTrimRight(string& s) { - notImplemented("string& stringOps::inplaceTrimRight(string&)"); + if (!s.empty()) + { + string::size_type sz = s.size(); + while (sz && isspace(s[sz-1])) + { + --sz; + } + + s.resize(sz); + } + return s; } Foam::string Foam::stringOps::trim(const string& original) { - notImplemented("string stringOps::trim(const string&)"); - return original; + return trimLeft(trimRight(original)); } Foam::string& Foam::stringOps::inplaceTrim(string& s) { - notImplemented("string& stringOps::inplaceTrim(string&)"); + inplaceTrimRight(s); + inplaceTrimLeft(s); + return s; } -- GitLab