- dropped regularExpression in favour of regExp

- moved findStrings from stringList to new stringListOps
  (helps reduce the influence on dependencies)
- findStrings can also do partial matches
This commit is contained in:
Mark Olesen 2008-12-10 14:29:19 +01:00
parent 4aaa07cc14
commit c6cf2e539a
9 changed files with 113 additions and 189 deletions

View File

@ -26,7 +26,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "stringList.H"
#include "stringListOps.H"
#include "IOstreams.H"
using namespace Foam;

View File

@ -1,125 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 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::regularExpression
Description
Wrapper around regular expressions.
SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef regularExpression_H
#define regularExpression_H
#include <sys/types.h>
#include <regex.h>
#include "string.H"
#include "IOstreams.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class regularExpression Declaration
\*---------------------------------------------------------------------------*/
class regularExpression
{
// Private data
//- Precompiled regular expression
regex_t* preg_;
// Private member functions
//- Disallow default bitwise copy construct
regularExpression(const regularExpression&);
//- Disallow default bitwise assignment
void operator=(const regularExpression&);
public:
// Constructors
//- Construct from string
inline regularExpression(const string& s)
{
preg_ = new regex_t;
if (regcomp(preg_, s.c_str(), REG_EXTENDED) != 0)
{
FatalErrorIn
(
"regularExpression::regularExpression(const char*)"
) << "Failed to compile regular expression " << s
<< exit(FatalError);
}
}
// Destructor
//- Construct from string
inline ~regularExpression()
{
if (preg_)
{
regfree(preg_);
delete preg_;
}
}
// Member functions
//- Matches?
inline bool matches(const string& s) const
{
size_t nmatch = 1;
regmatch_t pmatch[1];
int errVal = regexec(preg_, s.c_str(), nmatch, pmatch, 0);
return (errVal == 0 && pmatch[0].rm_eo == label(s.size()));
}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -27,7 +27,7 @@ License
#include "dictionary.H"
#include "primitiveEntry.H"
#include "dictionaryEntry.H"
#include "regularExpression.H"
#include "regExp.H"
/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
@ -43,7 +43,7 @@ bool Foam::dictionary::findInWildcards
const bool wildCardMatch,
const word& Keyword,
DLList<entry*>::const_iterator& wcLink,
DLList<autoPtr<regularExpression> >::const_iterator& reLink
DLList<autoPtr<regExp> >::const_iterator& reLink
) const
{
if (wildCardEntries_.size() > 0)
@ -57,7 +57,7 @@ bool Foam::dictionary::findInWildcards
{
return true;
}
else if (wildCardMatch && reLink()->matches(Keyword))
else if (wildCardMatch && reLink()->match(Keyword))
{
return true;
}
@ -76,7 +76,7 @@ bool Foam::dictionary::findInWildcards
const bool wildCardMatch,
const word& Keyword,
DLList<entry*>::iterator& wcLink,
DLList<autoPtr<regularExpression> >::iterator& reLink
DLList<autoPtr<regExp> >::iterator& reLink
)
{
if (wildCardEntries_.size() > 0)
@ -87,7 +87,7 @@ bool Foam::dictionary::findInWildcards
{
return true;
}
else if (wildCardMatch && reLink()->matches(Keyword))
else if (wildCardMatch && reLink()->match(Keyword))
{
return true;
}
@ -133,10 +133,7 @@ Foam::dictionary::dictionary
wildCardEntries_.insert(&iter());
wildCardRegexps_.insert
(
autoPtr<regularExpression>
(
new regularExpression(iter().keyword())
)
autoPtr<regExp>(new regExp(iter().keyword()))
);
}
}
@ -166,10 +163,7 @@ Foam::dictionary::dictionary
wildCardEntries_.insert(&iter());
wildCardRegexps_.insert
(
autoPtr<regularExpression>
(
new regularExpression(iter().keyword())
)
autoPtr<regExp>(new regExp(iter().keyword()))
);
}
}
@ -229,7 +223,7 @@ bool Foam::dictionary::found(const word& keyword, bool recursive) const
if (wildCardEntries_.size() > 0)
{
DLList<entry*>::const_iterator wcLink = wildCardEntries_.begin();
DLList<autoPtr<regularExpression> >::const_iterator reLink =
DLList<autoPtr<regExp> >::const_iterator reLink =
wildCardRegexps_.begin();
// Find in wildcards using regular expressions only
@ -266,7 +260,7 @@ const Foam::entry* Foam::dictionary::lookupEntryPtr
{
DLList<entry*>::const_iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regularExpression> >::const_iterator reLink =
DLList<autoPtr<regExp> >::const_iterator reLink =
wildCardRegexps_.begin();
// Find in wildcards using regular expressions only
@ -305,7 +299,7 @@ Foam::entry* Foam::dictionary::lookupEntryPtr
{
DLList<entry*>::iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regularExpression> >::iterator reLink =
DLList<autoPtr<regExp> >::iterator reLink =
wildCardRegexps_.begin();
// Find in wildcards using regular expressions only
if (findInWildcards(wildCardMatch, keyword, wcLink, reLink))
@ -487,10 +481,7 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
wildCardEntries_.insert(entryPtr);
wildCardRegexps_.insert
(
autoPtr<regularExpression>
(
new regularExpression(entryPtr->keyword())
)
autoPtr<regExp>(new regExp(entryPtr->keyword()))
);
}
@ -519,9 +510,9 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry)
wildCardEntries_.insert(entryPtr);
wildCardRegexps_.insert
(
autoPtr<regularExpression>
autoPtr<regExp>
(
new regularExpression(entryPtr->keyword())
new regExp(entryPtr->keyword())
)
);
}
@ -615,8 +606,7 @@ bool Foam::dictionary::remove(const word& Keyword)
// Delete from wildcards first
DLList<entry*>::iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regularExpression> >::iterator reLink =
wildCardRegexps_.begin();
DLList<autoPtr<regExp> >::iterator reLink = wildCardRegexps_.begin();
// Find in wildcards using exact match only
if (findInWildcards(false, Keyword, wcLink, reLink))
@ -683,7 +673,7 @@ bool Foam::dictionary::changeKeyword
// Delete from wildcards first
DLList<entry*>::iterator wcLink =
wildCardEntries_.begin();
DLList<autoPtr<regularExpression> >::iterator reLink =
DLList<autoPtr<regExp> >::iterator reLink =
wildCardRegexps_.begin();
// Find in wildcards using exact match only
@ -722,10 +712,7 @@ bool Foam::dictionary::changeKeyword
wildCardEntries_.insert(iter());
wildCardRegexps_.insert
(
autoPtr<regularExpression>
(
new regularExpression(newKeyword)
)
autoPtr<regExp>(new regExp(newKeyword))
);
}

