From 3a00dd9b9a765303c99e5c62bfb00d316abad5d5 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 21 May 2019 12:29:52 +0100 Subject: [PATCH 1/3] CONFIG: use C++11 regex instead of POSIX for newer compilers BUG: The ok_ flag was not being updated in the regExpCxx::set() method --- applications/test/regex1/Test-regex1.C | 24 ++++++++++++++++++- applications/test/regex1/testRegexps2 | 19 +++++++++++++++ src/OSspecific/POSIX/regExp/regExp.H | 1 + src/OSspecific/POSIX/regExp/regExpFwd.H | 5 ++++ src/OSspecific/POSIX/regExp/regExpPosix.H | 2 +- .../primitives/strings/regex/regExpCxx.C | 12 +++++----- .../primitives/strings/regex/regExpCxx.H | 6 ++--- .../primitives/strings/regex/regExpCxxI.H | 3 ++- 8 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 applications/test/regex1/testRegexps2 diff --git a/applications/test/regex1/Test-regex1.C b/applications/test/regex1/Test-regex1.C index ca5bfb60cd..9bc38d64b2 100644 --- a/applications/test/regex1/Test-regex1.C +++ b/applications/test/regex1/Test-regex1.C @@ -27,12 +27,16 @@ Description \*---------------------------------------------------------------------------*/ #include "argList.H" +#include "IOobject.H" #include "IOstreams.H" #include "IFstream.H" #include "Switch.H" +#include "SubStrings.H" #include "regExpCxx.H" +#ifndef _WIN32 #include "regExpPosix.H" +#endif using namespace Foam; @@ -83,6 +87,7 @@ static Ostream& operator<<(Ostream& os, const regExpCxx::results_type& sm) // Simple output of match groups +#ifndef _WIN32 static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) { for (std::smatch::size_type i = 1; i < sm.size(); ++i) @@ -92,6 +97,7 @@ static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) return os; } +#endif template @@ -209,7 +215,6 @@ void generalTests() } - template void testExpressions(const UList& tests) { @@ -293,11 +298,13 @@ int main(int argc, char *argv[]) "Test C++11 regular expressions" ); + #ifndef _WIN32 argList::addBoolOption ( "posix", "Test POSIX regular expressions" ); + #endif argList::addArgument("file"); argList::addArgument("..."); @@ -306,6 +313,17 @@ int main(int argc, char *argv[]) #include "setRootCase.H" + if (std::is_same::value) + { + Info<<"Foam::regExp uses C++11 regex" << nl << nl; + } + #ifndef _WIN32 + if (std::is_same::value) + { + Info<<"Foam::regExp uses POSIX regex" << nl << nl; + } + #endif + if (!args.count({"cxx", "posix"})) { Info<< "Specified one or more of -cxx, -posix" << nl; @@ -321,10 +339,12 @@ int main(int argc, char *argv[]) generalTests(); } + #ifndef _WIN32 if (args.found("posix")) { generalTests(); } + #endif } for (label argi = 1; argi < args.size(); ++argi) @@ -339,10 +359,12 @@ int main(int argc, char *argv[]) testExpressions(tests); } + #ifndef _WIN32 if (args.found("posix")) { testExpressions(tests); } + #endif } Info<< "\nDone" << nl << endl; diff --git a/applications/test/regex1/testRegexps2 b/applications/test/regex1/testRegexps2 new file mode 100644 index 0000000000..207c05b048 --- /dev/null +++ b/applications/test/regex1/testRegexps2 @@ -0,0 +1,19 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v1812 | +| \\ / A nd | Web: www.OpenFOAM.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Pattern, String +( + ( true "(U|k|epsilon)" "U" ) + ( false "(U|k|epsilon)" "alpha" ) + ( true "ab.*" "abc" ) + ( true ".*" "abc" ) +) + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/POSIX/regExp/regExp.H b/src/OSspecific/POSIX/regExp/regExp.H index eee48c78d8..fa85bea12e 100644 --- a/src/OSspecific/POSIX/regExp/regExp.H +++ b/src/OSspecific/POSIX/regExp/regExp.H @@ -32,6 +32,7 @@ Description #ifndef regExp_H #define regExp_H +#include "regExpCxx.H" #include "regExpPosix.H" #include "regExpFwd.H" diff --git a/src/OSspecific/POSIX/regExp/regExpFwd.H b/src/OSspecific/POSIX/regExp/regExpFwd.H index 47e8c268cf..0983822280 100644 --- a/src/OSspecific/POSIX/regExp/regExpFwd.H +++ b/src/OSspecific/POSIX/regExp/regExpFwd.H @@ -39,7 +39,12 @@ namespace Foam class regExpCxx; class regExpPosix; + // Newer compilers support regex directly + #if (_GLIBCXX_RELEASE >= 7) || (__clang_major__ >= 7) + typedef regExpCxx regExp; + #else typedef regExpPosix regExp; + #endif } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OSspecific/POSIX/regExp/regExpPosix.H b/src/OSspecific/POSIX/regExp/regExpPosix.H index dbed07a4dc..86c6147538 100644 --- a/src/OSspecific/POSIX/regExp/regExpPosix.H +++ b/src/OSspecific/POSIX/regExp/regExpPosix.H @@ -120,7 +120,7 @@ public: inline ~regExpPosix(); - // Member functions + // Member Functions // Access diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxx.C b/src/OpenFOAM/primitives/strings/regex/regExpCxx.C index 0f99c82dc1..f5dfe943c4 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxx.C +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxx.C @@ -108,7 +108,7 @@ static std::string error_string(const std::regex_error& err) bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) { - clear(); + clear(); // Also sets ok_ = false size_t len = (pattern ? strlen(pattern) : 0); @@ -139,7 +139,7 @@ bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) try { re_.assign(pat, flags); - return true; + ok_ = true; } catch (const std::regex_error& err) { @@ -151,13 +151,13 @@ bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) } } - return false; + return ok_; } bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase) { - clear(); + clear(); // Also sets ok_ = false auto len = pattern.size(); @@ -188,7 +188,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase) try { re_.assign(pat, pattern.end(), flags); - return true; + ok_ = true; } catch (const std::regex_error& err) { @@ -200,7 +200,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase) } } - return false; + return ok_; } diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H index a96ad8e84a..d56d16f927 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxx.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxx.H @@ -41,7 +41,7 @@ Description Note The C++11 regular expressions may be broken on some compilers. For example, gcc 4.8 is known to fail. - For these systems the POSIX implementation should be used. + For these systems the POSIX implementation or alternative must be used. SourceFiles regExpCxxI.H @@ -66,7 +66,7 @@ namespace Foam class regExpCxx { - // Private data + // Private Data //- Regular expression (using char type) std::regex re_; @@ -132,7 +132,7 @@ public: ~regExpCxx() = default; - // Member functions + // Member Functions // Access diff --git a/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H b/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H index 92d84a0037..b60cbf1e8c 100644 --- a/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H +++ b/src/OpenFOAM/primitives/strings/regex/regExpCxxI.H @@ -157,7 +157,8 @@ inline void Foam::regExpCxx::swap(regExpCxx& rgx) } -inline std::string::size_type Foam::regExpCxx::find(const std::string& text) const +inline std::string::size_type +Foam::regExpCxx::find(const std::string& text) const { std::smatch mat; if (!text.empty() && std::regex_search(text, mat, re_)) From b043be3063664d1c9b71f3ad1dc9536b78de331d Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 17 May 2019 09:02:51 +0100 Subject: [PATCH 2/3] STYLE: add notices for deprecated dictionary methods --- .../foamUpgradeCyclics/foamUpgradeCyclics.C | 4 +- .../linkTypes/DLListBase/DLListBase.H | 6 +-- .../linkTypes/SLListBase/SLListBase.H | 6 +-- src/OpenFOAM/db/dictionary/dictionary.H | 47 ++++++++++++++----- src/OpenFOAM/meshes/meshShapes/face/face.H | 4 +- .../meshes/meshShapes/triFace/triFace.H | 4 +- .../primitiveShapes/triangle/triangle.H | 4 +- .../primitives/strings/lists/hashedWordList.H | 6 +-- 8 files changed, 49 insertions(+), 32 deletions(-) diff --git a/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C b/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C index 5c7ea64814..c948cbf9ab 100644 --- a/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C +++ b/applications/utilities/preProcessing/foamUpgradeCyclics/foamUpgradeCyclics.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -313,7 +313,7 @@ void rewriteField if ( boundaryField.found(patchName) - && !boundaryField.found(newName, false, false) + && !boundaryField.found(newName, keyType::LITERAL) ) { Info<< " Changing entry " << patchName << " to " << newName diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H index 24c0b5bd8b..e6bb07cd9f 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/DLListBase/DLListBase.H @@ -243,8 +243,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } @@ -297,8 +296,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } diff --git a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H index 066cce1b86..c48913e399 100644 --- a/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H +++ b/src/OpenFOAM/containers/LinkedLists/linkTypes/SLListBase/SLListBase.H @@ -219,8 +219,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } @@ -271,8 +270,7 @@ public: //- Deprecated(2019-01) Pointing at a valid storage node // \deprecated(2019-01) - use good() method - inline bool found() const - FOAM_DEPRECATED_FOR(2019-01, "good() method") + bool FOAM_DEPRECATED_FOR(2019-01, "good() method") found() const { return this->good(); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 33dc695cd5..1a510281d8 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -1108,7 +1108,8 @@ public: //- Deprecated(2018-07) find and return an entry data stream // // \deprecated(2018-07) - use lookup() method - ITstream& operator[](const word& keyword) const + ITstream& FOAM_DEPRECATED_FOR(2018-07, "lookup() method") + operator[](const word& keyword) const { return lookup(keyword); } @@ -1116,7 +1117,8 @@ public: //- Deprecated(2018-10) find and return a T. // \deprecated(2018-10) - use get() method template - T lookupType + T FOAM_DEPRECATED_FOR(2018-10, "get() method") + lookupType ( const word& keyword, bool recursive = false, @@ -1128,7 +1130,8 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - bool found + bool FOAM_DEPRECATED_FOR(2018-10, "found(keyType::option)") + found ( const word& keyword, bool recursive, @@ -1140,7 +1143,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - entry* lookupEntryPtr + entry* + FOAM_DEPRECATED_FOR(2018-10, "lookupEntryPtr(keyType::option)") + lookupEntryPtr ( const word& keyword, bool recursive, @@ -1152,7 +1157,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - const entry* lookupEntryPtr + const entry* + FOAM_DEPRECATED_FOR(2018-10, "lookupEntryPtr(keyType::option)") + lookupEntryPtr ( const word& keyword, bool recursive, @@ -1164,7 +1171,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - const entry* lookupScopedEntryPtr + const entry* + FOAM_DEPRECATED_FOR(2018-10, "lookupScopedEntryPtr(keyType::option)") + lookupScopedEntryPtr ( const word& keyword, bool recursive, @@ -1180,7 +1189,9 @@ public: // // Search type: non-recursive with patterns. // \deprecated(2018-10) - use findDict() method - const dictionary* subDictPtr(const word& keyword) const + const dictionary* + FOAM_DEPRECATED_FOR(2018-10, "findDict() method") + subDictPtr(const word& keyword) const { return findDict(keyword, keyType::REGEX); } @@ -1191,14 +1202,18 @@ public: // // Search type: non-recursive with patterns. // \deprecated(2018-10) - use findDict() method - dictionary* subDictPtr(const word& keyword) + dictionary* + FOAM_DEPRECATED_FOR(2018-10, "findDict() method") + subDictPtr(const word& keyword) { return findDict(keyword, keyType::REGEX); } //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - const entry& lookupEntry + const entry& + FOAM_DEPRECATED_FOR(2018-10, "lookupEntry(keyType::option)") + lookupEntry ( const word& keyword, bool recursive, @@ -1210,7 +1225,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version - ITstream& lookup + ITstream& + FOAM_DEPRECATED_FOR(2018-10, "lookup(keyType::option)") + lookup ( const word& keyword, bool recursive, @@ -1223,7 +1240,8 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version template - T lookupOrDefault + T FOAM_DEPRECATED_FOR(2018-10, "lookupOrDefault(keyType::option)") + lookupOrDefault ( const word& keyword, const T& deflt, @@ -1239,7 +1257,8 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version template - T lookupOrAddDefault + T FOAM_DEPRECATED_FOR(2018-10, "lookupOrAddDefault(keyType::option)") + lookupOrAddDefault ( const word& keyword, const T& deflt, @@ -1255,7 +1274,9 @@ public: //- Deprecated(2018-10) // \deprecated(2018-10) - use keyType::option version template - bool readIfPresent + bool + FOAM_DEPRECATED_FOR(2018-10, "readIfPresent(keyType::option)") + readIfPresent ( const word& keyword, T& val, diff --git a/src/OpenFOAM/meshes/meshShapes/face/face.H b/src/OpenFOAM/meshes/meshShapes/face/face.H index d2e78edf3e..28817059d4 100644 --- a/src/OpenFOAM/meshes/meshShapes/face/face.H +++ b/src/OpenFOAM/meshes/meshShapes/face/face.H @@ -202,8 +202,8 @@ public: //- Legacy name for areaNormal() // \deprecated(2018-06) Deprecated for new use - inline vector normal(const UList& p) const - FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + normal(const UList& p) const { return areaNormal(p); // Legacy definition } diff --git a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H index ea964983e3..8762215433 100644 --- a/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H +++ b/src/OpenFOAM/meshes/meshShapes/triFace/triFace.H @@ -133,8 +133,8 @@ public: //- Legacy name for areaNormal() // \deprecated(2018-06) Deprecated for new use - inline vector normal(const UList& points) const - FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + normal(const UList& points) const { return areaNormal(points); // Legacy definition } diff --git a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H index cf985f0278..58b90a93aa 100644 --- a/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H +++ b/src/OpenFOAM/meshes/primitiveShapes/triangle/triangle.H @@ -216,8 +216,8 @@ public: //- Legacy name for areaNormal(). // \deprecated(2018-06) Deprecated for new use - inline vector normal() const - FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + vector FOAM_DEPRECATED_FOR(2018-12, "areaNormal() or unitNormal()") + normal() const { return areaNormal(); } diff --git a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H index cd401dbb5a..f58fc24838 100644 --- a/src/OpenFOAM/primitives/strings/lists/hashedWordList.H +++ b/src/OpenFOAM/primitives/strings/lists/hashedWordList.H @@ -182,10 +182,10 @@ public: //- Deprecated(2019-01) Is the specified name found in the list? // \deprecated(2019-01) - use found() method - inline bool contains(const word& name) const - FOAM_DEPRECATED_FOR(2019-01, "found() method") + bool FOAM_DEPRECATED_FOR(2019-01, "found() method") + contains(const word& name) const { - return this-found(name); + return this->found(name); } }; From 32916fa8458e4cfdd0debfc47ce4c20e069c1d22 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 21 May 2019 19:10:14 +0100 Subject: [PATCH 3/3] ENH: dictionary checking methods with predicates on the input values - can be used to check the validity of input values. Example: dict.getCheck