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_))