From 14e2dbfb2a8e6266371dea85b098adb484fac268 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 28 Apr 2020 14:11:22 +0200 Subject: [PATCH] ENH: add string replaceAny() method - takes a search string and a replacement character. The replacement character can also be a nul char ('\0'), which simply removes the characters. Possible uses: * Replace reserved characters str.replaceAny("<>:", '_'); * Remove shell meta-characters or reserved filesystem characters str.replaceAny("*?<>{}[]:", '\0'); --- applications/test/string/Test-string.C | 15 ++++++++++- .../primitives/strings/string/string.C | 27 +++++++++++++++++++ .../primitives/strings/string/string.H | 14 ++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) 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 //