mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
reworked regExp + wordRe a bit, minor change to keyType
regExp:
- added optional ignoreCase for constructor.
- the compile() methods is now exposed as set(...) method with an optional
ignoreCase argument. Not currently much use for the other regex compile
flags though. The set() method can be used directly instead of the
operator=() assignment.
keyType + wordRe:
- it's not clear that any particular characters are valid/invalid (compared
to string or word), so just drop the valid(char) method for now
wordRe:
- a bool doesn't suffice, added enum compOption (compile-option)
- most constructors now have a compOption. In *all* cases it defaults to
LITERAL - ie, the same behaviour for std::string and Foam::string
- added set(...) methods that do much the same as operator=(...), but the
compOption can be specified. In all cases, it defaults to DETECT.
In Summary
By default the constructors will generally preserve the argument as
string literal and the assignment operators will use the wordRe::DETECT
compOption to scan the string for regular expression meta characters
and/or invalid word characters and react accordingly.
The exceptions are when constructing/assigning from another
Foam::wordRe (preserve the same type) or from a Foam::word (always
literal).
This commit is contained in:
@ -32,30 +32,6 @@ License
|
||||
#include "List.H"
|
||||
#include "IOstreams.H"
|
||||
|
||||
|
||||
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
|
||||
|
||||
void Foam::regExp::compile(const char* pattern) const
|
||||
{
|
||||
clear();
|
||||
|
||||
// avoid NULL pointer and zero-length patterns
|
||||
if (pattern && *pattern)
|
||||
{
|
||||
preg_ = new regex_t;
|
||||
|
||||
if (regcomp(preg_, pattern, REG_EXTENDED) != 0)
|
||||
{
|
||||
FatalErrorIn
|
||||
(
|
||||
"regExp::compile(const char*)"
|
||||
) << "Failed to compile regular expression '" << pattern << "'"
|
||||
<< exit(FatalError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||
|
||||
Foam::regExp::regExp()
|
||||
@ -64,19 +40,19 @@ Foam::regExp::regExp()
|
||||
{}
|
||||
|
||||
|
||||
Foam::regExp::regExp(const char* pattern)
|
||||
Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
|
||||
:
|
||||
preg_(0)
|
||||
{
|
||||
compile(pattern);
|
||||
set(pattern, ignoreCase);
|
||||
}
|
||||
|
||||
|
||||
Foam::regExp::regExp(const std::string& pattern)
|
||||
Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
|
||||
:
|
||||
preg_(0)
|
||||
{
|
||||
compile(pattern.c_str());
|
||||
set(pattern.c_str(), ignoreCase);
|
||||
}
|
||||
|
||||
|
||||
@ -90,6 +66,39 @@ Foam::regExp::~regExp()
|
||||
|
||||
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
|
||||
|
||||
void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
|
||||
{
|
||||
clear();
|
||||
|
||||
// avoid NULL pointer and zero-length patterns
|
||||
if (pattern && *pattern)
|
||||
{
|
||||
preg_ = new regex_t;
|
||||
|
||||
int cflags = REG_EXTENDED;
|
||||
if (ignoreCase)
|
||||
{
|
||||
cflags |= REG_ICASE;
|
||||
}
|
||||
|
||||
if (regcomp(preg_, pattern, cflags) != 0)
|
||||
{
|
||||
FatalErrorIn
|
||||
(
|
||||
"regExp::set(const char*)"
|
||||
) << "Failed to compile regular expression '" << pattern << "'"
|
||||
<< exit(FatalError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
|
||||
{
|
||||
return set(pattern.c_str(), ignoreCase);
|
||||
}
|
||||
|
||||
|
||||
bool Foam::regExp::clear() const
|
||||
{
|
||||
if (preg_)
|
||||
@ -194,13 +203,13 @@ bool Foam::regExp::match(const string& str, List<string>& groups) const
|
||||
|
||||
void Foam::regExp::operator=(const char* pat)
|
||||
{
|
||||
compile(pat);
|
||||
set(pat);
|
||||
}
|
||||
|
||||
|
||||
void Foam::regExp::operator=(const std::string& pat)
|
||||
{
|
||||
compile(pat.c_str());
|
||||
set(pat);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -65,9 +65,6 @@ class regExp
|
||||
|
||||
// Private member functions
|
||||
|
||||
//- Compile into a regular expression
|
||||
void compile(const char*) const;
|
||||
|
||||
//- Disallow default bitwise copy construct
|
||||
regExp(const regExp&);
|
||||
|
||||
@ -100,11 +97,11 @@ public:
|
||||
//- Construct null
|
||||
regExp();
|
||||
|
||||
//- Construct from character array
|
||||
regExp(const char*);
|
||||
//- Construct from character array, optionally ignoring case
|
||||
regExp(const char*, const bool ignoreCase=false);
|
||||
|
||||
//- Construct from std::string (or string)
|
||||
regExp(const std::string&);
|
||||
//- Construct from std::string (or string), optionally ignoring case
|
||||
regExp(const std::string&, const bool ignoreCase=false);
|
||||
|
||||
// Destructor
|
||||
|
||||
@ -113,7 +110,9 @@ public:
|
||||
|
||||
// Member functions
|
||||
|
||||
//- Is the precompiled expression set?
|
||||
//- Access
|
||||
|
||||
//- Does a precompiled expression exist?
|
||||
inline bool exists() const
|
||||
{
|
||||
return preg_ ? true : false;
|
||||
@ -125,10 +124,23 @@ public:
|
||||
return preg_ ? preg_->re_nsub : 0;
|
||||
}
|
||||
|
||||
|
||||
//- Editing
|
||||
|
||||
//- Compile pattern into a regular expression, optionally ignoring case
|
||||
void set(const char*, const bool ignoreCase=false) const;
|
||||
|
||||
//- Compile pattern into a regular expression, optionally ignoring case
|
||||
void set(const std::string&, const bool ignoreCase=false) const;
|
||||
|
||||
|
||||
//- Release precompiled expression.
|
||||
// Returns true if precompiled expression existed before clear
|
||||
bool clear() const;
|
||||
|
||||
|
||||
//- Searching
|
||||
|
||||
//- Find position within string.
|
||||
// Returns the index where it begins or string::npos if not found
|
||||
std::string::size_type find(const std::string& str) const;
|
||||
@ -150,12 +162,14 @@ public:
|
||||
|
||||
// Member Operators
|
||||
|
||||
//- Assign from a string and compile regular expression
|
||||
void operator=(const std::string&);
|
||||
|
||||
//- Assign from a character array and compile regular expression
|
||||
//- Assign and compile pattern from a character array
|
||||
// Always case sensitive
|
||||
void operator=(const char*);
|
||||
|
||||
//- Assign and compile pattern from string
|
||||
// Always case sensitive
|
||||
void operator=(const std::string&);
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user