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

@ -40,6 +40,25 @@ int main(int argc, char *argv[])
Info<< test << endl; Info<< test << endl;
// test sub-strings via iterators
string::const_iterator iter = test.end();
string::const_iterator iter2 = test.end();
string::size_type fnd = test.find('\\');
if (fnd != string::npos)
{
iter = test.begin() + fnd;
iter2 = iter + 6;
}
Info<< "sub-string via iterators : >";
while (iter != iter2)
{
Info<< *iter;
iter++;
}
Info<< "<\n";
Info<< string(test).replace("kj", "zzz") << endl; Info<< string(test).replace("kj", "zzz") << endl;
Info<< string(test).replace("kj", "") << endl; Info<< string(test).replace("kj", "") << endl;
Info<< string(test).replaceAll("kj", "zzz") << endl; Info<< string(test).replaceAll("kj", "zzz") << endl;

View File

@ -0,0 +1,3 @@
wordReTest.C
EXE = $(FOAM_USER_APPBIN)/wordReTest

View File

View File

@ -0,0 +1,32 @@
/*-------------------------------*- C++ -*---------------------------------*\
| ========= |
| \\ / OpenFOAM |
| \\ / |
| \\ / The Open Source CFD Toolbox |
| \\/ http://www.OpenFOAM.org |
\*-------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// wordRe, string
(
( "a.*" "abc" )
( "a.*" "bac" )
( "a.*" "abcd" )
( "a.*" "def" )
( "d(.*)f" "def" )
( "plain" "def" )
( "plain" "def" )
( "plain\\(0\\)" "def" )
( "plain\(0\)" "ghi" )
( "regex(0)" "ghi" )
( "plain\\\(0\\\)" "ghi" )
( "this" "def" )
( "this" "this" )
( plain\\(0\\) "def" )
( plain\(0\) "ghi" )
( plain\\\(0\\\) "ghi" )
( "done" "done" )
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -0,0 +1,105 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
Description
\*---------------------------------------------------------------------------*/
#include "IOstreams.H"
#include "IOobject.H"
#include "IFstream.H"
#include "List.H"
#include "Tuple2.H"
#include "wordRe.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
wordRe wre;
std::string s1("this .* file");
Foam::string s2("this .* file");
const char * s3 = "this .* file";
Info<< wordRe(s1).info() << endl;
Info<< wordRe(s2, false).info() << endl;
Info<< wordRe(s2).info() << endl;
Info<< wordRe(s3, true).info() << endl;
wre = "this .* file";
Info<< wre.info() << endl;
wre = s1;
Info<< wre.info() << endl;
wre.uncompile();
Info<< wre.info() << " uncompiled" << endl;
wre = "something";
Info<< wre.info() << " before" << endl;
wre.uncompile();
Info<< wre.info() << " uncompiled" << endl;
wre.compile(true);
Info<< wre.info() << " after auto-detect" << endl;
wre = "something .* value";
Info<< wre.info() << " before" << endl;
wre.uncompile();
Info<< wre.info() << " uncompiled" << endl;
wre.compile(true);
Info<< wre.info() << " after auto-detect" << endl;
wre.uncompile();
Info<< wre.info() << " uncompiled" << endl;
wre.recompile();
Info<< wre.info() << " recompiled" << endl;
IOobject::writeDivider(Info);
List<Tuple2<wordRe, string> > rawList(IFstream("testRegexps")());
Info<< "input list:" << rawList << endl;
IOobject::writeDivider(Info);
Info<< endl;
forAll(rawList, elemI)
{
const wordRe& wre = rawList[elemI].first();
const string& str = rawList[elemI].second();
Info<< wre.info()
<< " equals:" << (wre == str)
<< "(" << wre.match(str, true) << ")"
<< " match:" << wre.match(str)
<< " str=" << str
<< endl;
}
Info<< endl;
return 0;
}
// ************************************************************************* //

View File

