ENH: rationalize some string methods.

- silently deprecate 'startsWith', 'endsWith' methods
  (added in 2016: 2b14360662), in favour of
  'starts_with', 'ends_with' methods, corresponding to C++20 and
  allowing us to cull then in a few years.

- handle single character versions of starts_with, ends_with.

- add single character version of removeEnd and silently deprecate
  removeTrailing which did the same thing.

- drop the const versions of removeRepeated, removeTrailing.
  Unused and with potential confusion.

STYLE: use shrink_to_fit(), erase()
This commit is contained in:
Mark Olesen 2019-11-11 18:50:00 +01:00 committed by Andrew Heather
parent ea214727a5
commit 7c1190f0b1
28 changed files with 185 additions and 206 deletions

View File

@ -83,7 +83,7 @@ int main(int argc, char *argv[])
const word& item const word& item
: DirLister::files(".").where : DirLister::files(".").where
( (
[](const word& val){ return val.startsWith("T"); } [](const word& val){ return val.starts_with('T'); }
) )
) )
{ {
@ -135,7 +135,7 @@ int main(int argc, char *argv[])
<< "~~~~~~~~~~" << nl << "~~~~~~~~~~" << nl
<< DirLister(".").list<fileName> << DirLister(".").list<fileName>
( (
[](const word& val){ return val.startsWith("D"); }, [](const word& val){ return val.starts_with('D'); },
false false
) )
<< nl; << nl;

View File

@ -353,7 +353,7 @@ int main(int argc, char *argv[])
<< word::printf("formatted '%08d'", val) << "\n"; << word::printf("formatted '%08d'", val) << "\n";
} }
// test startsWith, endsWith methods // test starts_with, ends_with methods
{ {
string empty; //; string empty; //;
string input1 = "shorter input"; string input1 = "shorter input";
@ -362,64 +362,64 @@ int main(int argc, char *argv[])
stringList checks{"match", "long", "", "short", "text", "s", "l", "t"}; stringList checks{"match", "long", "", "short", "text", "s", "l", "t"};
Info<< nl; Info<< nl;
Info<< "check startsWith:" << nl Info<< "check starts_with:" << nl
<< "~~~~~~~~~~~~~~~~~" << nl; << "~~~~~~~~~~~~~~~~~" << nl;
Info<<"input: " << empty << nl; Info<<"input: " << empty << nl;
for (const string& test : checks) for (const string& test : checks)
{ {
Info<< " startsWith(" << test << ") = " Info<< " starts_with(" << test << ") = "
<< Switch(empty.startsWith(test)) << nl; << Switch(empty.starts_with(test)) << nl;
} }
Info<<"input: " << input1 << nl; Info<<"input: " << input1 << nl;
for (const string& test : checks) for (const string& test : checks)
{ {
Info<< " startsWith(" << test << ") = " Info<< " starts_with(" << test << ") = "
<< Switch(input1.startsWith(test)) << nl; << Switch(input1.starts_with(test)) << nl;
} }
Info<<"input: " << input2 << nl; Info<<"input: " << input2 << nl;
for (const string& test : checks) for (const string& test : checks)
{ {
Info<< " startsWith(" << test << ") = " Info<< " starts_with(" << test << ") = "
<< Switch(input2.startsWith(test)) << nl; << Switch(input2.starts_with(test)) << nl;
} }
Info<< nl; Info<< nl;
Info<< "check endsWith:" << nl Info<< "check ends_with:" << nl
<< "~~~~~~~~~~~~~~~~~" << nl; << "~~~~~~~~~~~~~~~~~" << nl;
Info<<"input: " << empty << nl; Info<<"input: " << empty << nl;
for (const string& test : checks) for (const string& test : checks)
{ {
Info<< " endsWith(" << test << ") = " Info<< " ends_with(" << test << ") = "
<< Switch(empty.endsWith(test)) << nl; << Switch(empty.ends_with(test)) << nl;
} }
Info<<"input: " << input1 << nl; Info<<"input: " << input1 << nl;
for (const string& test : checks) for (const string& test : checks)
{ {
Info<< " endsWith(" << test << ") = " Info<< " ends_with(" << test << ") = "
<< Switch(input1.endsWith(test)) << nl; << Switch(input1.ends_with(test)) << nl;
} }
Info<<"input: " << input2 << nl; Info<<"input: " << input2 << nl;
for (const string& test : checks) for (const string& test : checks)
{ {
Info<< " endsWith(" << test << ") = " Info<< " ends_with(" << test << ") = "
<< Switch(input2.endsWith(test)) << nl; << Switch(input2.ends_with(test)) << nl;
} }
Info<< nl; Info<< nl;
Info<< "check endsWith as applied to field names:" << nl Info<< "check ends_with as applied to field names:" << nl
<< "~~~~~~~~~~~~~~~~~" << nl; << "~~~~~~~~~~~~~~~~~" << nl;
string input3 = "field_0"; string input3 = "field_0";
string input4 = "_0"; string input4 = "_0";
Info<<input3 << " endsWith(\"_0\") = " Info<<input3 << " ends_with(\"_0\") = "
<< Switch(input3.endsWith("_0")) << nl; << Switch(input3.ends_with("_0")) << nl;
Info<<input4 << " endsWith(\"_0\") = " Info<<input4 << " ends_with(\"_0\") = "
<< Switch(input4.endsWith("_0")) << nl; << Switch(input4.ends_with("_0")) << nl;
} }

