mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: implement stringOps - trim, trimRight, inplaceTrim, inplaceTrimRight
This commit is contained in:
@ -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();
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user