@ -25,6 +25,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
#include <sys/types.h> #include <sys/types.h>
#include "regExp.H" #include "regExp.H"
#include "label.H" #include "label.H"
#include "string.H" #include "string.H"
@ -34,38 +35,27 @@ License
// * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
void Foam::regExp::compile(const char* pat) const void Foam::regExp::compile(const char* pattern) const
{ {
clear(); clear();
// avoid NULL and zero-length patterns // avoid NULL pointer and zero-length patterns
if (pat && *pat) if (pattern && *pattern)
{ {
preg_ = new regex_t; preg_ = new regex_t;
if (regcomp(preg_, pat, REG_EXTENDED) != 0) if (regcomp(preg_, pattern, REG_EXTENDED) != 0)
{ {
FatalErrorIn FatalErrorIn
( (
"regExp::compile(const char*)" "regExp::compile(const char*)"
) << "Failed to compile regular expression '" << pat << "'" ) << "Failed to compile regular expression '" << pattern << "'"
<< exit(FatalError); << exit(FatalError);
} }
} }
} }
void Foam::regExp::clear() const
{
if (preg_)
{
regfree(preg_);
delete preg_;
preg_ = 0;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::regExp::regExp() Foam::regExp::regExp()
@ -74,19 +64,19 @@ Foam::regExp::regExp()
{} {}
Foam::regExp::regExp(const string& pat) Foam::regExp::regExp(const char* pattern)
: :
preg_(0) preg_(0)
{ {
compile(pat.c_str()); compile(pattern);
} }
Foam::regExp::regExp(const char* pat) Foam::regExp::regExp(const std::string& pattern)
: :
preg_(0) preg_(0)
{ {
compile(pat); compile(pattern.c_str());
} }
@ -100,17 +90,22 @@ Foam::regExp::~regExp()
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * 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 bool Foam::regExp::match(const std::string& str, bool partial) const
(
const string& str,
bool partialMatch
) const
{ {
if (preg_ && str.size()) if (preg_ && str.size())
{ {
@ -124,7 +119,7 @@ bool Foam::regExp::match
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0 regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&& &&
( (
partialMatch partial
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size())) || (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
) )
) )
@ -137,12 +132,7 @@ bool Foam::regExp::match
} }
bool Foam::regExp::match bool Foam::regExp::match(const string& str, List<string>& groups) const
(
const string& str,
List<string>& groups,
bool partialMatch
) const
{ {
if (preg_ && str.size()) if (preg_ && str.size())
{ {
@ -155,11 +145,7 @@ bool Foam::regExp::match
if if
( (
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0 regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&& && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
(
partialMatch
|| (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
)
) )
{ {
groups.setSize(ngroups()); groups.setSize(ngroups());
@ -193,16 +179,16 @@ bool Foam::regExp::match
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
void Foam::regExp::operator=(const string& pat)
{
compile(pat.c_str());
}
void Foam::regExp::operator=(const char* pat) void Foam::regExp::operator=(const char* pat)
{ {
compile(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 #define regExp_H
#include <regex.h> #include <regex.h>
#include <string>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -67,10 +68,7 @@ class regExp
// Private member functions // Private member functions
//- release allocated space //- Compile into a regular expression
void clear() const;
//- compile into a regular expression
void compile(const char*) const; void compile(const char*) const;
//- Disallow default bitwise copy construct //- Disallow default bitwise copy construct
@ -81,48 +79,72 @@ class regExp
public: 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 // Constructors
//- Construct null //- Construct null
regExp(); regExp();
//- Construct from string
regExp(const string&);
//- Construct from character array //- Construct from character array
regExp(const char*); regExp(const char*);
//- Construct from std::string (or string)
regExp(const std::string&);
// Destructor // Destructor
~regExp(); ~regExp();
// Member functions // Member functions
//- Is the precompiled expression set?
inline bool exists() const
{
return preg_ ? true : false;
}
//- Return the number of (groups) //- 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 //- Return true if it matches, partial matches are optional
bool match bool match(const std::string&, bool partial=false) const;
(
const string&, //- Return true if it matches and sets the sub-groups matched
bool partialMatch=false bool match(const string&, List<string>& groups) const;
) 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 // Member Operators
//- Assign from a string //- Assign from a string and compile regular expression
void operator=(const string&); void operator=(const std::string&);
//- Assign from a character array //- Assign from a character array and compile regular expression
void operator=(const char*); void operator=(const char*);
}; };

View File

@ -40,6 +40,7 @@ $(strings)/word/wordIO.C
$(strings)/fileName/fileName.C $(strings)/fileName/fileName.C
$(strings)/fileName/fileNameIO.C $(strings)/fileName/fileNameIO.C
$(strings)/keyType/keyTypeIO.C $(strings)/keyType/keyTypeIO.C
$(strings)/wordRe/wordReIO.C
primitives/random/Random.C primitives/random/Random.C

View File

@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
Typedef
Foam::wordReList
Description
List of wordRe (word or regular expression)
\*---------------------------------------------------------------------------*/
#ifndef wordReList_H
#define wordReList_H
#include "wordRe.H"
#include "List.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef List<wordRe> wordReList;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -37,14 +37,11 @@ const Foam::fileName Foam::fileName::null;
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::fileName::fileName(const wordList& wrdList) Foam::fileName::fileName(const wordList& lst)
{ {
if (wrdList.size() != 0) forAll(lst, elemI)
{ {
forAll(wrdList, i) operator=((*this)/lst[elemI]);
{
operator=((*this)/wrdList[i]);
}
} }
} }
@ -77,7 +74,7 @@ Foam::word Foam::fileName::name() const
} }
//- Return directory path name (part before last /) // Return directory path name (part before last /)
// //
// behaviour compared to /usr/bin/dirname: // behaviour compared to /usr/bin/dirname:
// input path() dirname // input path() dirname
@ -205,35 +202,35 @@ Foam::fileName::Type Foam::fileName::type() const
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
void Foam::fileName::operator=(const fileName& q) void Foam::fileName::operator=(const fileName& str)
{ {
string::operator=(q); string::operator=(str);
} }
void Foam::fileName::operator=(const word& q) void Foam::fileName::operator=(const word& str)
{ {
string::operator=(q); string::operator=(str);
} }
void Foam::fileName::operator=(const string& q) void Foam::fileName::operator=(const string& str)
{ {
string::operator=(q); string::operator=(str);
stripInvalid(); stripInvalid();
} }
void Foam::fileName::operator=(const std::string& q) void Foam::fileName::operator=(const std::string& str)
{ {
string::operator=(q); string::operator=(str);
stripInvalid(); stripInvalid();
} }
void Foam::fileName::operator=(const char* q) void Foam::fileName::operator=(const char* str)
{ {
string::operator=(q); string::operator=(str);
stripInvalid(); stripInvalid();
} }

View File

@ -73,7 +73,7 @@ class fileName
{ {
// Private member functions // Private member functions
//- Strip invalid characters from this word //- Strip invalid characters
inline void stripInvalid(); inline void stripInvalid();
@ -102,16 +102,16 @@ public:
inline fileName(); inline fileName();
//- Construct as copy //- Construct as copy
inline fileName(const fileName& fn); inline fileName(const fileName&);
//- Construct as copy of word //- Construct as copy of word
inline fileName(const word& w); inline fileName(const word&);
//- Construct as copy of string //- Construct as copy of string
inline fileName(const string& s); inline fileName(const string&);
//- Construct as copy of std::string //- Construct as copy of std::string
inline fileName(const std::string& s); inline fileName(const std::string&);
//- Construct as copy of character array //- Construct as copy of character array
inline fileName(const char*); inline fileName(const char*);
@ -125,7 +125,7 @@ public:
// Member functions // Member functions
//- Is this character valid for a fileName //- Is this character valid for a fileName?
inline static bool valid(char); inline static bool valid(char);

View File

@ -74,17 +74,17 @@ inline Foam::fileName::fileName(const string& str)
} }
inline Foam::fileName::fileName(const std::string& stdStr) inline Foam::fileName::fileName(const std::string& str)
: :
string(stdStr) string(str)
{ {
stripInvalid(); stripInvalid();
} }
inline Foam::fileName::fileName(const char* chars) inline Foam::fileName::fileName(const char* str)
: :
string(chars) string(str)
{ {
stripInvalid(); stripInvalid();
} }

View File

@ -53,9 +53,9 @@ Foam::Istream& Foam::operator>>(Istream& is, fileName& fn)
} }
Foam::Ostream& Foam::operator<<(Ostream& os, const fileName& s) Foam::Ostream& Foam::operator<<(Ostream& os, const fileName& fn)
{ {
os.write(s); os.write(fn);
os.check("Ostream& operator<<(Ostream&, const fileName&)"); os.check("Ostream& operator<<(Ostream&, const fileName&)");
return os; return os;
} }

View File

@ -41,12 +41,7 @@ Foam::string::size_type Foam::string::count(const char c) const
{ {
register size_type cCount = 0; register size_type cCount = 0;
for for (const_iterator iter = begin(); iter != end(); ++iter)
(
const_iterator iter = begin();
iter != end();
++iter
)
{ {
if (*iter == c) if (*iter == c)
{ {
@ -269,9 +264,9 @@ bool Foam::string::removeRepeated(const char character)
// Return string with repeated characters removed // Return string with repeated characters removed
Foam::string Foam::string::removeRepeated(const char character) const Foam::string Foam::string::removeRepeated(const char character) const
{ {
string s(*this); string str(*this);
s.removeRepeated(character); str.removeRepeated(character);
return s; return str;
} }
@ -294,9 +289,9 @@ bool Foam::string::removeTrailing(const char character)
// Return string with trailing character removed // Return string with trailing character removed
Foam::string Foam::string::removeTrailing(const char character) const Foam::string Foam::string::removeTrailing(const char character) const
{ {
string s(*this); string str(*this);
s.removeTrailing(character); str.removeTrailing(character);
return s; return str;
} }

View File

@ -88,18 +88,10 @@ public:
//- Hashing function class //- Hashing function class
class hash class hash
{ {
public: public:
inline hash(); inline hash();
inline size_type operator()(const string&) const;
inline size_type operator()(const string& key) const; inline size_type operator()(const string&, const size_type) const;
inline size_type operator()
(
const string& key,
const size_type tableSize
) const;
}; };
@ -135,6 +127,11 @@ public:
template<class String> template<class String>
static inline bool valid(const string&); static inline bool valid(const string&);
//- Does this string have particular meta-characters?
// The meta characters can be optionally quoted.
template<class String>
static inline bool meta(const string&, const char quote='\\');
// Edit // Edit
@ -146,6 +143,11 @@ public:
template<class String> template<class String>
static inline String validate(const string&); static inline String validate(const string&);
//- Return a String with quoted meta-characters from the given string
template<class String>
static inline string quotemeta(const string&, const char quote='\\');
//- Replace first occurence of sub-string oldStr with newStr //- Replace first occurence of sub-string oldStr with newStr
// starting at start // starting at start
string& replace string& replace
@ -180,16 +182,16 @@ public:
string& expand(); string& expand();
//- Remove repeated characters returning true if string changed //- Remove repeated characters returning true if string changed
bool removeRepeated(const char character); bool removeRepeated(const char);
//- Return string with repeated characters removed //- Return string with repeated characters removed
string removeRepeated(const char character) const; string removeRepeated(const char) const;
//- Remove trailing character returning true if string changed //- Remove trailing character returning true if string changed
bool removeTrailing(const char character); bool removeTrailing(const char);
//- Return string with trailing character removed //- Return string with trailing character removed
string removeTrailing(const char character) const; string removeTrailing(const char) const;
// Member Operators // Member Operators

View File

@ -62,9 +62,9 @@ inline Foam::string::string(const char c)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class String> template<class String>
inline bool Foam::string::valid(const string& s) inline bool Foam::string::valid(const string& str)
{ {
for (const_iterator iter = s.begin(); iter != s.end(); iter++) for (const_iterator iter = str.begin(); iter != str.end(); iter++)
{ {
if (!String::valid(*iter)) if (!String::valid(*iter))
{ {
@ -76,17 +76,17 @@ inline bool Foam::string::valid(const string& s)
template<class String> template<class String>
inline bool Foam::string::stripInvalid(string& s) inline bool Foam::string::stripInvalid(string& str)
{ {
if (!valid<String>(s)) if (!valid<String>(str))
{ {
register size_type nValid = 0; register size_type nValid = 0;
iterator iter2 = s.begin(); iterator iter2 = str.begin();
for for
( (
const_iterator iter1 = iter2; const_iterator iter1 = iter2;
iter1 != const_cast<const string&>(s).end(); iter1 != const_cast<const string&>(str).end();
iter1++ iter1++
) )
{ {
@ -100,7 +100,7 @@ inline bool Foam::string::stripInvalid(string& s)
} }
} }
s.resize(nValid); str.resize(nValid);
return true; return true;
} }
@ -109,6 +109,66 @@ inline bool Foam::string::stripInvalid(string& s)
} }
template<class String>
inline bool Foam::string::meta(const string& str, const char quote)
{
int escaped = 0;
for (const_iterator iter = str.begin(); iter != str.end(); iter++)
{
if (quote && *iter == quote)
{
escaped ^= 1; // toggle state
}
else if (escaped)
{
escaped = false;
}
else if (String::meta(*iter))
{
return true;
}
}
return false;
}
template<class String>
inline Foam::string
Foam::string::quotemeta(const string& str, const char quote)
{
if (!quote)
{
return str;
}
string sQuoted;
sQuoted.reserve(2*str.length());
int escaped = 0;
for (const_iterator iter = str.begin(); iter != str.end(); iter++)
{
if (*iter == quote)
{
escaped ^= 1; // toggle state
}
else if (escaped)
{
escaped = 0;
}
else if (String::meta(*iter))
{
sQuoted += quote;
}
sQuoted += *iter;
}
sQuoted.resize(sQuoted.length());
return sQuoted;
}
template<class String> template<class String>
inline String Foam::string::validate(const string& str) inline String Foam::string::validate(const string& str)
{ {

View File

@ -0,0 +1,206 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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::wordRe
Description
A wordRe is a word, but can also have a regular expression for matching
words.
Note
If the string contents are changed - eg, by the operator+=() or by
string::replace(), etc - it will be necessary to use compile() or
recompile() to sychronize the regular expression.
THIS IS STILL A DRAFT -- NOT YET RELEASED FOR GENERAL USE
SourceFiles
wordRe.C
wordReIO.C
\*---------------------------------------------------------------------------*/
#ifndef wordRe_H
#define wordRe_H
#include "word.H"
#include "regExp.H"
#include "InfoProxy.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of friend functions and operators
class wordRe;
class Istream;
class Ostream;
Istream& operator>>(Istream&, wordRe&);
Ostream& operator<<(Ostream&, const wordRe&);
/*---------------------------------------------------------------------------*\
Class wordRe Declaration
\*---------------------------------------------------------------------------*/
class wordRe
:
public word
{
// Private member data
//- The regular expression
mutable regExp re_;
public:
//- Is this a meta character?
static inline bool meta(char);
//- Is this character valid for a wordRe
inline static bool valid(char);
//- Test string for regular expression meta characters
static inline bool isPattern(const string&);
// Constructors
//- Construct null
inline wordRe();
//- Construct as copy
inline wordRe(const wordRe&);
//- Construct as copy of word
inline wordRe(const word&);
//- Construct as copy of character array
// Treat as regular expression specified explicitly.
inline wordRe(const char*, const bool asPattern=false);
//- Construct as copy of string.
// Treat as regular expression specified explicitly.
inline wordRe(const string&, const bool asPattern);
//- Construct as copy of string.
// Auto-test for regular expression
inline wordRe(const string&);
//- Construct as copy of std::string
// Treat as regular expression specified explicitly.
inline wordRe(const std::string&, const bool asPattern);
//- Construct as copy of std::string
// Auto-test for regular expression
inline wordRe(const std::string&);
//- Construct from Istream
wordRe(Istream&);
// Destructor
~wordRe();
// Member functions
//- Should be treated as a match rather than a literal string?
inline bool isPattern() const;
//- Create and compile the regular expression
// Optionally detect if it appears to be a regular expression
inline bool compile(const bool detect=false) const;
//- Recompile an existing regular expression
inline bool recompile() const;
//- Frees precompiled regular expression and makes is a literal string.
// Optionally strips invalid word characters
inline void uncompile(const bool doStripInvalid=false) const;
//- Clear string and precompiled regular expression
inline void clear();
//- Smart match as regular expression or as a string
// Optionally specify a literal match only
inline bool match(const string&, bool literalMatch=false) const;
//- Return a string with quoted meta-characters
inline string quotemeta() const;
//- Return info proxy.
InfoProxy<wordRe> info() const
{
return *this;
}
// Member operators
// Assignment
//- Assign copy
inline void operator=(const wordRe&);
//- Copy word, never a regular expression
inline void operator=(const word&);
//- Copy string, auto-test for regular expression
inline void operator=(const string&);
//- Copy string, auto-test for regular expression
inline void operator=(const std::string&);
//- Copy string, auto-test for regular expression
inline void operator=(const char*);
// IOstream operators
friend Istream& operator>>(Istream&, wordRe&);
friend Ostream& operator<<(Ostream&, const wordRe&);
};
template<>
Ostream& operator<<(Ostream&, const InfoProxy<wordRe>&);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "wordReI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,263 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
inline bool Foam::wordRe::meta(char c)
{
return regExp::meta(c);
}
inline bool Foam::wordRe::valid(char c)
{
return
(
!isspace(c)
&& c != '"'
&& c != '/'
);
}
inline bool Foam::wordRe::isPattern(const string& str)
{
return string::meta<regExp>(str);
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::wordRe::wordRe()
:
word(),
re_()
{}
inline Foam::wordRe::wordRe(const wordRe& str)
:
word(str),
re_()
{
if (str.isPattern())
{
compile();
}
}
inline Foam::wordRe::wordRe(const word& str)
:
word(str),
re_()
{}
inline Foam::wordRe::wordRe(const char* str, const bool asPattern)
:
word(str, false),
re_()
{
if (asPattern)
{
compile();
}
}
inline Foam::wordRe::wordRe(const string& str, const bool asPattern)
:
word(str, false),
re_()
{
if (asPattern)
{
compile();
}
}
inline Foam::wordRe::wordRe(const string& str)
:
word(str, false),
re_()
{
compile(true); // auto-detect regex
}
inline Foam::wordRe::wordRe(const std::string& str, const bool asPattern)
:
word(str, false),
re_()
{
if (asPattern)
{
compile();
}
}
inline Foam::wordRe::wordRe(const std::string& str)
:
word(str, false),
re_()
{
compile(true); // auto-detect regex
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::wordRe::~wordRe()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline bool Foam::wordRe::isPattern() const
{
return re_.exists();
}
inline bool Foam::wordRe::compile(const bool detect) const
{
// appears to be a plain word and not a regex
if (detect && string::valid<word>(*this) && !string::meta<regExp>(*this))
{
re_.clear();
}
else
{
re_ = *this;
}
return re_.exists();
}
inline bool Foam::wordRe::recompile() const
{
if (re_.exists())
{
re_ = *this;
}
return re_.exists();
}
inline void Foam::wordRe::uncompile(const bool doStripInvalid) const
{
if (re_.clear())
{
// skip stripping unless debug is active to avoid costly operations
if (word::debug && doStripInvalid)
{
string::stripInvalid<word>
(
const_cast<word&>(static_cast<const word&>(*this))
);
}
}
}
inline void Foam::wordRe::clear()
{
word::clear();
re_.clear();
}
inline bool Foam::wordRe::match(const string& str, bool literalMatch) const
{
if (literalMatch || !re_.exists())
{
// check as string
return (*this == str);
}
else
{
// check as regex
return re_.match(str);
}
}
inline Foam::string Foam::wordRe::quotemeta() const
{
return string::quotemeta<regExp>(*this);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline void Foam::wordRe::operator=(const wordRe& str)
{
string::operator=(str);
if (str.isPattern())
{
compile();
}
else
{
re_.clear();
}
}
inline void Foam::wordRe::operator=(const word& str)
{
word::operator=(str);
re_.clear();
}
inline void Foam::wordRe::operator=(const string& str)
{
string::operator=(str);
compile(true); // auto-detect regex
}
inline void Foam::wordRe::operator=(const std::string& str)
{
string::operator=(str);
compile(true); // auto-detect regex
}
inline void Foam::wordRe::operator=(const char* str)
{
string::operator=(str);
compile(true); // auto-detect regex
}
// ************************************************************************* //

View File

@ -0,0 +1,113 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
\*---------------------------------------------------------------------------*/
#include "wordRe.H"
#include "IOstreams.H"
#include "InfoProxy.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Foam::wordRe::wordRe(Istream& is)
:
word(),
re_(NULL)
{
is >> *this;
}
Foam::Istream& Foam::operator>>(Istream& is, wordRe& w)
{
token t(is);
if (!t.good())
{
is.setBad();
return is;
}
if (t.isWord())
{
w = t.wordToken();
}
else if (t.isString())
{
// Auto-tests for regular expression
w = t.stringToken();
}
else
{
is.setBad();
FatalIOErrorIn("operator>>(Istream&, wordRe&)", is)
<< "wrong token type - expected word or string found "
<< t.info()
<< exit(FatalIOError);
return is;
}
// Check state of IOstream
is.check("Istream& operator>>(Istream&, wordRe&)");
return is;
}
Foam::Ostream& Foam::operator<<(Ostream& os, const wordRe& w)
{
if (w.isPattern())
{
os.write(static_cast<const string&>(w));
}
else
{
os.write(static_cast<const word&>(w));
}
os.check("Ostream& operator<<(Ostream&, const wordRe&)");
return os;
}
template<>
Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy<wordRe>& ip)
{
const wordRe& wre = ip.t_;
if (wre.isPattern())
{
os << "wordRe(regex) " << wre;
}
else
{
os << "wordRe(plain) '" << wre << "'";
}
os.flush();
return os;
}
// ************************************************************************* //