View File

@ -175,7 +175,7 @@ int main(int argc, char *argv[])
{ {
nodeStream.getLine(line); nodeStream.getLine(line);
} }
while (line.size() && line[0] == '#'); while (line.starts_with('#'));
IStringStream nodeLine(line); IStringStream nodeLine(line);
@ -252,7 +252,7 @@ int main(int argc, char *argv[])
{ {
eleStream.getLine(line); eleStream.getLine(line);
} }
while (line.size() && line[0] == '#'); while (line.starts_with('#'));
IStringStream eleLine(line); IStringStream eleLine(line);
@ -368,7 +368,7 @@ int main(int argc, char *argv[])
{ {
faceStream.getLine(line); faceStream.getLine(line);
} }
while (line.size() && line[0] == '#'); while (line.starts_with('#'));
IStringStream faceLine(line); IStringStream faceLine(line);

View File

@ -6,6 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -51,7 +52,7 @@ string getLine(std::ifstream& is)
{ {
std::getline(is, line); std::getline(is, line);
} }
while (line.size() && line[0] == '#'); while (line.starts_with('#'));
return line; return line;
} }

View File

@ -40,7 +40,7 @@ Foam::autoPtr<Foam::helpType> Foam::helpType::New
{ {
// special treatment for -help // special treatment for -help
// exit without stack trace // exit without stack trace
if (helpTypeName.startsWith("-help")) if (helpTypeName.starts_with("-help"))
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Valid helpType selections:" << nl << "Valid helpType selections:" << nl

View File

@ -678,7 +678,7 @@ void Foam::vtkPVFoam::Update
// Suppress caching of Lagrangian since it normally always changes. // Suppress caching of Lagrangian since it normally always changes.
cachedVtp_.filterKeys cachedVtp_.filterKeys
( (
[](const word& k){ return k.startsWith("lagrangian/"); }, [](const word& k){ return k.starts_with("lagrangian/"); },
true // prune true // prune
); );
} }

View File

