new wordRe class - a word that holds a regExp

- a possible future replacement for keyType, but the immediate use is the
    wordReList for grepping through other lists.
  - note that the argList treatment of '(' ... ')' yields quoted strings,
    which we can use for building a wordReList

minor cleanup of regExp class

  - constructor from std::string, match std::string and
    operator=(std::string&)
    rely on automatic conversion to Foam::string
  - ditch partialMatch with sub-groups, it doesn't make much sense
This commit is contained in:
Mark Olesen
2009-01-04 00:33:27 +01:00
parent 1d866d7fe8
commit 2717aa5c7d
19 changed files with 1036 additions and 182 deletions

View File

@ -25,6 +25,7 @@ License
\*---------------------------------------------------------------------------*/
#include <sys/types.h>
#include "regExp.H"
#include "label.H"
#include "string.H"
@ -34,38 +35,27 @@ License
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
void Foam::regExp::compile(const char* pat) const
void Foam::regExp::compile(const char* pattern) const
{
clear();
// avoid NULL and zero-length patterns
if (pat && *pat)
// avoid NULL pointer and zero-length patterns
if (pattern && *pattern)
{
preg_ = new regex_t;
if (regcomp(preg_, pat, REG_EXTENDED) != 0)
if (regcomp(preg_, pattern, REG_EXTENDED) != 0)
{
FatalErrorIn
(
"regExp::compile(const char*)"
) << "Failed to compile regular expression '" << pat << "'"
) << "Failed to compile regular expression '" << pattern << "'"
<< exit(FatalError);
}
}
}
void Foam::regExp::clear() const
{
if (preg_)
{
regfree(preg_);
delete preg_;
preg_ = 0;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::regExp::regExp()
@ -74,19 +64,19 @@ Foam::regExp::regExp()
{}
Foam::regExp::regExp(const string& pat)
Foam::regExp::regExp(const char* pattern)
:
preg_(0)
{
compile(pat.c_str());
compile(pattern);
}
Foam::regExp::regExp(const char* pat)
Foam::regExp::regExp(const std::string& pattern)
:
preg_(0)
{
compile(pat);
compile(pattern.c_str());
}
@ -100,17 +90,22 @@ Foam::regExp::~regExp()
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
int Foam::regExp::ngroups() const
bool Foam::regExp::clear() const
{
return preg_ ? preg_->re_nsub : 0;
if (preg_)
{
regfree(preg_);
delete preg_;
preg_ = 0;
return true;
}
return false;
}
bool Foam::regExp::match
(
const string& str,
bool partialMatch
) const
bool Foam::regExp::match(const std::string& str, bool partial) const
{
if (preg_ && str.size())
{
@ -124,7 +119,7 @@ bool Foam::regExp::match
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&&
(
partialMatch
partial
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
)
@ -137,12 +132,7 @@ bool Foam::regExp::match
}
bool Foam::regExp::match
(
const string& str,
List<string>& groups,
bool partialMatch
) const
bool Foam::regExp::match(const string& str, List<string>& groups) const
{
if (preg_ && str.size())
{
@ -155,11 +145,7 @@ bool Foam::regExp::match
if
(
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&&
(
partialMatch
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
{
groups.setSize(ngroups());
@ -193,16 +179,16 @@ bool Foam::regExp::match
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
void Foam::regExp::operator=(const string& pat)
{
compile(pat.c_str());
}
void Foam::regExp::operator=(const char* pat)
{
compile(pat);
}
void Foam::regExp::operator=(const std::string& pat)
{
compile(pat.c_str());
}
// ************************************************************************* //

View File

@ -44,6 +44,7 @@ SourceFiles
#define regExp_H
#include <regex.h>
#include <string>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -67,10 +68,7 @@ class regExp
// Private member functions
//- release allocated space
void clear() const;
//- compile into a regular expression
//- Compile into a regular expression
void compile(const char*) const;
//- Disallow default bitwise copy construct
@ -81,48 +79,72 @@ class regExp
public:
//- Is character a regular expression meta-character?
// any character: '.' \n
// quantifiers: '*', '+', '?' \n
// grouping: '(', '|', ')' \n
// range: '[', ']' \n
//
// Don't bother checking for '{digit}' bounds
inline static bool meta(char c)
{
return
(
(c == '.') // any character
|| (c == '*' || c == '+' || c == '?') // quantifiers
|| (c == '(' || c == ')' || c == '|') // grouping/branching
|| (c == '[' || c == ']') // range
);
}
// Constructors
//- Construct null
regExp();
//- Construct from string
regExp(const string&);
//- Construct from character array
regExp(const char*);
//- Construct from std::string (or string)
regExp(const std::string&);
// Destructor
~regExp();
// Member functions
//- Is the precompiled expression set?
inline bool exists() const
{
return preg_ ? true : false;
}
//- Return the number of (groups)
int ngroups() const;
inline int ngroups() const
{
return preg_ ? preg_->re_nsub : 0;
}
//- Release precompiled expression.
// Returns true if precompiled expression existed before clear
bool clear() const;
//- Return true if it matches, partial matches are optional
bool match
(
const string&,
bool partialMatch=false
) const;
bool match(const std::string&, bool partial=false) const;
//- Return true if it matches and sets the sub-groups matched
bool match(const string&, List<string>& groups) const;
//- Return true if it matches and sets the sub-groups matched,
// partial matches are optional
bool match
(
const string&,
List<string>& groups,
bool partialMatch=false
) const;
// Member Operators
//- Assign from a string
void operator=(const string&);
//- Assign from a string and compile regular expression
void operator=(const std::string&);
//- Assign from a character array
//- Assign from a character array and compile regular expression
void operator=(const char*);
};