diff --git a/applications/test/regex/Test-regex.C b/applications/test/regex/Test-regex.C index 6f506f7659..6996d4445c 100644 --- a/applications/test/regex/Test-regex.C +++ b/applications/test/regex/Test-regex.C @@ -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 groups; + List 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; diff --git a/applications/test/string/Test-string.C b/applications/test/string/Test-string.C index 7975620442..73c703d170 100644 --- a/applications/test/string/Test-string.C +++ b/applications/test/string/Test-string.C @@ -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"); diff --git a/applications/test/wordRe/Test-wordRe.C b/applications/test/wordRe/Test-wordRe.C index be1af08a54..5504e66a1e 100644 --- a/applications/test/wordRe/Test-wordRe.C +++ b/applications/test/wordRe/Test-wordRe.C @@ -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; diff --git a/src/OSspecific/POSIX/regExp.H b/src/OSspecific/POSIX/regExp.H index 6a03192cf2..c5c7892909 100644 --- a/src/OSspecific/POSIX/regExp.H +++ b/src/OSspecific/POSIX/regExp.H @@ -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); + }; diff --git a/src/OSspecific/POSIX/regExpI.H b/src/OSspecific/POSIX/regExpI.H index 7302e02410..60c7da3053 100644 --- a/src/OSspecific/POSIX/regExpI.H +++ b/src/OSspecific/POSIX/regExpI.H @@ -23,6 +23,8 @@ License \*---------------------------------------------------------------------------*/ +#include + // * * * * * * * * * * * * * 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); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H index 20f9258bda..002a3e9227 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicList.H @@ -249,6 +249,9 @@ public: //- Append an element to the end of this list. inline DynamicList& append(const T& val); + //- Move append an element + inline DynamicList& append(T&& val); + //- Append another list to the end of this list. inline DynamicList& append(const UList& lst); diff --git a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H index c6adaec88f..81ec482e21 100644 --- a/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H +++ b/src/OpenFOAM/containers/Lists/DynamicList/DynamicListI.H @@ -492,7 +492,22 @@ Foam::DynamicList::append const label idx = List::size(); setSize(idx + 1); - this->operator[](idx) = val; + this->operator[](idx) = val; // copy element + return *this; +} + + +template +inline Foam::DynamicList& +Foam::DynamicList::append +( + T&& val +) +{ + const label idx = List::size(); + setSize(idx + 1); + + this->operator[](idx) = std::move(val); // move assign element return *this; } @@ -510,12 +525,13 @@ Foam::DynamicList::append << "Attempted appending to self" << abort(FatalError); } - label nextFree = List::size(); - setSize(nextFree + lst.size()); + label idx = List::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::append const FixedList& lst ) { - label nextFree = List::size(); - setSize(nextFree + lst.size()); + label idx = List::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::append std::initializer_list lst ) { - label nextFree = List::size(); - setSize(nextFree + lst.size()); + label idx = List::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::append const UIndirectList& lst ) { - label nextFree = List::size(); - setSize(nextFree + lst.size()); + label idx = List::size(); + const label n = lst.size(); - forAll(lst, elemI) + setSize(idx + n); + + for (label i=0; ioperator[](nextFree++) = lst[elemI]; + this->operator[](idx++) = lst[i]; // copy element } return *this; } @@ -589,12 +608,13 @@ Foam::DynamicList::append << "Attempted appending to self" << abort(FatalError); } - label nextFree = List::size(); - setSize(nextFree + lst.size()); + label idx = List::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::operator= const FixedList& lst ) { - setSize(lst.size()); + const label n = lst.size(); - forAll(lst, i) + setSize(n); + + for (label i=0; ioperator[](i) = lst[i]; + this->operator[](i) = lst[i]; // copy element } } diff --git a/src/OpenFOAM/containers/Lists/List/List.H b/src/OpenFOAM/containers/Lists/List/List.H index e2224c8c3c..843272099f 100644 --- a/src/OpenFOAM/containers/Lists/List/List.H +++ b/src/OpenFOAM/containers/Lists/List/List.H @@ -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& lst); //- Append a UIndirectList at the end of this list diff --git a/src/OpenFOAM/containers/Lists/List/ListI.H b/src/OpenFOAM/containers/Lists/List/ListI.H index f93a099bb4..b815a32d87 100644 --- a/src/OpenFOAM/containers/Lists/List/ListI.H +++ b/src/OpenFOAM/containers/Lists/List/ListI.H @@ -172,7 +172,17 @@ inline Foam::Xfer> Foam::List::xfer() template inline void Foam::List::append(const T& val) { - setSize(this->size()+1, val); + setSize(this->size() + 1, val); // copy element +} + + +template +inline void Foam::List::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::append(const UList& 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; ioperator[](nextFree++) = lst[i]; + this->operator[](idx++) = lst[i]; // copy element } } @@ -198,12 +210,14 @@ inline void Foam::List::append(const UList& lst) template inline void Foam::List::append(const UIndirectList& 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; ioperator[](nextFree++) = lst[i]; + this->operator[](idx++) = lst[i]; // copy element } } diff --git a/src/OpenFOAM/containers/Lists/List/ListIO.C b/src/OpenFOAM/containers/Lists/List/ListIO.C index b29418c24a..aa8cd447f1 100644 --- a/src/OpenFOAM/containers/Lists/List/ListIO.C +++ b/src/OpenFOAM/containers/Lists/List/ListIO.C @@ -92,7 +92,7 @@ Foam::Istream& Foam::operator>>(Istream& is, List& 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& L) // Putback the opening bracket is.putBack(firstToken); - // Now read as a singly-linked list + // Read as a singly-linked list SLList sll(is); // Convert the singly-linked list to this list @@ -176,8 +176,11 @@ Foam::List Foam::readList(Istream& is) << exit(FatalIOError); } - // Read via a singly-linked list - L = SLList(is); + // Read as singly-linked list + SLList sll(is); + + // Convert the singly-linked list to this list + L = sll; } else { diff --git a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C index 5c6f14103e..3cdcf3e6d0 100644 --- a/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C +++ b/src/OpenFOAM/db/IOstreams/Sstreams/OSstream.C @@ -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; } diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index 8ca6388330..ceba588116 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -359,10 +359,8 @@ std::string Foam::fileName::path(const std::string& str) { return str.substr(0, i); } - else - { - return "/"; - } + + return "/"; } diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.H b/src/OpenFOAM/primitives/strings/keyType/keyType.H index 0f9d613943..14770a9ce9 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyType.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.H @@ -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 diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H index 575bb834b1..95fa78496a 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H @@ -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 + // * * * * * * * * * * * * 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(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(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); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index b16b30f66d..ac8beb6f18 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -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); } diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index 3af6835f14..ee5aee9b9c 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -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, diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 89a36cc3ee..7d584d2244 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -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 diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index 0bb89dd693..8646523b68 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -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, diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H index 0d84856f52..3ae8bf4f4f 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.H +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.H @@ -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 diff --git a/src/OpenFOAM/primitives/strings/word/word.C b/src/OpenFOAM/primitives/strings/word/word.C index 932f10af4c..c51d489c91 100644 --- a/src/OpenFOAM/primitives/strings/word/word.C +++ b/src/OpenFOAM/primitives/strings/word/word.C @@ -25,6 +25,7 @@ License #include "word.H" #include "debug.H" +#include // * * * * * * * * * * * * * * 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; +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/word/word.H b/src/OpenFOAM/primitives/strings/word/word.H index b92d720934..b840928cef 100644 --- a/src/OpenFOAM/primitives/strings/word/word.H +++ b/src/OpenFOAM/primitives/strings/word/word.H @@ -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 diff --git a/src/OpenFOAM/primitives/strings/word/wordI.H b/src/OpenFOAM/primitives/strings/word/wordI.H index 2fa48801a7..7ee9e0a08d 100644 --- a/src/OpenFOAM/primitives/strings/word/wordI.H +++ b/src/OpenFOAM/primitives/strings/word/wordI.H @@ -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; - } -} - - // ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H index 8195abb90e..a7573068be 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H @@ -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 diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H index 658df00695..c269e321a4 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H @@ -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(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(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); +} + + // ************************************************************************* //