@ -179,7 +179,7 @@ void Foam::vtkPVFoam::convertMeshPatches()
} }
} }
if (longName.startsWith("group/")) if (longName.starts_with("group/"))
{ {
// Patch group. Collect patch faces. // Patch group. Collect patch faces.

View File

@ -1221,7 +1221,7 @@ void* Foam::dlOpen(const fileName& libName, const bool check)
( (
!handle !handle
&& libName.find('/') == std::string::npos && libName.find('/') == std::string::npos
&& !libso.startsWith("lib") && !libso.starts_with("lib")
) )
{ {
// Try with 'lib' prefix // Try with 'lib' prefix

View File

@ -1674,7 +1674,7 @@ void* Foam::dlOpen(const fileName& libName, const bool check)
if if
( (
libName.find('/') == std::string::npos libName.find('/') == std::string::npos
&& !libName.startsWith("lib") && !libName.starts_with("lib")
) )
{ {
// Try with 'lib' prefix // Try with 'lib' prefix

View File

@ -97,17 +97,6 @@ namespace Foam
} }
// file-scope
//
// A file is 'outside' of the case if it has been specified using an
// absolute path (starts with '/')
//
static inline bool isOutsideOfCase(const std::string& file)
{
return !file.empty() && file[0] == '/';
}
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
bool Foam::IOobject::fileNameComponents bool Foam::IOobject::fileNameComponents
@ -119,7 +108,7 @@ bool Foam::IOobject::fileNameComponents
) )
{ {
// Convert explicit relative file-system path to absolute file-system path. // Convert explicit relative file-system path to absolute file-system path.
if (path.startsWith("./") || path.startsWith("../")) if (path.starts_with("./") || path.starts_with("../"))
{ {
fileName absPath = cwd()/path; fileName absPath = cwd()/path;
absPath.clean(); absPath.clean();
@ -461,7 +450,10 @@ const Foam::fileName& Foam::IOobject::caseName() const
Foam::fileName Foam::IOobject::path() const Foam::fileName Foam::IOobject::path() const
{ {
if (isOutsideOfCase(instance())) // A file is 'outside' of the case if it has been specified using an
// absolute path (starts with '/')
if (instance().starts_with('/'))
{ {
return instance(); return instance();
} }

View File

@ -382,7 +382,7 @@ Foam::label Foam::IOobjectList::prune_0()
return return
HashPtrTable<IOobject>::filterKeys HashPtrTable<IOobject>::filterKeys
( (
[](const word& k){ return k.endsWith("_0"); }, [](const word& k){ return k.ends_with("_0"); },
true // prune true // prune
); );
} }

View File

@ -924,7 +924,7 @@ void Foam::GeometricField<Type, PatchField, GeoMesh>::storeOldTimes() const
( (
field0Ptr_ field0Ptr_
&& timeIndex_ != this->time().timeIndex() && timeIndex_ != this->time().timeIndex()
&& !this->name().endsWith("_0") && !this->name().ends_with("_0")
) )
{ {
storeOldTime(); storeOldTime();

View File

@ -1164,7 +1164,7 @@ void Foam::argList::parse
} }
// Case-relative if not absolute and not "./" etc // Case-relative if not absolute and not "./" etc
if (!source.isAbsolute() && !source.startsWith(".")) if (!source.isAbsolute() && !source.starts_with('.'))
{ {
source = rootPath_/globalCase_/source; source = rootPath_/globalCase_/source;
adjustOpt = true; adjustOpt = true;
@ -1632,8 +1632,8 @@ void Foam::argList::displayDoc(bool source) const
for (const fileName& dir : docDirs) for (const fileName& dir : docDirs)
{ {
// http protocols are last in the list // The http protocols are last in the list
if (dir.startsWith("http:") || dir.startsWith("https:")) if (dir.starts_with("http:") || dir.starts_with("https:"))
{ {
url = dir/executable_ + docExt; url = dir/executable_ + docExt;
break; break;
@ -1643,7 +1643,7 @@ void Foam::argList::displayDoc(bool source) const
if if
( (
docFile.startsWith("file://") docFile.starts_with("file://")
? isFile(docFile.substr(7)) // check part after "file://" ? isFile(docFile.substr(7)) // check part after "file://"
: isFile(docFile) : isFile(docFile)
) )

View File

@ -77,11 +77,11 @@ void Foam::solution::read(const dictionary& dict)
{ {
scalar value = relaxDict.get<scalar>(e); scalar value = relaxDict.get<scalar>(e);
if (e.startsWith("p")) if (e.starts_with('p'))
{ {
fieldRelaxDict_.add(e, value); fieldRelaxDict_.add(e, value);
} }
else if (e.startsWith("rho")) else if (e.starts_with("rho"))
{ {
fieldRelaxDict_.add(e, value); fieldRelaxDict_.add(e, value);
} }

View File

@ -191,36 +191,31 @@ bool Foam::fileName::equals(const std::string& s1, const std::string& s2)
} }
bool Foam::fileName::isBackup(const std::string& str) bool Foam::fileName::isBackup(const std::string& s)
{ {
if (str.empty()) if (s.empty())
{ {
return false; return false;
} }
else if (str.back() == '~') else if (s.back() == '~')
{ {
return true; return true;
} }
// Now check the extension // Now check the extension
const auto dot = find_ext(str); auto dot = find_ext(s);
if (dot == npos) if (dot == npos)
{ {
return false; return false;
} }
const std::string ending = str.substr(dot+1); ++dot;
if (ending.empty())
{
return false;
}
return return
( (
ending == "bak" || ending == "BAK" !s.compare(dot, npos, "bak") || !s.compare(dot, npos, "BAK")
|| ending == "old" || ending == "save" || !s.compare(dot, npos, "old") || !s.compare(dot, npos, "save")
); );
} }
@ -448,7 +443,7 @@ Foam::fileName Foam::fileName::relative
if if
( (
top && (f.size() > (top+1)) && f[top] == '/' top && (f.size() > (top+1)) && f[top] == '/'
&& f.startsWith(parent) && f.starts_with(parent)
) )
{ {
if (caseTag) if (caseTag)

View File

@ -128,7 +128,7 @@ inline void Foam::fileName::stripInvalid()
} }
removeRepeated('/'); removeRepeated('/');
removeTrailing('/'); removeEnd('/');
} }
} }
@ -137,7 +137,7 @@ inline bool Foam::fileName::isAbsolute(const std::string& str)
{ {
return return
( (
(!str.empty() && str[0] == '/') (!str.empty() && str.front() == '/') // ie, str.starts_with('/')
#ifdef _WIN32 #ifdef _WIN32
|| ||
( (

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016-2017 OpenCFD Ltd. Copyright (C) 2016-2019 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -34,7 +34,9 @@ License
/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
const char* const Foam::string::typeName = "string"; const char* const Foam::string::typeName = "string";
int Foam::string::debug(Foam::debug::debugSwitch(string::typeName, 0));
int Foam::string::debug(Foam::debug::debugSwitch(Foam::string::typeName, 0));
const Foam::string Foam::string::null; const Foam::string Foam::string::null;
@ -117,16 +119,14 @@ Foam::string::size_type Foam::string::count(const char c) const
Foam::string& Foam::string::replace Foam::string& Foam::string::replace
( (
const string& oldStr, const std::string& s1,
const string& newStr, const std::string& s2,
const size_type start size_type pos
) )
{ {
size_type pos = start; if ((pos = find(s1, pos)) != npos)
if ((pos = find(oldStr, pos)) != npos)
{ {
std::string::replace(pos, oldStr.size(), newStr); std::string::replace(pos, s1.size(), s2);
} }
return *this; return *this;
@ -135,24 +135,20 @@ Foam::string& Foam::string::replace
Foam::string& Foam::string::replaceAll Foam::string& Foam::string::replaceAll
( (
const string& oldStr, const std::string& s1,
const string& newStr, const std::string& s2,
const size_type start size_type pos
) )
{ {
const size_type lenOld = oldStr.size(); const auto n1 = s1.length();
const size_type lenNew = newStr.size(); const auto n2 = s2.length();
if (lenOld) if (n1)
{ {
for while ((pos = find(s1, pos)) != npos)
(
size_type pos = start;
(pos = find(oldStr, pos)) != npos;
pos += lenNew
)
{ {
std::string::replace(pos, lenOld, newStr); std::string::replace(pos, n1, s2);
pos += n2;
} }
} }
@ -201,47 +197,14 @@ bool Foam::string::removeRepeated(const char character)
} }
Foam::string Foam::string::removeRepeated(const char character) const
{
string str(*this);
str.removeRepeated(character);
return str;
}
bool Foam::string::removeTrailing(const char character)
{
const string::size_type nChar = size();
if (character && nChar > 1 && operator[](nChar-1) == character)
{
resize(nChar-1);
return true;
}
return false;
}
Foam::string Foam::string::removeTrailing(const char character) const
{
string str(*this);
str.removeTrailing(character);
return str;
}
bool Foam::string::removeStart(const std::string& text) bool Foam::string::removeStart(const std::string& text)
{ {
const size_type txtLen = text.size(); const auto txtLen = text.length();
if (!txtLen) const auto strLen = length();
{
return true;
}
const size_type strLen = this->size(); if (txtLen && strLen >= txtLen && !compare(0, txtLen, text))
if (strLen >= txtLen && !compare(0, txtLen, text))
{ {
this->erase(0, txtLen); erase(0, txtLen);
return true; return true;
} }
@ -251,16 +214,12 @@ bool Foam::string::removeStart(const std::string& text)
bool Foam::string::removeEnd(const std::string& text) bool Foam::string::removeEnd(const std::string& text)
{ {
const size_type txtLen = text.size(); const auto txtLen = text.length();
if (!txtLen) const auto strLen = length();
{
return true;
}
const size_type strLen = this->size(); if (txtLen && strLen >= txtLen && !compare(strLen - txtLen, npos, text))
if (strLen >= txtLen && !compare(strLen - txtLen, npos, text))
{ {
this->resize(strLen - txtLen); resize(strLen - txtLen);
return true; return true;
} }
@ -268,29 +227,28 @@ bool Foam::string::removeEnd(const std::string& text)
} }
bool Foam::string::startsWith(const std::string& text) const bool Foam::string::removeStart(const char c)
{ {
const size_type strLen = this->size(); if (length() > 1 && front() == c)
const size_type txtLen = text.size(); {
erase(0, 1);
return true;
}
return return false;
(
!txtLen
|| (strLen >= txtLen && !compare(0, txtLen, text))
);
} }
bool Foam::string::endsWith(const std::string& text) const bool Foam::string::removeEnd(const char c)
{ {
const size_type strLen = this->size(); const auto n = length();
const size_type txtLen = text.size(); if (n > 1 && back() == c)
{
resize(n-1);
return true;
}
return return false;
(
!txtLen
|| (strLen >= txtLen && !compare(strLen - txtLen, npos, text))
);
} }

View File

@ -40,6 +40,7 @@ See also
configuration directory configuration directory
SourceFiles SourceFiles
stringI.H
string.C string.C
stringIO.C stringIO.C
stringTemplates.C stringTemplates.C
@ -51,7 +52,6 @@ SourceFiles
#include "char.H" #include "char.H"
#include "Hasher.H" #include "Hasher.H"
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
@ -136,9 +136,12 @@ protected:
public: public:
// Static data members // Static Data Members
//- The type name "string"
static const char* const typeName; static const char* const typeName;
//- The debug flag
static int debug; static int debug;
//- An empty string //- An empty string
@ -183,12 +186,7 @@ public:
explicit string(Istream& is); explicit string(Istream& is);
// Member Functions // Static Member Functions
//- Count the number of occurrences of the specified character
//- in the string
// Partially deprecated (NOV-2017) in favour of stringOps::count
size_type count(const char c) const;
//- Does the string contain valid characters only? //- Does the string contain valid characters only?
template<class String> template<class String>
@ -215,6 +213,9 @@ public:
const char quote = '\\' const char quote = '\\'
); );
// Member Functions
//- Test for equality. //- Test for equality.
// \return True when strings match literally. // \return True when strings match literally.
inline bool match(const std::string& text) const; inline bool match(const std::string& text) const;
@ -222,22 +223,23 @@ public:
//- Avoid masking the normal std::string replace //- Avoid masking the normal std::string replace
using std::string::replace; using std::string::replace;
//- Replace first occurrence of sub-string oldStr with newStr, //- Replace first occurrence of sub-string s1 with s2,
//- beginning at start //- beginning at pos
string& replace string& replace
( (
const string& oldStr, const std::string& s1,
const string& newStr, const std::string& s2,
const size_type start = 0 size_type pos = 0
); );
//- Replace all occurrences of sub-string oldStr with newStr, //- Replace all occurrences of sub-string s1 with s2,
//- beginning at start. This is a no-op if oldStr is empty. //- beginning at pos in the string.
// This is a no-op if s1 is empty.
string& replaceAll string& replaceAll
( (
const string& oldStr, const std::string& s1,
const string& newStr, const std::string& s2,
const size_type start = 0 size_type pos = 0
); );
//- Inplace expand initial tags, tildes, and all occurrences of //- Inplace expand initial tags, tildes, and all occurrences of
@ -252,34 +254,21 @@ public:
// \return True if string changed // \return True if string changed
bool removeRepeated(const char character); bool removeRepeated(const char character);
//- Return string with repeated characters removed
string removeRepeated(const char character) const;
//- Remove trailing character, unless string is a single character
// \return True if string changed
bool removeTrailing(const char character);
//- Return string with trailing character removed,
// unless string is a single character
string removeTrailing(const char character) const;
//- Remove the given text from the start of the string. //- Remove the given text from the start of the string.
// \return True if the removal occurred or the given text is empty. // \return True if the removal occurred
bool removeStart(const std::string& text); bool removeStart(const std::string& text);
//- Remove leading character, unless string is a single character
// \return True if the removal occurred
bool removeStart(const char c);
//- Remove the given text from the end of the string. //- Remove the given text from the end of the string.
// Always true if the removal occurred or the given text is empty. // \return True if the removal occurred
bool removeEnd(const std::string& text); bool removeEnd(const std::string& text);
//- True if the string starts with the given text. //- Remove trailing character, unless string is a single character
// Always true if the given text is empty or if the string // \return True if the removal occurred
// is identical to the given text. bool removeEnd(const char c);
bool startsWith(const std::string& text) const;
//- True if the string ends with the given text.
// Always true if the given text is empty or if the string
// is identical to the given text.
bool endsWith(const std::string& text) const;
// Editing // Editing
@ -293,6 +282,50 @@ public:
//- Test for equality. Allows use as a predicate. //- Test for equality. Allows use as a predicate.
// \return True when strings match literally. // \return True when strings match literally.
inline bool operator()(const std::string& text) const; inline bool operator()(const std::string& text) const;
// Housekeeping
//- True if string starts with the given prefix (cf. C++20)
bool starts_with(const std::string& s) const
{
return (size() >= s.size() && !compare(0, s.size(), s));
}
//- True if string starts with the given character (cf. C++20)
bool starts_with(const char c) const
{
return (!empty() && front() == c);
}
//- True if string ends with the given suffix (cf. C++20)
bool ends_with(const std::string& s) const
{
return (size() >= s.size() && !compare(size()-s.size(), npos, s));
}
//- True if string ends with the given character (cf. C++20)
bool ends_with(const char c) const
{
return (!empty() && back() == c);
}
//- Count the number of occurrences of the specified character
//- in the string
// Partially deprecated (NOV-2017) in favour of stringOps::count
size_type count(const char c) const;
//- Deprecated(2019-11)
// \deprecated(2019-11) use starts_with instead
bool startsWith(const std::string& s) const { return starts_with(s); }
//- Deprecated(2019-11)
// \deprecated(2019-11) use ends_with instead
bool endsWith(const std::string& s) const { return ends_with(s); }
//- Deprecated(2019-11)
// \deprecated(2019-11) use removeEnd instead
bool removeTrailing(const char c) { return removeEnd(c); }
}; };

View File

@ -68,7 +68,7 @@ inline bool Foam::string::removePath()
return false; return false;
} }
this->erase(0, i+1); erase(0, i+1);
return true; return true;
} }
@ -82,7 +82,7 @@ inline bool Foam::string::removeExt()
return false; return false;
} }
this->resize(i); erase(i);
return true; return true;
} }
@ -163,7 +163,7 @@ inline bool Foam::string::stripInvalid(std::string& str)
} }
} }
str.resize(nChar); str.erase(nChar);
return true; return true;
} }
@ -228,7 +228,7 @@ Foam::string::quotemeta(const std::string& str, const char quote)
sQuoted += c; sQuoted += c;
} }
sQuoted.resize(sQuoted.size()); sQuoted.shrink_to_fit();
return sQuoted; return sQuoted;
} }
@ -251,7 +251,7 @@ inline String Foam::string::validate(const std::string& str)
} }
} }
out.resize(len); out.erase(len);
return out; return out;
} }