View File

@ -67,7 +67,7 @@ namespace Foam
{
// Forward declaration of friend functions and operators
class regularExpression;
class regExp;
class dictionary;
Istream& operator>>(Istream&, dictionary&);
Ostream& operator<<(Ostream&, const dictionary&);
@ -95,8 +95,8 @@ class dictionary
//- Wildcard entries
DLList<entry*> wildCardEntries_;
//- Wildcard precompiled regex
DLList<autoPtr<regularExpression> > wildCardRegexps_;
//- Wildcard precompiled regular expressions
DLList<autoPtr<regExp> > wildCardRegexps_;
// Private Member Functions
@ -107,7 +107,7 @@ class dictionary
const bool wildCardMatch,
const word& Keyword,
DLList<entry*>::const_iterator& wcLink,
DLList<autoPtr<regularExpression> >::const_iterator& reLink
DLList<autoPtr<regExp> >::const_iterator& reLink
) const;
//- Search wildcard table either for exact match or for regular
@ -117,7 +117,7 @@ class dictionary
const bool wildCardMatch,
const word& Keyword,
DLList<entry*>::iterator& wcLink,
DLList<autoPtr<regularExpression> >::iterator& reLink
DLList<autoPtr<regExp> >::iterator& reLink
);

View File

@ -27,7 +27,7 @@ License
#include "dictionary.H"
#include "IFstream.H"
#include "inputModeEntry.H"
#include "regularExpression.H"
#include "regExp.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //

View File

@ -28,6 +28,7 @@ License
#include "polyMesh.H"
#include "primitiveMesh.H"
#include "processorPolyPatch.H"
#include "stringListOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -420,13 +421,13 @@ Foam::labelHashSet Foam::polyBoundaryMesh::patchSet
forAll(patchNames, i)
{
// Treat the diven patch names as wild-cards and search the set
// Treat the given patch names as wild-cards and search the set
// of all patch names for matches
labelList patchIDs = findStrings(patchNames[i], allPatchNames);
if (patchIDs.size() == 0)
{
WarningIn("polyBoundaryMesh::patchSet(const wordList& patchNames)")
WarningIn("polyBoundaryMesh::patchSet(const wordList&)")
<< "Cannot find any patch names matching " << patchNames[i]
<< endl;
}

View File

@ -34,27 +34,15 @@ Description
#define stringList_H
#include "string.H"
#include "labelList.H"
#include "List.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef List<string> stringList;
//- Return the indices of the strings in the list
// that match the given regular expression
template<class StringList>
labelList findStrings(const string& regexp, const StringList&);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "stringListTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif

View File

@ -0,0 +1,70 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 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
InNamspace
Foam
Description
Operations on lists of strings.
SourceFiles
stringListOpsTemplates.C
\*---------------------------------------------------------------------------*/
#ifndef stringListOps_H
#define stringListOps_H
#include "labelList.H"
#include "stringList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
//- Return the indices of the strings in the list
// that match the given regular expression
// partial matches are optional
template<class StringType>
labelList findStrings
(
const string& regexpPattern,
const UList<StringType>&,
bool partialMatch=false
);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
# include "stringListOpsTemplates.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -25,7 +25,7 @@ License
\*---------------------------------------------------------------------------*/
#include "labelList.H"
#include "regularExpression.H"
#include "regExp.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -34,25 +34,28 @@ namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class StringList>
labelList findStrings(const string& regexp, const StringList& sl)
template<class StringType>
labelList findStrings
(
const string& pattern,
const UList<StringType>& lst,
bool partialMatch
)
{
labelList matches(sl.size());
regExp re(pattern);
labelList matched(lst.size());
regularExpression re(regexp);
label matchi = 0;
forAll(sl, i)
label matchI = 0;
forAll(lst, elemI)
{
if (re.matches(sl[i]))
if (re.match(lst[elemI], partialMatch))
{
matches[matchi++] = i;
matched[matchI++] = elemI;
}
}
matched.setSize(matchI);
matches.setSize(matchi);
return matches;
return matched;
}