mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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:
committed by
Andrew Heather
parent
515027b7ab
commit
3a00dd9b9a
@ -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<class RegexType>
|
||||
@ -209,7 +215,6 @@ void generalTests()
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class RegexType>
|
||||
void testExpressions(const UList<regexTest>& 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<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"}))
|
||||
{
|
||||
Info<< "Specified one or more of -cxx, -posix" << nl;
|
||||
@ -321,10 +339,12 @@ int main(int argc, char *argv[])
|
||||
generalTests<regExpCxx>();
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
if (args.found("posix"))
|
||||
{
|
||||
generalTests<regExpPosix>();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
for (label argi = 1; argi < args.size(); ++argi)
|
||||
@ -339,10 +359,12 @@ int main(int argc, char *argv[])
|
||||
testExpressions<regExpCxx>(tests);
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
if (args.found("posix"))
|
||||
{
|
||||
testExpressions<regExpPosix>(tests);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Info<< "\nDone" << nl << endl;
|
||||
|
||||
19
applications/test/regex1/testRegexps2
Normal file
19
applications/test/regex1/testRegexps2
Normal 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" )
|
||||
)
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
@ -32,6 +32,7 @@ Description
|
||||
#ifndef regExp_H
|
||||
#define regExp_H
|
||||
|
||||
#include "regExpCxx.H"
|
||||
#include "regExpPosix.H"
|
||||
#include "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
|
||||
}
|
||||
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
@ -120,7 +120,7 @@ public:
|
||||
inline ~regExpPosix();
|
||||
|
||||
|
||||
// Member functions
|
||||
// Member Functions
|
||||
|
||||
// Access
|
||||
|
||||
|
||||
@ -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_;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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_))
|
||||
|
||||
Reference in New Issue
Block a user