CONFIG: use C++11 regex instead of POSIX for newer compilers

BUG: The ok_ flag was not being updated in the regExpCxx::set() method
This commit is contained in:
Mark Olesen
2019-05-21 12:29:52 +01:00
committed by Andrew Heather
parent 515027b7ab
commit 3a00dd9b9a
8 changed files with 60 additions and 12 deletions

View File

@ -27,12 +27,16 @@ Description
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include "argList.H" #include "argList.H"
#include "IOobject.H"
#include "IOstreams.H" #include "IOstreams.H"
#include "IFstream.H" #include "IFstream.H"
#include "Switch.H" #include "Switch.H"
#include "SubStrings.H"
#include "regExpCxx.H" #include "regExpCxx.H"
#ifndef _WIN32
#include "regExpPosix.H" #include "regExpPosix.H"
#endif
using namespace Foam; using namespace Foam;
@ -83,6 +87,7 @@ static Ostream& operator<<(Ostream& os, const regExpCxx::results_type& sm)
// Simple output of match groups // Simple output of match groups
#ifndef _WIN32
static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm) static Ostream& operator<<(Ostream& os, const regExpPosix::results_type& sm)
{ {
for (std::smatch::size_type i = 1; i < sm.size(); ++i) 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; return os;
} }
#endif
template<class RegexType> template<class RegexType>
@ -209,7 +215,6 @@ void generalTests()
} }
template<class RegexType> template<class RegexType>
void testExpressions(const UList<regexTest>& tests) void testExpressions(const UList<regexTest>& tests)
{ {
@ -293,11 +298,13 @@ int main(int argc, char *argv[])
"Test C++11 regular expressions" "Test C++11 regular expressions"
); );
#ifndef _WIN32
argList::addBoolOption argList::addBoolOption
( (
"posix", "posix",
"Test POSIX regular expressions" "Test POSIX regular expressions"
); );
#endif
argList::addArgument("file"); argList::addArgument("file");
argList::addArgument("..."); argList::addArgument("...");
@ -306,6 +313,17 @@ int main(int argc, char *argv[])
#include "setRootCase.H" #include "setRootCase.H"
if (std::is_same<regExp, regExpCxx>::value)
{
Info<<"Foam::regExp uses C++11 regex" << nl << nl;
}
#ifndef _WIN32
if (std::is_same<regExp, regExpPosix>::value)
{
Info<<"Foam::regExp uses POSIX regex" << nl << nl;
}
#endif
if (!args.count({"cxx", "posix"})) if (!args.count({"cxx", "posix"}))
{ {
Info<< "Specified one or more of -cxx, -posix" << nl; Info<< "Specified one or more of -cxx, -posix" << nl;
@ -321,10 +339,12 @@ int main(int argc, char *argv[])
generalTests<regExpCxx>(); generalTests<regExpCxx>();
} }
#ifndef _WIN32
if (args.found("posix")) if (args.found("posix"))
{ {
generalTests<regExpPosix>(); generalTests<regExpPosix>();
} }
#endif
} }
for (label argi = 1; argi < args.size(); ++argi) for (label argi = 1; argi < args.size(); ++argi)
@ -339,10 +359,12 @@ int main(int argc, char *argv[])
testExpressions<regExpCxx>(tests); testExpressions<regExpCxx>(tests);
} }
#ifndef _WIN32
if (args.found("posix")) if (args.found("posix"))
{ {
testExpressions<regExpPosix>(tests); testExpressions<regExpPosix>(tests);
} }
#endif
} }
Info<< "\nDone" << nl << endl; Info<< "\nDone" << nl << endl;

View File

@ -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" )
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -32,6 +32,7 @@ Description
#ifndef regExp_H #ifndef regExp_H
#define regExp_H #define regExp_H
#include "regExpCxx.H"
#include "regExpPosix.H" #include "regExpPosix.H"
#include "regExpFwd.H" #include "regExpFwd.H"

View File

@ -39,7 +39,12 @@ namespace Foam
class regExpCxx; class regExpCxx;
class regExpPosix; class regExpPosix;
// Newer compilers support regex directly
#if (_GLIBCXX_RELEASE >= 7) || (__clang_major__ >= 7)
typedef regExpCxx regExp;
#else
typedef regExpPosix regExp; typedef regExpPosix regExp;
#endif
} }
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -120,7 +120,7 @@ public:
inline ~regExpPosix(); inline ~regExpPosix();
// Member functions // Member Functions
// Access // Access

View File

@ -108,7 +108,7 @@ static std::string error_string(const std::regex_error& err)
bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase) bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase)
{ {
clear(); clear(); // Also sets ok_ = false
size_t len = (pattern ? strlen(pattern) : 0); size_t len = (pattern ? strlen(pattern) : 0);
@ -139,7 +139,7 @@ bool Foam::regExpCxx::set(const char* pattern, bool ignoreCase)
try try
{ {
re_.assign(pat, flags); re_.assign(pat, flags);
return true; ok_ = true;
} }
catch (const std::regex_error& err) 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) bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase)
{ {
clear(); clear(); // Also sets ok_ = false
auto len = pattern.size(); auto len = pattern.size();
@ -188,7 +188,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase)
try try
{ {
re_.assign(pat, pattern.end(), flags); re_.assign(pat, pattern.end(), flags);
return true; ok_ = true;
} }
catch (const std::regex_error& err) catch (const std::regex_error& err)
{ {
@ -200,7 +200,7 @@ bool Foam::regExpCxx::set(const std::string& pattern, bool ignoreCase)
} }
} }
return false; return ok_;
} }

View File

@ -41,7 +41,7 @@ Description
Note Note
The C++11 regular expressions may be broken on some compilers. The C++11 regular expressions may be broken on some compilers.
For example, gcc 4.8 is known to fail. 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 SourceFiles
regExpCxxI.H regExpCxxI.H
@ -66,7 +66,7 @@ namespace Foam
class regExpCxx class regExpCxx
{ {
// Private data // Private Data
//- Regular expression (using char type) //- Regular expression (using char type)
std::regex re_; std::regex re_;
@ -132,7 +132,7 @@ public:
~regExpCxx() = default; ~regExpCxx() = default;
// Member functions // Member Functions
// Access // Access

View File

@ -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; std::smatch mat;
if (!text.empty() && std::regex_search(text, mat, re_)) if (!text.empty() && std::regex_search(text, mat, re_))