ENH: add move/swap semantics to string types and regExp
- move append() single element to List and DynamicList ENH: add stringOps::count to avoid unnecessary string conversions
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -44,7 +44,7 @@ int main(int argc, char *argv[])
|
||||
Info<< "Test expressions:" << rawList << endl;
|
||||
IOobject::writeDivider(Info) << endl;
|
||||
|
||||
List<string> groups;
|
||||
List<std::string> groups;
|
||||
|
||||
// Report matches:
|
||||
forAll(rawList, elemI)
|
||||
@ -74,7 +74,23 @@ int main(int argc, char *argv[])
|
||||
Info<< "false";
|
||||
}
|
||||
}
|
||||
|
||||
Info<< endl;
|
||||
|
||||
if (false)
|
||||
{
|
||||
regExp re2(std::move(re));
|
||||
Info<<"move construct: " << re.exists() << "/" << re2.exists()
|
||||
<< endl;
|
||||
|
||||
re = std::move(re2);
|
||||
Info<<"move assign: " << re.exists() << "/" << re2.exists()
|
||||
<< endl;
|
||||
|
||||
re.swap(re2);
|
||||
Info<<"swap: " << re.exists() << "/" << re2.exists()
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
|
||||
Info<< nl << "test regExp(const char*) ..." << endl;
|
||||
|
||||
@ -69,6 +69,65 @@ int main(int argc, char *argv[])
|
||||
Info<<"trimRight: " << stringOps::trimRight(test) << endl;
|
||||
Info<<"trim: " << stringOps::trim(test) << endl;
|
||||
|
||||
if (false)
|
||||
{
|
||||
Info<<"test move construct - string size:" << test.size() << nl;
|
||||
string test2(std::move(test));
|
||||
|
||||
Info<<"input size:" << test.size() << nl;
|
||||
Info<<"moved size:" << test2.size() << nl;
|
||||
|
||||
Info<<"test move assign - string sizes:"
|
||||
<< test.size() << "/" << test2.size() << nl;
|
||||
|
||||
test = std::move(test2);
|
||||
|
||||
Info<<"input size:" << test.size() << nl;
|
||||
Info<<"moved size:" << test2.size() << nl;
|
||||
}
|
||||
|
||||
if (false)
|
||||
{
|
||||
std::string str("some text");
|
||||
|
||||
Info<<"test move construct to string:" << str.size() << nl;
|
||||
|
||||
Foam::string test2(std::move(str));
|
||||
|
||||
Info<<"input/moved sizes:" << str.size() << "/" << test2.size() << nl;
|
||||
|
||||
str = std::move(test2);
|
||||
|
||||
Info<<"test move assign - sizes:"
|
||||
<< str.size() << "/" << test2.size() << nl;
|
||||
}
|
||||
|
||||
if (false)
|
||||
{
|
||||
Foam::string str("thisIsAWord");
|
||||
|
||||
Info<<"test move construct to word:" << str.size() << nl;
|
||||
|
||||
word test2(std::move(str));
|
||||
|
||||
Info<<"input/moved sizes:" << str.size() << "/" << test2.size() << nl;
|
||||
|
||||
str = std::move(test2);
|
||||
|
||||
Info<<"test move assign - sizes:"
|
||||
<< str.size() << "/" << test2.size() << nl;
|
||||
|
||||
// move back
|
||||
test2.swap(str);
|
||||
|
||||
Info<<"test move assign - sizes:"
|
||||
<< str.size() << "/" << test2.size() << nl;
|
||||
|
||||
string str2(std::move(test2));
|
||||
Info<<"input/moved sizes:" << test2.size() << "/" << str2.size() << nl;
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
fileName test1("libFooBar.so");
|
||||
|
||||
|
||||
@ -56,6 +56,56 @@ int main(int argc, char *argv[])
|
||||
{"file[a-b]", wordRe::REGEX},
|
||||
};
|
||||
|
||||
if (true)
|
||||
{
|
||||
Info<<"keyType: " << keyre << endl;
|
||||
|
||||
keyType key2(std::move(keyre));
|
||||
|
||||
Info<<"move construct: <" << keyre << "> <" << key2 << ">" << endl;
|
||||
|
||||
keyre = std::move(key2);
|
||||
|
||||
Info<<"move assign: <" << keyre << "> <" << key2 << ">" << endl;
|
||||
|
||||
keyType key3;
|
||||
|
||||
keyre.swap(key3);
|
||||
|
||||
Info<<"swap: <" << keyre << "> <" << key3 << ">" << endl;
|
||||
|
||||
keyre = std::move(key3);
|
||||
Info<<"move assign: <" << keyre << "> <" << key3 << ">" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (false)
|
||||
{
|
||||
wordRe keyre("y.*", wordRe::REGEX);
|
||||
|
||||
Info<<"wordRe: " << keyre << endl;
|
||||
|
||||
wordRe key2(std::move(keyre));
|
||||
|
||||
Info<<"keyTypes: " << keyre << " " << key2 << endl;
|
||||
|
||||
keyre = std::move(key2);
|
||||
|
||||
Info<<"keyTypes: " << keyre << " " << key2 << endl;
|
||||
|
||||
wordRe key3;
|
||||
|
||||
keyre.swap(key3);
|
||||
|
||||
Info<<"keyTypes: <" << keyre << "> <" << key3 << ">" << endl;
|
||||
|
||||
keyre = std::move(key3);
|
||||
Info<<"keyTypes: <" << keyre << "> <" << key3 << ">" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
wordRes wrelist(wordrelist);
|
||||
|
||||
Info<< "re-list:" << wrelist() << endl;
|
||||
@ -76,7 +126,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
wre = "this .* file";
|
||||
|
||||
Info<<"substring: " << wre(4) << endl;
|
||||
Info<<"substring: " << wre.substr(4) << endl;
|
||||
|
||||
wre.info(Info) << endl;
|
||||
wre = s1;
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -113,6 +113,9 @@ public:
|
||||
//- Construct from string, optionally ignore case
|
||||
inline regExp(const std::string& pattern, bool ignoreCase);
|
||||
|
||||
//- Move construct
|
||||
inline regExp(regExp&& rgx);
|
||||
|
||||
|
||||
//- Destructor
|
||||
inline ~regExp();
|
||||
@ -146,6 +149,9 @@ public:
|
||||
// \return True if expression had existed prior to the clear.
|
||||
bool clear();
|
||||
|
||||
//- Swap contents
|
||||
inline void swap(regExp& rgx);
|
||||
|
||||
|
||||
// Matching/Searching
|
||||
|
||||
@ -177,6 +183,10 @@ public:
|
||||
//- Assign and compile pattern from string
|
||||
// Always case sensitive
|
||||
inline void operator=(const std::string& pattern);
|
||||
|
||||
//- Move assignment
|
||||
inline void operator=(regExp&& rgx);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -23,6 +23,8 @@ License
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
|
||||
|
||||
@ -78,6 +80,14 @@ inline Foam::regExp::regExp(const std::string& pattern, bool ignoreCase)
|
||||
}
|
||||
|
||||
|
||||
inline Foam::regExp::regExp(regExp&& rgx)
|
||||
:
|
||||
preg_(rgx.preg_)
|
||||
{
|
||||
rgx.preg_ = nullptr;
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::regExp::~regExp()
|
||||
@ -112,6 +122,12 @@ inline bool Foam::regExp::search(const std::string& text) const
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::regExp::swap(regExp& rgx)
|
||||
{
|
||||
std::swap(preg_, rgx.preg_);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::regExp::operator()(const std::string& text) const
|
||||
@ -132,4 +148,11 @@ inline void Foam::regExp::operator=(const std::string& pattern)
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::regExp::operator=(regExp&& rgx)
|
||||
{
|
||||
clear();
|
||||
swap(rgx);
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -249,6 +249,9 @@ public:
|
||||
//- Append an element to the end of this list.
|
||||
inline DynamicList<T, SizeMin>& append(const T& val);
|
||||
|
||||
//- Move append an element
|
||||
inline DynamicList<T, SizeMin>& append(T&& val);
|
||||
|
||||
//- Append another list to the end of this list.
|
||||
inline DynamicList<T, SizeMin>& append(const UList<T>& lst);
|
||||
|
||||
|
||||
@ -492,7 +492,22 @@ Foam::DynamicList<T, SizeMin>::append
|
||||
const label idx = List<T>::size();
|
||||
setSize(idx + 1);
|
||||
|
||||
this->operator[](idx) = val;
|
||||
this->operator[](idx) = val; // copy element
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template<class T, int SizeMin>
|
||||
inline Foam::DynamicList<T, SizeMin>&
|
||||
Foam::DynamicList<T, SizeMin>::append
|
||||
(
|
||||
T&& val
|
||||
)
|
||||
{
|
||||
const label idx = List<T>::size();
|
||||
setSize(idx + 1);
|
||||
|
||||
this->operator[](idx) = std::move(val); // move assign element
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -510,12 +525,13 @@ Foam::DynamicList<T, SizeMin>::append
|
||||
<< "Attempted appending to self" << abort(FatalError);
|
||||
}
|
||||
|
||||
label nextFree = List<T>::size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = List<T>::size();
|
||||
|
||||
setSize(idx + lst.size());
|
||||
|
||||
for (const T& val : lst)
|
||||
{
|
||||
this->operator[](nextFree++) = val;
|
||||
this->operator[](idx++) = val; // copy element
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -529,12 +545,12 @@ Foam::DynamicList<T, SizeMin>::append
|
||||
const FixedList<T, FixedSize>& lst
|
||||
)
|
||||
{
|
||||
label nextFree = List<T>::size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = List<T>::size();
|
||||
setSize(idx + lst.size());
|
||||
|
||||
for (const T& val : lst)
|
||||
{
|
||||
this->operator[](nextFree++) = val;
|
||||
this->operator[](idx++) = val; // copy element
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -547,12 +563,13 @@ Foam::DynamicList<T, SizeMin>::append
|
||||
std::initializer_list<T> lst
|
||||
)
|
||||
{
|
||||
label nextFree = List<T>::size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = List<T>::size();
|
||||
|
||||
setSize(idx + lst.size());
|
||||
|
||||
for (const T& val : lst)
|
||||
{
|
||||
this->operator[](nextFree++) = val;
|
||||
this->operator[](idx++) = val; // copy element
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -565,12 +582,14 @@ Foam::DynamicList<T, SizeMin>::append
|
||||
const UIndirectList<T>& lst
|
||||
)
|
||||
{
|
||||
label nextFree = List<T>::size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = List<T>::size();
|
||||
const label n = lst.size();
|
||||
|
||||
forAll(lst, elemI)
|
||||
setSize(idx + n);
|
||||
|
||||
for (label i=0; i<n; ++i)
|
||||
{
|
||||
this->operator[](nextFree++) = lst[elemI];
|
||||
this->operator[](idx++) = lst[i]; // copy element
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
@ -589,12 +608,13 @@ Foam::DynamicList<T, SizeMin>::append
|
||||
<< "Attempted appending to self" << abort(FatalError);
|
||||
}
|
||||
|
||||
label nextFree = List<T>::size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = List<T>::size();
|
||||
|
||||
setSize(idx + lst.size());
|
||||
|
||||
for (T& val : lst)
|
||||
{
|
||||
Foam::Swap(this->operator[](nextFree++), val);
|
||||
Foam::Swap(this->operator[](idx++), val); // moved content
|
||||
}
|
||||
|
||||
lst.clear();
|
||||
@ -779,11 +799,13 @@ inline void Foam::DynamicList<T, SizeMin>::operator=
|
||||
const FixedList<T, FixedSize>& lst
|
||||
)
|
||||
{
|
||||
setSize(lst.size());
|
||||
const label n = lst.size();
|
||||
|
||||
forAll(lst, i)
|
||||
setSize(n);
|
||||
|
||||
for (label i=0; i<n; ++i)
|
||||
{
|
||||
this->operator[](i) = lst[i];
|
||||
this->operator[](i) = lst[i]; // copy element
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -226,7 +226,10 @@ public:
|
||||
//- Append an element at the end of the list
|
||||
inline void append(const T& val);
|
||||
|
||||
//- Append a List at the end of this list
|
||||
//- Move append an element at the end of the list
|
||||
inline void append(T&& val);
|
||||
|
||||
//- Append a List to the end of this list
|
||||
inline void append(const UList<T>& lst);
|
||||
|
||||
//- Append a UIndirectList at the end of this list
|
||||
|
||||
@ -172,7 +172,17 @@ inline Foam::Xfer<Foam::List<T>> Foam::List<T>::xfer()
|
||||
template<class T>
|
||||
inline void Foam::List<T>::append(const T& val)
|
||||
{
|
||||
setSize(this->size()+1, val);
|
||||
setSize(this->size() + 1, val); // copy element
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void Foam::List<T>::append(T&& val)
|
||||
{
|
||||
const label idx = this->size();
|
||||
setSize(idx + 1);
|
||||
|
||||
this->operator[](idx) = std::move(val); // move assign element
|
||||
}
|
||||
|
||||
|
||||
@ -185,12 +195,14 @@ inline void Foam::List<T>::append(const UList<T>& lst)
|
||||
<< "attempted appending to self" << abort(FatalError);
|
||||
}
|
||||
|
||||
label nextFree = this->size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = this->size();
|
||||
const label n = lst.size();
|
||||
|
||||
forAll(lst, i)
|
||||
setSize(idx + n);
|
||||
|
||||
for (label i=0; i<n; ++i)
|
||||
{
|
||||
this->operator[](nextFree++) = lst[i];
|
||||
this->operator[](idx++) = lst[i]; // copy element
|
||||
}
|
||||
}
|
||||
|
||||
@ -198,12 +210,14 @@ inline void Foam::List<T>::append(const UList<T>& lst)
|
||||
template<class T>
|
||||
inline void Foam::List<T>::append(const UIndirectList<T>& lst)
|
||||
{
|
||||
label nextFree = this->size();
|
||||
setSize(nextFree + lst.size());
|
||||
label idx = this->size();
|
||||
const label n = lst.size();
|
||||
|
||||
forAll(lst, i)
|
||||
setSize(idx + n);
|
||||
|
||||
for (label i=0; i<n; ++i)
|
||||
{
|
||||
this->operator[](nextFree++) = lst[i];
|
||||
this->operator[](idx++) = lst[i]; // copy element
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -92,7 +92,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
|
||||
}
|
||||
else
|
||||
{
|
||||
// uniform content (delimiter == token::BEGIN_BLOCK)
|
||||
// Uniform content (delimiter == token::BEGIN_BLOCK)
|
||||
|
||||
T element;
|
||||
is >> element;
|
||||
@ -141,7 +141,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
|
||||
// Putback the opening bracket
|
||||
is.putBack(firstToken);
|
||||
|
||||
// Now read as a singly-linked list
|
||||
// Read as a singly-linked list
|
||||
SLList<T> sll(is);
|
||||
|
||||
// Convert the singly-linked list to this list
|
||||
@ -176,8 +176,11 @@ Foam::List<T> Foam::readList(Istream& is)
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
|
||||
// Read via a singly-linked list
|
||||
L = SLList<T>(is);
|
||||
// Read as singly-linked list
|
||||
SLList<T> sll(is);
|
||||
|
||||
// Convert the singly-linked list to this list
|
||||
L = sll;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@ -24,8 +24,9 @@ License
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "error.H"
|
||||
#include "OSstream.H"
|
||||
#include "token.H"
|
||||
#include "OSstream.H"
|
||||
#include "stringOps.H"
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -52,7 +53,7 @@ Foam::Ostream& Foam::OSstream::write(const char c)
|
||||
os_ << c;
|
||||
if (c == token::NL)
|
||||
{
|
||||
lineNumber_++;
|
||||
++lineNumber_;
|
||||
}
|
||||
setState(os_.rdstate());
|
||||
return *this;
|
||||
@ -61,7 +62,7 @@ Foam::Ostream& Foam::OSstream::write(const char c)
|
||||
|
||||
Foam::Ostream& Foam::OSstream::write(const char* str)
|
||||
{
|
||||
lineNumber_ += string(str).count(token::NL);
|
||||
lineNumber_ += stringOps::count(str, token::NL);
|
||||
os_ << str;
|
||||
setState(os_.rdstate());
|
||||
return *this;
|
||||
@ -169,7 +170,7 @@ Foam::Ostream& Foam::OSstream::writeQuoted
|
||||
else
|
||||
{
|
||||
// output unquoted string, only advance line number on newline
|
||||
lineNumber_ += string(str).count(token::NL);
|
||||
lineNumber_ += stringOps::count(str, token::NL);
|
||||
os_ << str;
|
||||
}
|
||||
|
||||
|
||||
@ -359,10 +359,8 @@ std::string Foam::fileName::path(const std::string& str)
|
||||
{
|
||||
return str.substr(0, i);
|
||||
}
|
||||
else
|
||||
{
|
||||
return "/";
|
||||
}
|
||||
|
||||
return "/";
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -72,7 +72,7 @@ class keyType
|
||||
|
||||
// Private Member Functions
|
||||
|
||||
//- Disallow assignments where we cannot determine string/word type
|
||||
//- No assignment where we cannot determine string/word type
|
||||
void operator=(const std::string&) = delete;
|
||||
|
||||
public:
|
||||
@ -88,22 +88,34 @@ public:
|
||||
//- Construct null
|
||||
inline keyType();
|
||||
|
||||
//- Construct as copy, retaining type (literal or regex)
|
||||
//- Copy construct, retaining type (literal or regex)
|
||||
inline keyType(const keyType& s);
|
||||
|
||||
//- Construct as copy of word. Not treated as a regular expression
|
||||
//- Copy construct from word. Not treated as a regular expression
|
||||
inline keyType(const word& s);
|
||||
|
||||
//- Construct as copy of string. Treat as regular expression.
|
||||
//- Copy construct from string. Treat as regular expression.
|
||||
inline keyType(const string& s);
|
||||
|
||||
//- Construct as copy of character array.
|
||||
// Not treated as a regular expression
|
||||
inline keyType(const char* s);
|
||||
|
||||
//- Construct as copy of std::string with specified treatment
|
||||
//- Copy construct from std::string with specified treatment
|
||||
inline keyType(const std::string& s, const bool isPattern);
|
||||
|
||||
//- Move construct, retaining type (literal or regex)
|
||||
inline keyType(keyType&& s);
|
||||
|
||||
//- Move construct from word. Not treated as a regular expression
|
||||
inline keyType(word&& s);
|
||||
|
||||
//- Move construct from string. Treat as regular expression.
|
||||
inline keyType(string&& s);
|
||||
|
||||
//- Move construct from std::string with specified treatment
|
||||
inline keyType(std::string&& s, const bool isPattern);
|
||||
|
||||
//- Construct from Istream
|
||||
// Treat as regular expression if surrounded by quotation marks.
|
||||
keyType(Istream& is);
|
||||
@ -119,6 +131,9 @@ public:
|
||||
//- Treat as a pattern rather than a literal string?
|
||||
inline bool isPattern() const;
|
||||
|
||||
//- Swap contents
|
||||
inline void swap(keyType& s);
|
||||
|
||||
//- Smart match as regular expression or as a string.
|
||||
// Optionally force a literal match only
|
||||
bool match(const std::string& text, bool literal = false) const;
|
||||
@ -126,27 +141,25 @@ public:
|
||||
|
||||
// Member operators
|
||||
|
||||
//- Avoid masking the normal operator()
|
||||
using word::operator();
|
||||
|
||||
//- Perform smart match on text
|
||||
inline bool operator()(const std::string& text) const;
|
||||
|
||||
|
||||
// Assignment
|
||||
|
||||
//- Assignment operator, retaining type (literal or regex)
|
||||
//- Copy assignment, retaining type (literal or regex)
|
||||
inline void operator=(const keyType& s);
|
||||
|
||||
//- Assign as word, not treated as a regular expression.
|
||||
inline void operator=(const word& s);
|
||||
|
||||
//- Assign as regular expression
|
||||
//- Assign from Foam::string as regular expression
|
||||
inline void operator=(const string& s);
|
||||
|
||||
//- Assign as word, not treated as a regular expression.
|
||||
inline void operator=(const char* s);
|
||||
|
||||
//- Move assignment, retaining type (literal or regex)
|
||||
inline void operator=(keyType&& s);
|
||||
|
||||
|
||||
// IOstream operators
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
||||
\\ / O peration |
|
||||
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
|
||||
\\/ M anipulation |
|
||||
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
|
||||
-------------------------------------------------------------------------------
|
||||
License
|
||||
This file is part of OpenFOAM.
|
||||
@ -23,6 +23,8 @@ License
|
||||
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::keyType::valid(char c)
|
||||
@ -75,17 +77,44 @@ inline Foam::keyType::keyType(const char* s)
|
||||
{}
|
||||
|
||||
|
||||
inline Foam::keyType::keyType
|
||||
(
|
||||
const std::string& s,
|
||||
const bool isPattern
|
||||
)
|
||||
inline Foam::keyType::keyType(const std::string& s, const bool isPattern)
|
||||
:
|
||||
word(s, false),
|
||||
isPattern_(isPattern)
|
||||
{}
|
||||
|
||||
|
||||
|
||||
inline Foam::keyType::keyType(keyType&& s)
|
||||
:
|
||||
word(std::move(static_cast<word&>(s)), false),
|
||||
isPattern_(s.isPattern())
|
||||
{
|
||||
s.isPattern_ = false;
|
||||
}
|
||||
|
||||
|
||||
inline Foam::keyType::keyType(word&& s)
|
||||
:
|
||||
word(std::move(s), false),
|
||||
isPattern_(false)
|
||||
{}
|
||||
|
||||
|
||||
inline Foam::keyType::keyType(string&& s)
|
||||
:
|
||||
word(std::move(s), false),
|
||||
isPattern_(true)
|
||||
{}
|
||||
|
||||
|
||||
inline Foam::keyType::keyType(std::string&& s, const bool isPattern)
|
||||
:
|
||||
word(std::move(s), false),
|
||||
isPattern_(isPattern)
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::keyType::isPattern() const
|
||||
@ -94,6 +123,13 @@ inline bool Foam::keyType::isPattern() const
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::keyType::swap(keyType& s)
|
||||
{
|
||||
word::swap(static_cast<word&>(s));
|
||||
std::swap(isPattern_, s.isPattern_);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::keyType::operator()(const std::string& text) const
|
||||
@ -104,30 +140,37 @@ inline bool Foam::keyType::operator()(const std::string& text) const
|
||||
|
||||
inline void Foam::keyType::operator=(const keyType& s)
|
||||
{
|
||||
string::operator=(s); // Bypass checking
|
||||
string::operator=(s); // Bypass char checking
|
||||
isPattern_ = s.isPattern_;
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::keyType::operator=(const word& s)
|
||||
{
|
||||
string::operator=(s); // Bypass checking
|
||||
string::operator=(s); // Bypass char checking
|
||||
isPattern_ = false;
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::keyType::operator=(const string& s)
|
||||
{
|
||||
string::operator=(s); // Bypass checking
|
||||
string::operator=(s); // Bypass char checking
|
||||
isPattern_ = true;
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::keyType::operator=(const char* s)
|
||||
{
|
||||
string::operator=(s); // Bypass checking
|
||||
string::operator=(s); // Bypass char checking
|
||||
isPattern_ = false;
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::keyType::operator=(keyType&& s)
|
||||
{
|
||||
clear();
|
||||
swap(s);
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -101,17 +101,7 @@ bool Foam::string::hasExt(const wordRe& ending) const
|
||||
|
||||
Foam::string::size_type Foam::string::count(const char c) const
|
||||
{
|
||||
size_type nChar = 0;
|
||||
|
||||
for (auto iter = cbegin(); iter != cend(); ++iter)
|
||||
{
|
||||
if (*iter == c)
|
||||
{
|
||||
++nChar;
|
||||
}
|
||||
}
|
||||
|
||||
return nChar;
|
||||
return stringOps::count(*this, c);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -153,13 +153,18 @@ public:
|
||||
//- Construct from copies of a single character
|
||||
inline string(const size_type len, const char c);
|
||||
|
||||
//- Move construct from std::string
|
||||
inline string(std::string&& str);
|
||||
|
||||
//- Construct from Istream
|
||||
string(Istream& is);
|
||||
|
||||
|
||||
// Member Functions
|
||||
|
||||
//- Count and return the number of a given character in the string
|
||||
//- Count the number of occurences 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?
|
||||
@ -194,7 +199,7 @@ public:
|
||||
using std::string::replace;
|
||||
|
||||
//- Replace first occurence of sub-string oldStr with newStr,
|
||||
// beginning at start
|
||||
//- beginning at start
|
||||
string& replace
|
||||
(
|
||||
const string& oldStr,
|
||||
@ -203,7 +208,7 @@ public:
|
||||
);
|
||||
|
||||
//- Replace all occurences of sub-string oldStr with newStr,
|
||||
// beginning at start. This is a no-op if oldStr is empty.
|
||||
//- beginning at start. This is a no-op if oldStr is empty.
|
||||
string& replaceAll
|
||||
(
|
||||
const string& oldStr,
|
||||
|
||||
@ -99,6 +99,12 @@ inline Foam::string::string(const size_type len, const char c)
|
||||
{}
|
||||
|
||||
|
||||
inline Foam::string::string(std::string&& str)
|
||||
:
|
||||
std::string(std::move(str))
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
template<class String>
|
||||
|
||||
@ -132,6 +132,47 @@ static inline int findParameterAlternative
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
std::string::size_type Foam::stringOps::count
|
||||
(
|
||||
const std::string& str,
|
||||
const char c
|
||||
)
|
||||
{
|
||||
std::string::size_type n = 0;
|
||||
|
||||
for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
|
||||
{
|
||||
if (*iter == c)
|
||||
{
|
||||
++n;
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
std::string::size_type Foam::stringOps::count(const char* str, const char c)
|
||||
{
|
||||
if (!str)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string::size_type n = 0;
|
||||
|
||||
for (const char *iter = str; *iter; ++iter)
|
||||
{
|
||||
if (*iter == c)
|
||||
{
|
||||
++n;
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
Foam::string Foam::stringOps::expand
|
||||
(
|
||||
const string& original,
|
||||
|
||||
@ -54,6 +54,14 @@ namespace Foam
|
||||
|
||||
namespace stringOps
|
||||
{
|
||||
//- Count the number of occurences of the specified character
|
||||
std::string::size_type count(const std::string& str, const char c);
|
||||
|
||||
//- Count the number of occurences of the specified character
|
||||
// Correctly handles nullptr.
|
||||
std::string::size_type count(const char* str, const char c);
|
||||
|
||||
|
||||
//- Expand occurences of variables according to the mapping
|
||||
// Expansion includes:
|
||||
// -# variables
|
||||
|
||||
@ -25,6 +25,7 @@ License
|
||||
|
||||
#include "word.H"
|
||||
#include "debug.H"
|
||||
#include <cctype>
|
||||
|
||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||
|
||||
@ -77,10 +78,8 @@ Foam::word Foam::word::lessExt() const
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
else
|
||||
{
|
||||
return substr(0, i);
|
||||
}
|
||||
|
||||
return substr(0, i);
|
||||
}
|
||||
|
||||
|
||||
@ -109,4 +108,28 @@ bool Foam::word::hasExt(const wordRe& ending) const
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
|
||||
|
||||
Foam::word Foam::operator&(const word& a, const word& b)
|
||||
{
|
||||
if (a.size())
|
||||
{
|
||||
if (b.size())
|
||||
{
|
||||
// Two non-empty words: can concatenate and perform camel case
|
||||
word camelCase(a + b);
|
||||
camelCase[a.size()] = char(toupper(b[0]));
|
||||
|
||||
return camelCase;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
// Or, if the first string is empty (or both are empty)
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -31,6 +31,7 @@ Description
|
||||
semicolons or brace brackets. Words are delimited by whitespace.
|
||||
|
||||
SourceFiles
|
||||
wordI.H
|
||||
word.C
|
||||
wordIO.C
|
||||
|
||||
@ -48,7 +49,6 @@ namespace Foam
|
||||
|
||||
// Forward declaration of friend functions and operators
|
||||
class word;
|
||||
inline word operator&(const word& a, const word& b);
|
||||
Istream& operator>>(Istream& is, word& w);
|
||||
Ostream& operator<<(Ostream& os, const word& w);
|
||||
|
||||
@ -84,7 +84,7 @@ public:
|
||||
//- Construct null
|
||||
inline word();
|
||||
|
||||
//- Construct as copy
|
||||
//- Copy construct
|
||||
inline word(const word& w);
|
||||
|
||||
//- Construct as copy of character array
|
||||
@ -98,12 +98,21 @@ public:
|
||||
const bool doStripInvalid
|
||||
);
|
||||
|
||||
//- Construct as copy of string
|
||||
//- Construct as copy of Foam::string
|
||||
inline word(const string& s, const bool doStripInvalid=true);
|
||||
|
||||
//- Construct as copy of std::string
|
||||
inline word(const std::string& s, const bool doStripInvalid=true);
|
||||
|
||||
//- Move construct
|
||||
inline word(word&& w);
|
||||
|
||||
//- Move construct from Foam::string
|
||||
inline word(string&& s, const bool doStripInvalid=true);
|
||||
|
||||
//- Move construct from std::string
|
||||
inline word(std::string&& s, const bool doStripInvalid=true);
|
||||
|
||||
//- Construct from Istream
|
||||
word(Istream& is);
|
||||
|
||||
@ -149,24 +158,26 @@ public:
|
||||
|
||||
// Assignment
|
||||
|
||||
//- Copy, no character validation required
|
||||
//- Copy assignment, no character validation required
|
||||
inline void operator=(const word& w);
|
||||
|
||||
//- Copy, stripping invalid characters
|
||||
//- Copy assignment from Foam::string, stripping invalid characters
|
||||
inline void operator=(const string& s);
|
||||
|
||||
//- Copy, stripping invalid characters
|
||||
//- Copy assignment from std::string, stripping invalid characters
|
||||
inline void operator=(const std::string& s);
|
||||
|
||||
//- Copy, stripping invalid characters
|
||||
inline void operator=(const char* s);
|
||||
|
||||
//- Move assignment
|
||||
inline void operator=(word&& w);
|
||||
|
||||
// Friend Operators
|
||||
//- Move assignment from Foam::string, stripping invalid characters
|
||||
inline void operator=(string&& s);
|
||||
|
||||
//- Join word a and b, capitalising the first letter of b
|
||||
// (so-called camelCase)
|
||||
friend word operator&(const word& a, const word& b);
|
||||
//- Move assignment from std::string, stripping invalid characters
|
||||
inline void operator=(std::string&& s);
|
||||
|
||||
|
||||
// IOstream operators
|
||||
@ -176,6 +187,13 @@ public:
|
||||
};
|
||||
|
||||
|
||||
// Global Operators
|
||||
|
||||
//- Join words as camelCase, capitalizing the first letter of b.
|
||||
// No effect if either argument is empty.
|
||||
word operator&(const word& a, const word& b);
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
} // End namespace Foam
|
||||
|
||||
@ -111,6 +111,34 @@ inline Foam::word::word
|
||||
}
|
||||
|
||||
|
||||
inline Foam::word::word(word&& w)
|
||||
:
|
||||
string(std::move(w))
|
||||
{}
|
||||
|
||||
|
||||
inline Foam::word::word(string&& s, const bool doStripInvalid)
|
||||
:
|
||||
string(std::move(s))
|
||||
{
|
||||
if (doStripInvalid)
|
||||
{
|
||||
stripInvalid();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline Foam::word::word(std::string&& s, const bool doStripInvalid)
|
||||
:
|
||||
string(std::move(s))
|
||||
{
|
||||
if (doStripInvalid)
|
||||
{
|
||||
stripInvalid();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::word::valid(char c)
|
||||
@ -148,6 +176,12 @@ inline void Foam::word::operator=(const word& w)
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::word::operator=(word&& w)
|
||||
{
|
||||
string::operator=(std::move(w));
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::word::operator=(const string& s)
|
||||
{
|
||||
string::operator=(s);
|
||||
@ -155,6 +189,13 @@ inline void Foam::word::operator=(const string& s)
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::word::operator=(string&& s)
|
||||
{
|
||||
string::operator=(std::move(s));
|
||||
stripInvalid();
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::word::operator=(const std::string& s)
|
||||
{
|
||||
string::operator=(s);
|
||||
@ -162,6 +203,13 @@ inline void Foam::word::operator=(const std::string& s)
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::word::operator=(std::string&& s)
|
||||
{
|
||||
string::operator=(std::move(s));
|
||||
stripInvalid();
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::word::operator=(const char* s)
|
||||
{
|
||||
string::operator=(s);
|
||||
@ -169,22 +217,4 @@ inline void Foam::word::operator=(const char* s)
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
|
||||
|
||||
inline Foam::word Foam::operator&(const word& a, const word& b)
|
||||
{
|
||||
if (b.size())
|
||||
{
|
||||
string ub = b;
|
||||
ub.string::operator[](0) = char(toupper(ub.string::operator[](0)));
|
||||
|
||||
return a + ub;
|
||||
}
|
||||
else
|
||||
{
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
@ -154,6 +154,9 @@ public:
|
||||
//- Construct as copy of word, use specified compile option
|
||||
inline wordRe(const word& str, const compOption);
|
||||
|
||||
//- Move construct
|
||||
inline wordRe(wordRe&& str);
|
||||
|
||||
//- Construct from Istream
|
||||
// Words are treated as literals, strings with an auto-test
|
||||
wordRe(Istream& is);
|
||||
@ -191,6 +194,9 @@ public:
|
||||
//- Clear string and regular expression
|
||||
inline void clear();
|
||||
|
||||
//- Swap contents
|
||||
inline void swap(wordRe& str);
|
||||
|
||||
|
||||
// Matching/Searching
|
||||
|
||||
@ -210,17 +216,11 @@ public:
|
||||
|
||||
// Member operators
|
||||
|
||||
//- Avoid masking the normal operator()
|
||||
using word::operator();
|
||||
|
||||
//- Perform smart match on text, as per match()
|
||||
inline bool operator()(const std::string& text) const;
|
||||
|
||||
|
||||
// Assignment
|
||||
|
||||
//- Copy wordRe and its type (literal or regex)
|
||||
// Always case sensitive
|
||||
//- Copy assignment, retaining type (literal or regex)
|
||||
inline void operator=(const wordRe& str);
|
||||
|
||||
//- Copy word, never a regular expression
|
||||
@ -242,6 +242,9 @@ public:
|
||||
// Always case sensitive
|
||||
inline void operator=(const char* str);
|
||||
|
||||
//- Move assignment.
|
||||
inline void operator=(wordRe&& str);
|
||||
|
||||
|
||||
// IOstream operators
|
||||
|
||||
|
||||
@ -148,6 +148,13 @@ inline Foam::wordRe::wordRe(const word& str, const compOption opt)
|
||||
}
|
||||
|
||||
|
||||
inline Foam::wordRe::wordRe(wordRe&& str)
|
||||
:
|
||||
word(std::move(static_cast<word&>(str))),
|
||||
re_(std::move(str.re_))
|
||||
{}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::wordRe::isPattern() const
|
||||
@ -249,6 +256,13 @@ inline void Foam::wordRe::set(const char* str, const compOption opt)
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::wordRe::swap(wordRe& str)
|
||||
{
|
||||
word::swap(static_cast<word&>(str));
|
||||
re_.swap(str.re_);
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||
|
||||
inline bool Foam::wordRe::operator()(const std::string& text) const
|
||||
@ -313,4 +327,11 @@ inline void Foam::wordRe::operator=(const char* str)
|
||||
}
|
||||
|
||||
|
||||
inline void Foam::wordRe::operator=(wordRe&& str)
|
||||
{
|
||||
clear();
|
||||
swap(str);
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
|
||||
Reference in New Issue
Block a user