Commit e70fc616 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: consolidate, cleanup some string methods

- consolidate word::validated() into word::validate() and also allow
  as short form for string::validate<word>(). Also less confusing than
  having similarly named methods that essentially do the same thing.

- more consistent const access when iterating over strings

- add valid(char) for keyType and wordRe
parent 3f6e130c
......@@ -40,7 +40,8 @@ using namespace Foam;
int main(void)
{
string st("sfdsf sdfs23df sdf32f . sdfsdff23/2sf32");
Info<< word(string::validate<word>(st)) << "END" << endl;
Info<<"string: " << st << nl;
Info<<"word: \"" << string::validate<word>(st) << "\"" << endl;
string st1("1234567");
......
......@@ -84,7 +84,7 @@ int main(int argc, char *argv[])
for (const auto& s : { " text with \"spaces'", "08/15 value" })
{
Info<<"validated \"" << s << "\" => "
<< word::validated(s, true) << nl;
<< word::validate(s, true) << nl;
}
Info<< nl;
......
......@@ -207,7 +207,7 @@ int main(int argc, char *argv[])
// #include "checkHasMovingMesh.H"
// #include "checkHasLagrangian.H"
IOobjectList objects(mesh, timeDirs[timeDirs.size()-1].name());
IOobjectList objects(mesh, timeDirs.last().name());
forAll(timeDirs, timeI)
{
......
......@@ -488,7 +488,7 @@ mtype {space}"MTYPE:"{space}
<cellStreamTitle>{spaceNl}{word}{spaceNl} {
word streamName(Foam::string::validate<word>(YYText()));
const word streamName(word::validate(YYText()));
BEGIN(cellStreamFlags);
}
......
......@@ -370,7 +370,7 @@ void readPhysNames(IFstream& inFile, Map<word>& physicalNames)
lineStr >> regionI >> regionName;
Info<< " " << regionI << '\t'
<< string::validate<word>(regionName) << endl;
<< word::validate(regionName) << endl;
}
else if (nSpaces == 2)
{
......@@ -380,21 +380,21 @@ void readPhysNames(IFstream& inFile, Map<word>& physicalNames)
if (physType == 1)
{
Info<< " " << "Line " << regionI << '\t'
<< string::validate<word>(regionName) << endl;
<< word::validate(regionName) << endl;
}
else if (physType == 2)
{
Info<< " " << "Surface " << regionI << '\t'
<< string::validate<word>(regionName) << endl;
<< word::validate(regionName) << endl;
}
else if (physType == 3)
{
Info<< " " << "Volume " << regionI << '\t'
<< string::validate<word>(regionName) << endl;
<< word::validate(regionName) << endl;
}
}
physicalNames.insert(regionI, string::validate<word>(regionName));
physicalNames.insert(regionI, word::validate(regionName));
}
inFile.getLine(line);
......
......@@ -512,7 +512,7 @@ void readSets
>> dofSet >> tempSet >> contactSet >> nFaces;
is.getLine(line);
word groupName = string::validate<word>(line);
const word groupName = word::validate(line);
Info<< "For group " << group
<< " named " << groupName
......
......@@ -598,10 +598,7 @@ int main(int argc, char *argv[])
// Pass2: reconstruct the cloud
forAllConstIter(HashTable<IOobjectList>, cloudObjects, iter)
{
const word cloudName = string::validate<word>
(
iter.key()
);
const word cloudName = word::validate(iter.key());
// Objects (on arbitrary processor)
const IOobjectList& sprayObjs = iter();
......
......@@ -149,7 +149,7 @@ bool Foam::IOobject::fileNameComponents
// No '/' found (or empty entirely)
// => no instance or local
name = word::validated(path, false);
name = word::validate(path);
}
else if (first == 0)
{
......@@ -160,7 +160,7 @@ bool Foam::IOobject::fileNameComponents
const std::string ending = path.substr(last+1);
nameLen = ending.size(); // The raw length of name
name = word::validated(ending, false);
name = word::validate(ending);
}
else
{
......@@ -176,7 +176,7 @@ bool Foam::IOobject::fileNameComponents
const std::string ending = path.substr(last+1);
nameLen = ending.size(); // The raw length of name
name = word::validated(ending, false);
name = word::validate(ending);
}
// Check for valid (and stripped) name, regardless of the debug level
......
......@@ -185,7 +185,7 @@ Foam::Ostream& Foam::UOPstream::write(const char c)
Foam::Ostream& Foam::UOPstream::write(const char* str)
{
word nonWhiteChars(string::validate<word>(str));
const word nonWhiteChars(string::validate<word>(str));
if (nonWhiteChars.size() == 1)
{
......
......@@ -81,9 +81,9 @@ Foam::Ostream& Foam::OSstream::write(const string& str)
os_ << token::BEGIN_STRING;
int backslash = 0;
for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
{
char c = *iter;
const char c = *iter;
if (c == '\\')
{
......@@ -132,14 +132,9 @@ Foam::Ostream& Foam::OSstream::writeQuoted
os_ << token::BEGIN_STRING;
int backslash = 0;
for
(
string::const_iterator iter = str.begin();
iter != str.end();
++iter
)
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
{
char c = *iter;
const char c = *iter;
if (c == '\\')
{
......
......@@ -235,7 +235,7 @@ bool Foam::functionObjectList::readFunctionObject
{
args.append
(
string::validate<word>
word::validate
(
funcNameArgs.substr(start, i - start)
)
......@@ -255,7 +255,7 @@ bool Foam::functionObjectList::readFunctionObject
}
else if (c == '=')
{
argName = string::validate<word>
argName = word::validate
(
funcNameArgs.substr(start, i - start)
);
......@@ -330,7 +330,7 @@ bool Foam::functionObjectList::readFunctionObject
// Merge this functionObject dictionary into functionsDict
dictionary funcArgsDict;
funcArgsDict.add(string::validate<word>(funcNameArgs), funcDict);
funcArgsDict.add(word::validate(funcNameArgs), funcDict);
functionsDict.merge(funcArgsDict);
return true;
......
......@@ -55,7 +55,7 @@ Foam::objectRegistry::objectRegistry
(
IOobject
(
string::validate<word>(t.caseName()),
word::validate(t.caseName()),
t.path(),
t,
IOobject::NO_READ,
......
......@@ -53,13 +53,13 @@ class Ostream;
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Output wide character (Unicode) as UTF-8
Ostream& operator<<(Ostream&, const wchar_t);
Ostream& operator<<(Ostream& os, const wchar_t wc);
//- Output wide character (Unicode) string as UTF-8
Ostream& operator<<(Ostream&, const wchar_t*);
Ostream& operator<<(Ostream& os, const wchar_t* wstr);
//- Output wide character (Unicode) string as UTF-8
Ostream& operator<<(Ostream&, const std::wstring&);
Ostream& operator<<(Ostream& os, const std::wstring& wstr);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -116,12 +116,7 @@ Foam::Ostream& Foam::operator<<(Ostream& os, const wchar_t* wstr)
Foam::Ostream& Foam::operator<<(Ostream& os, const std::wstring& wstr)
{
for
(
std::wstring::const_iterator iter = wstr.begin();
iter != wstr.end();
++iter
)
for (auto iter = wstr.cbegin(); iter != wstr.cend(); ++iter)
{
os << *iter;
}
......
......@@ -120,14 +120,9 @@ bool Foam::fileName::clean()
string::size_type nChar = top+1;
string::size_type maxLen = this->size();
for
(
string::size_type src = nChar;
src < maxLen;
/*nil*/
)
for (string::size_type src = nChar; src < maxLen; /*nil*/)
{
char c = operator[](src++);
const char c = operator[](src++);
if (prev == '/')
{
......@@ -148,12 +143,12 @@ bool Foam::fileName::clean()
// Peek at the next character
char c1 = operator[](src);
const char c1 = operator[](src);
// Found '/./' - skip it
if (c1 == '/')
{
src++;
++src;
continue;
}
......
......@@ -108,7 +108,7 @@ public:
inline fileName(const fileName& fn);
//- Construct as copy of word
inline fileName(const word& s);
inline fileName(const word& w);
//- Construct as copy of string
inline fileName(const string& s, const bool doStripInvalid=true);
......
......@@ -23,12 +23,13 @@ License
\*---------------------------------------------------------------------------*/
#include <iostream> // for std::cerr
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
inline void Foam::fileName::stripInvalid()
{
// skip stripping unless debug is active to avoid
// costly operations
// Skip stripping unless debug is active (to avoid costly operations)
if (debug && string::stripInvalid<fileName>(*this))
{
std::cerr
......
......@@ -111,6 +111,11 @@ public:
// Member functions
//- Is this character valid for a keyType?
// This is largely identical with what word accepts, but also
// permit brace-brackets, which are valid for some regexs.
inline static bool valid(char c);
//- Treat as a pattern rather than a literal string?
inline bool isPattern() const;
......
......@@ -23,6 +23,21 @@ License
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
inline bool Foam::keyType::valid(char c)
{
return
(
!isspace(c)
&& c != '"' // string quote
&& c != '\'' // string quote
&& c != '/' // path separator
&& c != ';' // end statement
);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::keyType::keyType()
......
......@@ -46,8 +46,8 @@ inline Foam::CStringList::CStringList()
:
argc_(0),
len_(0),
argv_(0),
data_(0)
argv_(nullptr),
data_(nullptr)
{}
......@@ -69,12 +69,12 @@ inline void Foam::CStringList::clear()
if (data_)
{
delete[] data_;
data_ = 0;
data_ = nullptr;
}
if (argv_)
{
delete[] argv_;
argv_ = 0;
argv_ = nullptr;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment