From c6cf2e539a0f719de0b011b5bf532ba4a3afe44c Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 10 Dec 2008 14:29:19 +0100 Subject: [PATCH] - 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 --- applications/test/stringList/stringListTest.C | 2 +- src/OSspecific/Unix/regularExpression.H | 125 ------------------ src/OpenFOAM/db/dictionary/dictionary.C | 45 +++---- src/OpenFOAM/db/dictionary/dictionary.H | 10 +- src/OpenFOAM/db/dictionary/dictionaryIO.C | 2 +- .../polyBoundaryMesh/polyBoundaryMesh.C | 5 +- src/OpenFOAM/primitives/Lists/stringList.H | 14 +- src/OpenFOAM/primitives/Lists/stringListOps.H | 70 ++++++++++ ...stTemplates.C => stringListOpsTemplates.C} | 29 ++-- 9 files changed, 113 insertions(+), 189 deletions(-) delete mode 100644 src/OSspecific/Unix/regularExpression.H create mode 100644 src/OpenFOAM/primitives/Lists/stringListOps.H rename src/OpenFOAM/primitives/Lists/{stringListTemplates.C => stringListOpsTemplates.C} (80%) diff --git a/applications/test/stringList/stringListTest.C b/applications/test/stringList/stringListTest.C index 045ca1fe72..2c7da0d5e9 100644 --- a/applications/test/stringList/stringListTest.C +++ b/applications/test/stringList/stringListTest.C @@ -26,7 +26,7 @@ Description \*---------------------------------------------------------------------------*/ -#include "stringList.H" +#include "stringListOps.H" #include "IOstreams.H" using namespace Foam; diff --git a/src/OSspecific/Unix/regularExpression.H b/src/OSspecific/Unix/regularExpression.H deleted file mode 100644 index fed14379ad..0000000000 --- a/src/OSspecific/Unix/regularExpression.H +++ /dev/null @@ -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 -#include -#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 - -// ************************************************************************* // diff --git a/src/OpenFOAM/db/dictionary/dictionary.C b/src/OpenFOAM/db/dictionary/dictionary.C index c0b4b6fb73..a6a5f62568 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.C +++ b/src/OpenFOAM/db/dictionary/dictionary.C @@ -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::const_iterator& wcLink, - DLList >::const_iterator& reLink + DLList >::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::iterator& wcLink, - DLList >::iterator& reLink + DLList >::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 - ( - new regularExpression(iter().keyword()) - ) + autoPtr(new regExp(iter().keyword())) ); } } @@ -166,10 +163,7 @@ Foam::dictionary::dictionary wildCardEntries_.insert(&iter()); wildCardRegexps_.insert ( - autoPtr - ( - new regularExpression(iter().keyword()) - ) + autoPtr(new regExp(iter().keyword())) ); } } @@ -229,7 +223,7 @@ bool Foam::dictionary::found(const word& keyword, bool recursive) const if (wildCardEntries_.size() > 0) { DLList::const_iterator wcLink = wildCardEntries_.begin(); - DLList >::const_iterator reLink = + DLList >::const_iterator reLink = wildCardRegexps_.begin(); // Find in wildcards using regular expressions only @@ -266,7 +260,7 @@ const Foam::entry* Foam::dictionary::lookupEntryPtr { DLList::const_iterator wcLink = wildCardEntries_.begin(); - DLList >::const_iterator reLink = + DLList >::const_iterator reLink = wildCardRegexps_.begin(); // Find in wildcards using regular expressions only @@ -305,7 +299,7 @@ Foam::entry* Foam::dictionary::lookupEntryPtr { DLList::iterator wcLink = wildCardEntries_.begin(); - DLList >::iterator reLink = + DLList >::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 - ( - new regularExpression(entryPtr->keyword()) - ) + autoPtr(new regExp(entryPtr->keyword())) ); } @@ -519,9 +510,9 @@ bool Foam::dictionary::add(entry* entryPtr, bool mergeEntry) wildCardEntries_.insert(entryPtr); wildCardRegexps_.insert ( - autoPtr + autoPtr ( - new regularExpression(entryPtr->keyword()) + new regExp(entryPtr->keyword()) ) ); } @@ -615,8 +606,7 @@ bool Foam::dictionary::remove(const word& Keyword) // Delete from wildcards first DLList::iterator wcLink = wildCardEntries_.begin(); - DLList >::iterator reLink = - wildCardRegexps_.begin(); + DLList >::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::iterator wcLink = wildCardEntries_.begin(); - DLList >::iterator reLink = + DLList >::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 - ( - new regularExpression(newKeyword) - ) + autoPtr(new regExp(newKeyword)) ); } diff --git a/src/OpenFOAM/db/dictionary/dictionary.H b/src/OpenFOAM/db/dictionary/dictionary.H index 4ace026a01..1d87a08fd6 100644 --- a/src/OpenFOAM/db/dictionary/dictionary.H +++ b/src/OpenFOAM/db/dictionary/dictionary.H @@ -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 wildCardEntries_; - //- Wildcard precompiled regex - DLList > wildCardRegexps_; + //- Wildcard precompiled regular expressions + DLList > wildCardRegexps_; // Private Member Functions @@ -107,7 +107,7 @@ class dictionary const bool wildCardMatch, const word& Keyword, DLList::const_iterator& wcLink, - DLList >::const_iterator& reLink + DLList >::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::iterator& wcLink, - DLList >::iterator& reLink + DLList >::iterator& reLink ); diff --git a/src/OpenFOAM/db/dictionary/dictionaryIO.C b/src/OpenFOAM/db/dictionary/dictionaryIO.C index cb04194915..3e07d7fe0e 100644 --- a/src/OpenFOAM/db/dictionary/dictionaryIO.C +++ b/src/OpenFOAM/db/dictionary/dictionaryIO.C @@ -27,7 +27,7 @@ License #include "dictionary.H" #include "IFstream.H" #include "inputModeEntry.H" -#include "regularExpression.H" +#include "regExp.H" // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // diff --git a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C index e7d7567a13..2d5bd5eb97 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/polyBoundaryMesh/polyBoundaryMesh.C @@ -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; } diff --git a/src/OpenFOAM/primitives/Lists/stringList.H b/src/OpenFOAM/primitives/Lists/stringList.H index 2c02ae1c6f..b3f60e2d11 100644 --- a/src/OpenFOAM/primitives/Lists/stringList.H +++ b/src/OpenFOAM/primitives/Lists/stringList.H @@ -34,27 +34,15 @@ Description #define stringList_H #include "string.H" -#include "labelList.H" +#include "List.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { typedef List stringList; - - //- Return the indices of the strings in the list - // that match the given regular expression - template - labelList findStrings(const string& regexp, const StringList&); } - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -#ifdef NoRepository -# include "stringListTemplates.C" -#endif - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif diff --git a/src/OpenFOAM/primitives/Lists/stringListOps.H b/src/OpenFOAM/primitives/Lists/stringListOps.H new file mode 100644 index 0000000000..e814d88e13 --- /dev/null +++ b/src/OpenFOAM/primitives/Lists/stringListOps.H @@ -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 + labelList findStrings + ( + const string& regexpPattern, + const UList&, + bool partialMatch=false + ); +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository +# include "stringListOpsTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/Lists/stringListTemplates.C b/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C similarity index 80% rename from src/OpenFOAM/primitives/Lists/stringListTemplates.C rename to src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C index a93c46e8ac..e1075b3ea5 100644 --- a/src/OpenFOAM/primitives/Lists/stringListTemplates.C +++ b/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C @@ -25,7 +25,7 @@ License \*---------------------------------------------------------------------------*/ #include "labelList.H" -#include "regularExpression.H" +#include "regExp.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -34,25 +34,28 @@ namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // -template -labelList findStrings(const string& regexp, const StringList& sl) +template +labelList findStrings +( + const string& pattern, + const UList& 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; }