From 2561212f562458f233fe87c55474b4fcbb7d0397 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 1 Jun 2023 11:50:58 +0100 Subject: [PATCH] string: Added remove(const char) member functions which remove all occurrences of the specified character --- .../primitives/strings/string/string.C | 51 +++++++++++++++++-- .../primitives/strings/string/string.H | 12 +++-- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index 83d2280a7a..8fa9a0e8d1 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- 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 changed = false; @@ -142,7 +185,7 @@ bool Foam::string::removeRepeated(const char character) ( string::const_iterator iter1 = iter2; iter1 != end(); - ++ iter1 + ++iter1 ) { char c = *iter1; @@ -154,8 +197,8 @@ bool Foam::string::removeRepeated(const char character) else { *iter2 = cPrev = c; - ++ iter2; - ++ n; + ++iter2; + ++n; } } diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index e7415668c6..b8c299ad06 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2023 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -217,10 +217,16 @@ public: // Foam::findEtcFile 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); - //- Return string with repeated characters removed + //- Remove repeated character and return the string string removeRepeated(const char) const; //- Remove trailing character returning true if string changed