diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index 8982cda893..88f4579df3 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2013 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -173,6 +173,19 @@ int main(int argc, char *argv[]) Info<<"trimRight: " << stringOps::trimRight(test) << endl; Info<<"trim: " << stringOps::trim(test) << endl; + // With replace, replaceAll + { + string test2(test); + Info<<"replaceAny: (\"abcj\", '?')" << nl + << test2.replaceAny("abcj", '?') << endl; + Info<<"replaceAll: (\"k\", \"?\")" << nl + << test2.replaceAll("k", "?") << endl; + Info<<"replaceAll: (\"h\", null) - ie, remove them" << nl + << test2.replaceAll("h", "") << endl; + Info<<"replaceAny: (\"it${}?\", null) - ie, remove them" << nl + << test2.replaceAny("it${}?", '\0') << endl; + } + if (false) { Info<<"test move construct - string size:" << test.size() << nl; diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index b7076865c5..2c6fe97082 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -156,6 +156,33 @@ Foam::string& Foam::string::replaceAll } +Foam::string& Foam::string::replaceAny +( + const std::string& s1, + const char c2, + size_type pos +) +{ + if (s1.length()) + { + while ((pos = find_first_of(s1, pos)) != npos) + { + if (c2) + { + operator[](pos) = c2; + ++pos; + } + else + { + erase(pos, 1); + } + } + } + + return *this; +} + + Foam::string& Foam::string::expand(const bool allowEmpty) { stringOps::inplaceExpand(*this, allowEmpty); diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index 829b28f978..5361ce1c2e 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2016-2019 OpenCFD Ltd. + Copyright (C) 2016-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -234,7 +234,7 @@ public: //- Replace all occurrences of sub-string s1 with s2, //- beginning at pos in the string. - // This is a no-op if s1 is empty. + // A no-op if s1 is empty. string& replaceAll ( const std::string& s1, @@ -242,6 +242,16 @@ public: size_type pos = 0 ); + //- Replace any occurrence of s1 characters with c2, + //- beginning at pos in the string. + // A no-op if s1 is empty. + string& replaceAny + ( + const std::string& s1, + const char c2, + size_type pos = 0 + ); + //- Inplace expand initial tags, tildes, and all occurrences of //- environment variables as per stringOps::expand //