mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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');
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2011-2013 OpenFOAM Foundation
|
Copyright (C) 2011-2013 OpenFOAM Foundation
|
||||||
Copyright (C) 2016-2019 OpenCFD Ltd.
|
Copyright (C) 2016-2020 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -173,6 +173,19 @@ int main(int argc, char *argv[])
|
|||||||
Info<<"trimRight: " << stringOps::trimRight(test) << endl;
|
Info<<"trimRight: " << stringOps::trimRight(test) << endl;
|
||||||
Info<<"trim: " << stringOps::trim(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)
|
if (false)
|
||||||
{
|
{
|
||||||
Info<<"test move construct - string size:" << test.size() << nl;
|
Info<<"test move construct - string size:" << test.size() << nl;
|
||||||
|
|||||||
@ -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)
|
Foam::string& Foam::string::expand(const bool allowEmpty)
|
||||||
{
|
{
|
||||||
stringOps::inplaceExpand(*this, allowEmpty);
|
stringOps::inplaceExpand(*this, allowEmpty);
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
\\/ M anipulation |
|
\\/ M anipulation |
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
Copyright (C) 2011-2016 OpenFOAM Foundation
|
Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||||
Copyright (C) 2016-2019 OpenCFD Ltd.
|
Copyright (C) 2016-2020 OpenCFD Ltd.
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
License
|
License
|
||||||
This file is part of OpenFOAM.
|
This file is part of OpenFOAM.
|
||||||
@ -234,7 +234,7 @@ public:
|
|||||||
|
|
||||||
//- Replace all occurrences of sub-string s1 with s2,
|
//- Replace all occurrences of sub-string s1 with s2,
|
||||||
//- beginning at pos in the string.
|
//- beginning at pos in the string.
|
||||||
// This is a no-op if s1 is empty.
|
// A no-op if s1 is empty.
|
||||||
string& replaceAll
|
string& replaceAll
|
||||||
(
|
(
|
||||||
const std::string& s1,
|
const std::string& s1,
|
||||||
@ -242,6 +242,16 @@ public:
|
|||||||
size_type pos = 0
|
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
|
//- Inplace expand initial tags, tildes, and all occurrences of
|
||||||
//- environment variables as per stringOps::expand
|
//- environment variables as per stringOps::expand
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user