string: Added remove(const char) member functions

which remove all occurrences of the specified character
This commit is contained in:
Henry Weller
2023-06-01 11:50:58 +01:00
parent 4cc975464c
commit 2561212f56
2 changed files with 56 additions and 7 deletions

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -129,6 +129,49 @@ Foam::string& Foam::string::expand(const bool allowEmpty)
} }
bool Foam::string::remove(const char character)
{
bool changed = false;
string::size_type n = 0;
iterator iter2 = begin();
for
(
string::const_iterator iter1 = iter2;
iter1 != end();
++iter1
)
{
char c = *iter1;
if (c == character)
{
changed = true;
}
else
{
*iter2 = c;
++iter2;
++n;
}
}
resize(n);
return changed;
}
Foam::string Foam::string::remove(const char character) const
{
string str(*this);
str.remove(character);
return str;
}
bool Foam::string::removeRepeated(const char character) bool Foam::string::removeRepeated(const char character)
{ {
bool changed = false; bool changed = false;
@ -142,7 +185,7 @@ bool Foam::string::removeRepeated(const char character)
( (
string::const_iterator iter1 = iter2; string::const_iterator iter1 = iter2;
iter1 != end(); iter1 != end();
++ iter1 ++iter1
) )
{ {
char c = *iter1; char c = *iter1;
@ -154,8 +197,8 @@ bool Foam::string::removeRepeated(const char character)
else else
{ {
*iter2 = cPrev = c; *iter2 = cPrev = c;
++ iter2; ++iter2;
++ n; ++n;
} }
} }

View File

@ -2,7 +2,7 @@
========= | ========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org \\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
@ -217,10 +217,16 @@ public:
// Foam::findEtcFile // Foam::findEtcFile
string& expand(const bool allowEmpty = false); string& expand(const bool allowEmpty = false);
//- Remove repeated characters returning true if string changed //- Remove all occurrences of character returning true if string changed
bool remove(const char);
//- Remove all occurrences of character and return the string
string remove(const char) const;
//- Remove repeated character returning true if string changed
bool removeRepeated(const char); bool removeRepeated(const char);
//- Return string with repeated characters removed //- Remove repeated character and return the string
string removeRepeated(const char) const; string removeRepeated(const char) const;
//- Remove trailing character returning true if string changed //- Remove trailing character returning true if string changed