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

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
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
{
const size_type strLen = this->size();

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -201,6 +201,14 @@ public:
//- Return string with trailing character removed
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.
// Always true if the given text is empty or if the string
// is identical to the given text.