Commit 8e4f0152 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

Merge branch 'style-string-methods' into 'develop'

Style string methods

See merge request !136
parents 5ca19c29 46a2a736
......@@ -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;
}
......
......@@ -104,30 +104,26 @@ Foam::fileName& Foam::fileName::toAbsolute()
}
bool Foam::fileName::clean()
bool Foam::fileName::clean(std::string& str)
{
// The top slash - we are never allowed to go above it
string::size_type top = this->find('/');
// Start with the top slash found - we are never allowed to go above it
char prev = '/';
auto top = str.find(prev);
// No slashes - nothing to do
if (top == string::npos)
if (top == std::string::npos)
{
return false;
}
// Start with the '/' found:
char prev = '/';
string::size_type nChar = top+1;
string::size_type maxLen = this->size();
for
(
string::size_type src = nChar;
src < maxLen;
/*nil*/
)
// Number of output characters
std::string::size_type nChar = top+1;
const string::size_type maxLen = str.size();
for (string::size_type src = nChar; src < maxLen; /*nil*/)
{
char c = operator[](src++);
const char c = str[src++];
if (prev == '/')
{
......@@ -137,28 +133,27 @@ bool Foam::fileName::clean()
continue;
}
// Could be '/./' or '/../'
// Could be "/./", "/../" or a trailing "/."
if (c == '.')
{
// Found trailing '/.' - skip it
// Trailing "/." - skip it
if (src >= maxLen)
{
continue;
break;
}
// Peek at the next character
char c1 = operator[](src);
const char c1 = str[src];
// Found '/./' - skip it
// Found "/./" - skip it
if (c1 == '/')
{
src++;
++src;
continue;
}
// It is '/..' or '/../'
if (c1 == '.' && (src+1 >= maxLen || operator[](src+1) == '/'))
// Trailing "/.." or intermediate "/../"
if (c1 == '.' && (src+1 >= maxLen || str[src+1] == '/'))
{
string::size_type parent;
......@@ -168,7 +163,7 @@ bool Foam::fileName::clean()
if
(
nChar > 2
&& (parent = this->rfind('/', nChar-2)) != string::npos
&& (parent = str.rfind('/', nChar-2)) != string::npos
&& parent >= top
)
{
......@@ -184,47 +179,60 @@ bool Foam::fileName::clean()
}
}
}
operator[](nChar++) = prev = c;
str[nChar++] = prev = c;
}
// Remove trailing slash
if (nChar > 1 && operator[](nChar-1) == '/')
if (nChar > 1 && str[nChar-1] == '/')
{
nChar--;
}
this->resize(nChar);
str.resize(nChar);
return (nChar != maxLen);
}
bool Foam::fileName::clean()
{
return fileName::clean(*this);
}
Foam::fileName Foam::fileName::clean() const
{
fileName fName(*this);
fName.clean();
return fName;
fileName cleaned(*this);
fileName::clean(cleaned);
return cleaned;
}
Foam::word Foam::fileName::name() const
std::string Foam::fileName::name(const std::string& str)
{
const size_type i = rfind('/');
const auto beg = str.rfind('/');
if (i == npos)
if (beg == npos)
{
return *this;
return str;
}
else
{
return substr(i+1);
return str.substr(beg+1);
}
}
Foam::word Foam::fileName::nameLessExt() const
Foam::word Foam::fileName::name() const
{
return fileName::name(*this);
}
std::string Foam::fileName::nameLessExt(const std::string& str)
{
size_type beg = rfind('/');
size_type beg = str.rfind('/');
size_type dot = str.rfind('.');
if (beg == npos)
{
......@@ -235,7 +243,6 @@ Foam::word Foam::fileName::nameLessExt() const
++beg;
}
size_type dot = rfind('.');
if (dot != npos && dot <= beg)
{
dot = npos;
......@@ -243,18 +250,24 @@ Foam::word Foam::fileName::nameLessExt() const
if (dot == npos)
{
return substr(beg, npos);
return str.substr(beg);
}
else
{
return substr(beg, dot - beg);
return str.substr(beg, dot - beg);
}
}
Foam::fileName Foam::fileName::path() const
Foam::word Foam::fileName::nameLessExt() const
{
const size_type i = rfind('/');
return nameLessExt(*this);
}
std::string Foam::fileName::path(const std::string& str)
{
const auto i = str.rfind('/');
if (i == npos)
{
......@@ -262,7 +275,7 @@ Foam::fileName Foam::fileName::path() const
}
else if (i)
{
return substr(0, i);
return str.substr(0, i);
}
else
{
......@@ -271,9 +284,15 @@ Foam::fileName Foam::fileName::path() const
}
Foam::fileName Foam::fileName::path() const
{
return path(*this);
}
Foam::fileName Foam::fileName::lessExt() const
{
const size_type i = find_ext();
const auto i = find_ext();
if (i == npos)
{
......@@ -330,7 +349,7 @@ Foam::wordList Foam::fileName::components(const char delimiter) const
// Avoid empty trailing element
if (beg < size())
{
wrdList.append(substr(beg, npos));
wrdList.append(substr(beg));
}
// Transfer to wordList
......
......@@ -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);
......@@ -135,26 +135,42 @@ public:
//- Is this character valid for a fileName?
inline static bool valid(char c);
//- Cleanup file name
//- Cleanup filename
//
// * Removes repeated slashes
// Removes trailing \c /
// \verbatim
// / --> /
// /abc/ --> /abc
// \endverbatim
//
// Removes repeated slashes
// \verbatim
// /abc////def --> /abc/def
// \endverbatim
//
// * Removes '/./'
// /abc/def/./ghi/. --> /abc/def/./ghi
// Removes \c /./ (current directory)
// \verbatim
// /abc/def/./ghi/. --> /abc/def/ghi
// abc/def/./ --> abc/def
// ./abc/ --> ./abc
// \endverbatim
//
// * Removes '/../'
// Removes \c /../ (parent directory)
// \verbatim
// /abc/def/../ghi/jkl/nmo/.. --> /abc/ghi/jkl
// abc/../def/ghi/../jkl --> abc/../def/jkl
// \endverbatim
//
// * Removes trailing '/'
//
// \return True if any contents changed
// \return True if the content changed
static bool clean(std::string& str);
//- Cleanup filename inplace
// \return True if any contents changed
bool clean();
//- Cleanup file name
// eg, remove repeated slashes, etc.
//- Cleanup filename
// \return cleaned copy of fileName
fileName clean() const;
......@@ -164,6 +180,9 @@ public:
// LINK (only if followLink=false)
Type type(const bool followLink = true) const;
//- Return true if string starts with a '/'
inline static bool isAbsolute(const std::string& str);
//- Return true if file name is absolute (starts with a '/')
inline bool isAbsolute() const;
......@@ -174,19 +193,28 @@ public:
// Decomposition
//- Return basename (part beyond last /), including its extension
// The result normally coresponds to a Foam::word
//
// Behaviour compared to /usr/bin/basename:
// \verbatim
// input name() basename
// ----- ------ --------
// "foo" "foo" "foo"
// "/" "" "/"
// "/foo" "foo" "foo"
// "foo/bar" "bar" "bar"
// "/foo/bar" "bar" "bar"
// "/foo/bar/" "" "bar"
// \endverbatim
static std::string name(const std::string& str);
//- Return basename (part beyond last /), including its extension
word name() const;
//- Return basename, without extension
// The result normally coresponds to a Foam::word
static std::string nameLessExt(const std::string& str);
//- Return basename, without extension
word nameLessExt() const;
......@@ -199,17 +227,22 @@ public:
}
//- Return directory path name (part before last /)
// The result normally coresponds to a Foam::fileName
//
// Behaviour compared to /usr/bin/dirname:
// \verbatim
// input path() dirname
// ----- ------ -------
// "foo" "." "."
// "/" "/" "/"
// "/foo" "/" "foo"
// "foo/bar" "foo" "foo"
// "/foo/bar" "/foo" "/foo"
// "/foo/bar/" "/foo/bar/" "/foo"
// \endverbatim
static std::string path(const std::string& str);
//- Return directory path name (part before last /)
fileName path() const;
//- Return file name without extension (part before last .)
......@@ -242,11 +275,11 @@ public:
// \verbatim
// Input components()
// ----- ------
// "foo" 1("foo")
// "/foo" 1("foo")
// "foo/bar" 2("foo", "bar")
// "/foo/bar" 2("foo", "bar")
// "/foo/bar/" 2("foo", "bar")
// "foo" ("foo")
// "/foo" ("foo")