View File

@ -71,7 +71,7 @@ inline void Foam::ensight::FileName::stripInvalid()
string::stripInvalid<FileName>(*this); string::stripInvalid<FileName>(*this);
removeRepeated('/'); removeRepeated('/');
removeTrailing('/'); removeEnd('/');
if (empty()) if (empty())
{ {

View File

@ -618,7 +618,7 @@ Foam::label Foam::vtk::seriesWriter::scan
return return
( (
minLen < file.length() minLen < file.length()
&& file.hasExt(ext) && file.startsWith(stem) && file.hasExt(ext) && file.starts_with(stem)
); );
}; };

View File

@ -256,7 +256,7 @@ bool Foam::functionObjects::vtkCloud::writeCloud
( (
[](const word& k) [](const word& k)
{ {
return k.startsWith("position") || k.startsWith("coordinate"); return k.starts_with("position") || k.starts_with("coordinate");
}, },
true // prune true // prune
); );

View File

@ -226,7 +226,7 @@ bool Foam::functionObjects::ensightWrite::write()
// Prune restart fields // Prune restart fields
acceptField.filterKeys acceptField.filterKeys
( (
[](const word& k){ return k.endsWith("_0"); }, [](const word& k){ return k.ends_with("_0"); },
true // prune true // prune
); );

View File

@ -304,7 +304,7 @@ bool Foam::functionObjects::vtkWrite::write()
// Prune restart fields // Prune restart fields
acceptField.filterKeys acceptField.filterKeys
( (
[](const word& k){ return k.endsWith("_0"); }, [](const word& k){ return k.ends_with("_0"); },
true // prune true // prune
); );

View File

@ -115,7 +115,7 @@ bool Foam::fileFormats::OBJedgeFormat::read(const fileName& filename)
string line = this->getLineNoComment(is); string line = this->getLineNoComment(is);
// Line continuations // Line continuations
while (line.removeEnd("\\")) while (line.removeEnd('\\'))
{ {
line += this->getLineNoComment(is); line += this->getLineNoComment(is);
} }

View File

@ -611,7 +611,7 @@ bool Foam::dynamicOversetFvMesh::update()
Foam::word Foam::dynamicOversetFvMesh::baseName(const word& name) Foam::word Foam::dynamicOversetFvMesh::baseName(const word& name)
{ {
if (name.endsWith("_0")) if (name.ends_with("_0"))
{ {
return baseName(name.substr(0, name.size()-2)); return baseName(name.substr(0, name.size()-2));
} }

View File

@ -149,7 +149,7 @@ bool Foam::fileFormats::NASsurfaceFormat<Face>::read
// ANSA extension // ANSA extension
// line 1: $ANSA_NAME;<int>;<word>; // line 1: $ANSA_NAME;<int>;<word>;
// line 2: $partName // line 2: $partName
if (line.startsWith("$ANSA_NAME")) if (line.starts_with("$ANSA_NAME"))
{ {
const auto sem0 = line.find(';', 0); const auto sem0 = line.find(';', 0);
const auto sem1 = line.find(';', sem0+1); const auto sem1 = line.find(';', sem0+1);
@ -167,7 +167,7 @@ bool Foam::fileFormats::NASsurfaceFormat<Face>::read
string rawName; string rawName;
is.getLine(rawName); is.getLine(rawName);
rawName.removeEnd("\r"); // Possible CR-NL rawName.removeEnd('\r'); // Possible CR-NL
ansaName = word::validate(rawName.substr(1)); ansaName = word::validate(rawName.substr(1));
// Info<< "ANSA tag for NastranID:" << ansaId // Info<< "ANSA tag for NastranID:" << ansaId
@ -179,7 +179,7 @@ bool Foam::fileFormats::NASsurfaceFormat<Face>::read
// HYPERMESH extension // HYPERMESH extension
// $HMNAME COMP 1"partName" // $HMNAME COMP 1"partName"
if (line.startsWith("$HMNAME COMP") && line.find('"') != string::npos) if (line.starts_with("$HMNAME COMP") && line.find('"') != string::npos)
{ {
label groupId = readLabel(line.substr(16, 16)); label groupId = readLabel(line.substr(16, 16));

View File

@ -83,7 +83,7 @@ bool Foam::fileFormats::OBJsurfaceFormat<Face>::read
string line = this->getLineNoComment(is); string line = this->getLineNoComment(is);
// Line continuations // Line continuations
while (line.removeEnd("\\")) while (line.removeEnd('\\'))
{ {
line += this->getLineNoComment(is); line += this->getLineNoComment(is);
} }