Commit 2717aa5c authored by Mark Olesen's avatar Mark Olesen
Browse files

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
parent 1d866d7f
......@@ -40,6 +40,25 @@ int main(int argc, char *argv[])
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", "") << endl;
Info<< string(test).replaceAll("kj", "zzz") << endl;
......
wordReTest.C
EXE = $(FOAM_USER_APPBIN)/wordReTest
/*-------------------------------*- 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" )
)
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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;
}
// ************************************************************************* //
......@@ -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,15 +179,15 @@ bool Foam::regExp::match
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
void Foam::regExp::operator=(const string& pat)
void Foam::regExp::operator=(const char* pat)
{
compile(pat.c_str());
compile(pat);
}
void Foam::regExp::operator=(const char* pat)
void Foam::regExp::operator=(const std::string& pat)
{
compile(pat);
compile(pat.c_str());
}
......
......@@ -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;
//- 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;
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;
// 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*);
};
......
......@@ -40,6 +40,7 @@ $(strings)/word/wordIO.C
$(strings)/fileName/fileName.C
$(strings)/fileName/fileNameIO.C
$(strings)/keyType/keyTypeIO.C
$(strings)/wordRe/wordReIO.C
primitives/random/Random.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
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
// ************************************************************************* //
......@@ -37,14 +37,11 @@ const Foam::fileName Foam::fileName::null;
// * * * * * * * * * * * * * * * * 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)/wrdList[i]);
}
operator=((*this)/lst[elemI]);
}
}
......@@ -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:
// input path() dirname
......@@ -205,35 +202,35 @@ Foam::fileName::Type Foam::fileName::type() const
// * * * * * * * * * * * * * * * 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();
}
void Foam::fileName::operator=(const std::string& q)
void Foam::fileName::operator=(const std::string& str)
{
string::operator=(q);
string::operator=(str);
stripInvalid();
}
void Foam::fileName::operator=(const char* q)
void Foam::fileName::operator=(const char* str)
{
string::operator=(q);
string::operator=(str);
stripInvalid();
}
......
......@@ -64,7 +64,7 @@ Ostream& operator<<(Ostream&, const fileName&);
/*---------------------------------------------------------------------------*\
Class fileName Declaration
Class fileName Declaration
\*---------------------------------------------------------------------------*/
class fileName
......@@ -73,7 +73,7 @@ class fileName
{
// Private member functions
//- Strip invalid characters from this word
//- Strip invalid characters
inline void stripInvalid();
......@@ -102,16 +102,16 @@ public:
inline fileName();
//- Construct as copy
inline fileName(const fileName& fn);
inline fileName(const fileName&);
//- Construct as copy of word
inline fileName(const word& w);
inline fileName(const word&);
//- Construct as copy of string
inline fileName(const string& s);
inline fileName(const string&);
//- Construct as copy of std::string
inline fileName(const std::string& s);
inline fileName(const std::string&);
//- Construct as copy of character array
inline fileName(const char*);
......@@ -125,7 +125,7 @@ public:
// Member functions
//- Is this character valid for a fileName
//- Is this character valid for a fileName?
inline static bool valid(char);