Files
openfoam/src/OSspecific/Unix/regExp.H
Mark Olesen 3c5852ebfc 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).
2009-01-05 17:02:58 +01:00

185 lines
5.3 KiB
C++

/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::regExp
Description
Wrapper around POSIX extended regular expressions.
SeeAlso
The manpage regex(7) for more information about POSIX regular expressions.
These differ somewhat from @c Perl and @c sed regular expressions.
SourceFiles
regExp.C
\*---------------------------------------------------------------------------*/
#ifndef regExp_H
#define regExp_H
#include <regex.h>
#include <string>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class string;
template<class T> class List;
/*---------------------------------------------------------------------------*\
Class regExp Declaration
\*---------------------------------------------------------------------------*/
class regExp
{
// Private data
//- Precompiled regular expression
mutable regex_t* preg_;
// Private member functions
//- Disallow default bitwise copy construct
regExp(const regExp&);
//- Disallow default bitwise assignment
void operator=(const 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 character array, optionally ignoring case
regExp(const char*, const bool ignoreCase=false);
//- Construct from std::string (or string), optionally ignoring case
regExp(const std::string&, const bool ignoreCase=false);
// Destructor
~regExp();
// Member functions
//- Access
//- Does a precompiled expression exist?
inline bool exists() const
{
return preg_ ? true : false;
}
//- Return the number of (groups)
inline int ngroups() const
{
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;
//- Return true if it matches the entire string
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string&) const;
//- Return true if it matches and sets the sub-groups matched
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const string&, List<string>& groups) const;
//- Return true if the regex was found in within string
bool search(const std::string& str) const
{
return std::string::npos != find(str);
}
// Member Operators
//- 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&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //