ENH: provide string removeStart, removeEnd convenience methods

This commit is contained in:
Mark Olesen
2017-03-10 11:39:40 +01:00
parent 9810c68e76
commit 9077098935
4 changed files with 63 additions and 4 deletions

View File

@ -69,6 +69,16 @@ 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;
{
fileName test1("libFooBar.so");
Info<< nl;
Info<< "trim filename: " << test1 << nl;
test1.removeStart("lib");
Info<<"without leading 'lib': " << test1 << nl;
}
Info<< nl; Info<< nl;
Info<<"camel-case => " << (word("camel") & "case") << nl; Info<<"camel-case => " << (word("camel") & "case") << nl;
for (const auto& s : { " text with \"spaces'", "08/15 value" }) for (const auto& s : { " text with \"spaces'", "08/15 value" })

View File

@ -93,12 +93,11 @@ void Foam::dynamicCode::checkSecurity
Foam::word Foam::dynamicCode::libraryBaseName(const fileName& libPath) Foam::word Foam::dynamicCode::libraryBaseName(const fileName& libPath)
{ {
word libName(libPath.name(true)); word libName(libPath.name(true));
libName.erase(0, 3); // Remove leading 'lib' from name libName.removeStart("lib"); // Remove leading 'lib' from name
return libName; return libName;
} }
// * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
void Foam::dynamicCode::copyAndFilter void Foam::dynamicCode::copyAndFilter

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -167,6 +167,48 @@ Foam::string Foam::string::removeTrailing(const char character) const
} }
bool Foam::string::removeStart(const std::string& text)
{
const size_type txtLen = text.size();
if (!txtLen)
{
return true;
}
const size_type strLen = this->size();
if (strLen >= txtLen && !compare(0, txtLen, text))
{
this->erase(0, txtLen);
return true;
}
else
{
return false;
}
}
bool Foam::string::removeEnd(const std::string& text)
{
const size_type txtLen = text.size();
if (!txtLen)
{
return true;
}
const size_type strLen = this->size();
if (strLen >= txtLen && !compare(strLen - txtLen, npos, text))
{
this->resize(strLen - txtLen);
return true;
}
else
{
return false;
}
}
bool Foam::string::startsWith(const std::string& text) const bool Foam::string::startsWith(const std::string& text) const
{ {
const size_type strLen = this->size(); const size_type strLen = this->size();

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | \\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd. \\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -201,6 +201,14 @@ public:
//- Return string with trailing character removed //- Return string with trailing character removed
string removeTrailing(const char character) const; string removeTrailing(const char character) const;
//- Remove the given text from the start of the string.
// Always true if the removal occurred or the given text is empty.
bool removeStart(const std::string& text);
//- Remove the given text from the end of the string.
// Always true if the removal occurred or the given text is empty.
bool removeEnd(const std::string& text);
//- True if the string starts with the given text. //- True if the string starts with the given text.
// Always true if the given text is empty or if the string // Always true if the given text is empty or if the string
// is identical to the given text. // is identical to the given text.