diff --git a/applications/test/stringList/stringListTest.C b/applications/test/stringList/stringListTest.C index 98391ef5be..13718be6f0 100644 --- a/applications/test/stringList/stringListTest.C +++ b/applications/test/stringList/stringListTest.C @@ -27,6 +27,7 @@ Description \*---------------------------------------------------------------------------*/ #include "stringListOps.H" +#include "IStringStream.H" #include "IOstreams.H" using namespace Foam; @@ -36,21 +37,59 @@ using namespace Foam; int main(int argc, char *argv[]) { - stringList sl(3); - sl[0] = "hello"; - sl[1] = "heello"; - sl[2] = "heeello"; + stringList strLst + ( + IStringStream + ( + "(" + "\"hello\"" + "\"heello\"" + "\"heeello\"" + "\"bye\"" + "\"bbye\"" + "\"bbbye\"" + "\"okey\"" + "\"okkey\"" + "\"okkkey\"" + ")" + )() + ); - labelList matches = findStrings(".*ee.*", sl); + wordReList reLst(IStringStream("( okey \"[hy]e+.*\" )")()); - Info<< "matches found for regexp .*ee.* : "; + Info<< "stringList " << strLst << nl; + + labelList matches = findStrings(".*ee.*", strLst); + + Info<< "matches found for regexp .*ee.* :" << nl << matches << nl; forAll(matches, i) { - Info<< " " << sl[matches[i]]; + Info<< " -> " << strLst[matches[i]] << nl; } Info<< endl; - Info << "End\n" << endl; + matches = findStrings(reLst, strLst); + + Info<< "matches found for " << reLst << nl << matches << nl; + forAll(matches, i) + { + Info<< " -> " << strLst[matches[i]] << nl; + } + Info<< endl; + + stringList subLst = subsetStrings(".*ee.*", strLst); + Info<< "subset stringList: " << subLst << nl; + + subLst = subsetStrings(reLst, strLst); + Info<< "subset stringList: " << subLst << nl; + + inplaceSubsetStrings(reLst, strLst); + Info<< "subsetted stringList: " << strLst << nl; + + inplaceSubsetStrings(".*l.*", strLst); + Info<< "subsetted stringList: " << strLst << nl; + + Info<< "\nEnd\n" << endl; return 0; } diff --git a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C index 223b251c43..34154cce48 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C @@ -66,7 +66,9 @@ bool Foam::functionEntries::removeEntry::execute ) { wordList dictKeys = parentDict.toc(); - labelList indices = findStrings(readList(is), dictKeys); + wordReList patterns(is); + + labelList indices = findStrings(patterns, dictKeys); forAll(indices, indexI) { diff --git a/src/OpenFOAM/primitives/Lists/stringListOps.H b/src/OpenFOAM/primitives/Lists/stringListOps.H index 253a249c3d..493a0cb3d3 100644 --- a/src/OpenFOAM/primitives/Lists/stringListOps.H +++ b/src/OpenFOAM/primitives/Lists/stringListOps.H @@ -36,54 +36,262 @@ SourceFiles #ifndef stringListOps_H #define stringListOps_H +#include "regExp.H" #include "labelList.H" #include "stringList.H" #include "wordReList.H" +#include "wordReListMatcher.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { - //- Return list indices for strings matching the regular expression - template - labelList findStrings - ( - const char* regexpPattern, - const UList& - ); - - //- Return list indices for strings matching the regular expression - template - labelList findStrings - ( - const std::string& regexpPattern, - const UList& - ); - - //- Return list indices for strings matching the regular expression - template - labelList findStrings - ( - const wordRe&, - const UList& - ); - - //- Return list indices for strings matching one of the regular expression - template - labelList findStrings - ( - const UList&, - const UList& - ); + // single-string matches: //- Return true if string matches one of the regular expressions - template - bool findStrings + inline bool findStrings ( - const UList&, - const StringType& str + const wordReListMatcher& matcher, + const std::string& str + ) + { + return matcher.match(str); + } + + // multi-string matches: + + //- Return list indices for matching strings + template + labelList findMatchingStrings + ( + const Matcher&, + const UList&, + const bool invert=false ); + //- Return list indices for strings matching the regular expression + // Template partial specialization of findMatchingStrings + template + labelList findStrings + ( + const regExp& re, + const UList& lst, + const bool invert=false + ) + { + return findMatchingStrings(re, lst, invert); + } + + //- Return list indices for strings matching the regular expression + // Template partial specialization of findMatchingStrings + template + labelList findStrings + ( + const char* rePattern, + const UList& lst, + const bool invert=false + ) + { + return findStrings(regExp(rePattern), lst, invert); + } + + //- Return list indices for strings matching the regular expression + // Template partial specialization of findMatchingStrings + template + labelList findStrings + ( + const std::string& rePattern, + const UList& lst, + const bool invert=false + ) + { + return findMatchingStrings(regExp(rePattern), lst, invert); + } + + //- Return list indices for strings matching the regular expression + // Template partial specialization of findMatchingStrings + template + labelList findStrings + ( + const wordRe& wre, + const UList& lst, + const bool invert=false + ) + { + return findMatchingStrings(wre, lst, invert); + } + + + //- Return list indices for strings matching one of the regular expression + // Template partial specialization of findMatchingStrings + template + labelList findStrings + ( + const wordReListMatcher& matcher, + const UList& lst, + const bool invert=false + ) + { + return findMatchingStrings(matcher, lst, invert); + } + + // subsetting multi-string matches (similar to ListOp): + + //- Extract elements of StringList when regular expression matches + // optionally invert the match + // eg, to extract all selected elements: + // subsetMatchingStrings(myRegExp, lst); + template + StringListType subsetMatchingStrings + ( + const Matcher&, + const StringListType&, + const bool invert=false + ); + + //- Extract elements of StringList when regular expression matches + // Template partial specialization of subsetMatchingStrings + template + StringListType subsetStrings + ( + const regExp& re, + const StringListType& lst, + const bool invert=false + ) + { + return subsetMatchingStrings(re, lst, invert); + } + + //- Extract elements of StringList when regular expression matches + // Template partial specialization of subsetMatchingStrings + template + StringListType subsetStrings + ( + const char* rePattern, + const StringListType& lst, + const bool invert=false + ) + { + return subsetMatchingStrings(regExp(rePattern), lst, invert); + } + + //- Extract elements of StringList when regular expression matches + // Template partial specialization of subsetMatchingStrings + template + StringListType subsetStrings + ( + const std::string& rePattern, + const StringListType& lst, + const bool invert=false + ) + { + return subsetMatchingStrings(regExp(rePattern), lst, invert); + } + + //- Extract elements of StringList when regular expression matches + // Template partial specialization of subsetMatchingStrings + template + StringListType subsetStrings + ( + const wordRe& wre, + const StringListType& lst, + const bool invert=false + ) + { + return subsetMatchingStrings(wre, lst, invert); + } + + //- Extract elements of StringList when regular expression matches + // Template partial specialization of subsetMatchingStrings + template + StringListType subsetStrings + ( + const wordReListMatcher& matcher, + const StringListType& lst, + const bool invert=false + ) + { + return subsetMatchingStrings(matcher, lst, invert); + } + + + //- Inplace extract elements of StringList when regular expression matches + // optionally invert the match + // eg, to extract all selected elements: + // inplaceSubsetMatchingStrings(myRegExp, lst); + template + void inplaceSubsetMatchingStrings + ( + const Matcher&, + StringListType&, + const bool invert=false + ); + + //- Inplace extract elements of StringList when regular expression matches + // Template partial specialization of inplaceSubsetMatchingStrings + template + void inplaceSubsetStrings + ( + const regExp& re, + StringListType& lst, + const bool invert=false + ) + { + inplaceSubsetMatchingStrings(re, lst, invert); + } + + //- Inplace extract elements of StringList when regular expression matches + // Template partial specialization of inplaceSubsetMatchingStrings + template + void inplaceSubsetStrings + ( + const char* rePattern, + StringListType& lst, + const bool invert=false + ) + { + inplaceSubsetMatchingStrings(regExp(rePattern), lst, invert); + } + + //- Inplace extract elements of StringList when regular expression matches + // Template partial specialization of inplaceSubsetMatchingStrings + template + void inplaceSubsetStrings + ( + const std::string& rePattern, + StringListType& lst, + const bool invert=false + ) + { + inplaceSubsetMatchingStrings(regExp(rePattern), lst, invert); + } + + //- Inplace extract elements of StringList when regular expression matches + // Template partial specialization of inplaceSubsetMatchingStrings + template + void inplaceSubsetStrings + ( + const wordRe& wre, + StringListType& lst, + const bool invert=false + ) + { + inplaceSubsetMatchingStrings(wre, lst, invert); + } + + //- Inplace extract elements of StringList when regular expression matches + // Template partial specialization of inplaceSubsetMatchingStrings + template + void inplaceSubsetStrings + ( + const wordReListMatcher& matcher, + StringListType& lst, + const bool invert=false + ) + { + inplaceSubsetMatchingStrings(matcher, lst, invert); + } + } diff --git a/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C b/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C index 54f624d60a..6489658d53 100644 --- a/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C +++ b/src/OpenFOAM/primitives/Lists/stringListOpsTemplates.C @@ -24,126 +24,73 @@ License \*---------------------------------------------------------------------------*/ -#include "labelList.H" -#include "regExp.H" - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - -template -Foam::labelList Foam::findStrings +template +Foam::labelList Foam::findMatchingStrings ( - const char* pattern, - const UList& lst + const Matcher& matcher, + const UList& lst, + const bool invert ) { - regExp re(pattern); - labelList matched(lst.size()); + labelList indices(lst.size()); - label matchI = 0; + label nElem = 0; forAll(lst, elemI) { - if (re.match(lst[elemI])) + if (matcher.match(lst[elemI]) ? !invert : invert) { - matched[matchI++] = elemI; + indices[nElem++] = elemI; } } - matched.setSize(matchI); + indices.setSize(nElem); - return matched; + return indices; } -template -Foam::labelList Foam::findStrings +template +StringListType Foam::subsetMatchingStrings ( - const std::string& pattern, - const UList& lst + const Matcher& matcher, + const StringListType& lst, + const bool invert ) { - regExp re(pattern); - labelList matched(lst.size()); + StringListType newLst(lst.size()); - label matchI = 0; + label nElem = 0; forAll(lst, elemI) { - if (re.match(lst[elemI])) + if (matcher.match(lst[elemI]) ? !invert : invert) { - matched[matchI++] = elemI; + newLst[nElem++] = lst[elemI]; } } - matched.setSize(matchI); + newLst.setSize(nElem); - return matched; + return newLst; } -template -Foam::labelList Foam::findStrings +template +void Foam::inplaceSubsetMatchingStrings ( - const wordRe& wre, - const UList& lst + const Matcher& matcher, + StringListType& lst, + const bool invert ) { - labelList matched(lst.size()); - - label matchI = 0; + label nElem = 0; forAll(lst, elemI) { - if (wre.match(lst[elemI])) + if (matcher.match(lst[elemI]) ? !invert : invert) { - matched[matchI++] = elemI; + lst[nElem++] = lst[elemI]; } } - matched.setSize(matchI); - - return matched; -} - - -template -Foam::labelList Foam::findStrings -( - const UList& wreLst, - const UList& lst -) -{ - labelList matched(lst.size()); - - label matchI = 0; - forAll(lst, elemI) - { - forAll(wreLst, reI) - { - if (wreLst[reI].match(lst[elemI])) - { - matched[matchI++] = elemI; - break; - } - } - } - matched.setSize(matchI); - - return matched; -} - - -template -bool Foam::findStrings -( - const UList& wreLst, - const StringType& str -) -{ - forAll(wreLst, reI) - { - if (wreLst[reI].match(str)) - { - return true; - } - } - - return false; + lst.setSize(nElem); } diff --git a/src/OpenFOAM/primitives/Lists/wordReListMatcher.H b/src/OpenFOAM/primitives/Lists/wordReListMatcher.H new file mode 100644 index 0000000000..fa125048e5 --- /dev/null +++ b/src/OpenFOAM/primitives/Lists/wordReListMatcher.H @@ -0,0 +1,101 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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::wordReListMatcher + +Description + A wrapper for matching a List of wordRe. + +Note + The constructor should remain non-explicit. This allows automatic + conversion from UList\ to wordReListMatcher in search + functions. + +SourceFiles + wordReListMatcherI.H + +\*---------------------------------------------------------------------------*/ + +#ifndef wordReListMatcher_H +#define wordReListMatcher_H + +#include "wordReList.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class wordReListMatcher Declaration +\*---------------------------------------------------------------------------*/ + +class wordReListMatcher +{ + // Private data + + //- Reference to underlying list + const UList& reList_; + +public: + + // Constructors + + //- Construct from a List of wordRe + inline wordReListMatcher(const UList&); + + + // Member Functions + + // Access + + inline label size() const; + inline bool empty() const; + + //- Return underlying list of wordRe + inline const UList& operator()() const; + + + // Searching + + //- Return true if string matches any of the regular expressions + // Smart match as regular expression or as a string. + // Optionally specify a literal match only. + inline bool match(const string&, bool literalMatch=false) const; + +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "wordReListMatcherI.H" + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/Lists/wordReListMatcherI.H b/src/OpenFOAM/primitives/Lists/wordReListMatcherI.H new file mode 100644 index 0000000000..4b3774d2d4 --- /dev/null +++ b/src/OpenFOAM/primitives/Lists/wordReListMatcherI.H @@ -0,0 +1,79 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 + +\*---------------------------------------------------------------------------*/ + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +inline Foam::wordReListMatcher::wordReListMatcher +( + const UList& lst +) +: + reList_(lst) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline Foam::label Foam::wordReListMatcher::size() const +{ + return reList_.size(); +} + + +inline bool Foam::wordReListMatcher::empty() const +{ + return reList_.empty(); +} + + +inline const Foam::UList& +Foam::wordReListMatcher::operator()() const +{ + return reList_; +} + + +inline bool Foam::wordReListMatcher::match +( + const string& str, + bool literalMatch +) const +{ + const label nElem = reList_.size(); + for (label elemI = 0; elemI < nElem; ++elemI) + { + if (reList_[elemI].match(str, literalMatch)) + { + return true; + } + } + + return false; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H index d499998145..a0f6c550a1 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H @@ -170,7 +170,7 @@ public: //- Smart match as regular expression or as a string // Optionally specify a literal match only - inline bool match(const string&, bool literalMatch=false) const; + inline bool match(const std::string&, bool literalMatch=false) const; //- Miscellaneous diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H index 7f242eb2c5..084fc9c4c8 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H @@ -176,12 +176,12 @@ inline void Foam::wordRe::clear() } -inline bool Foam::wordRe::match(const string& str, bool literalMatch) const +inline bool Foam::wordRe::match(const std::string& str, bool literalMatch) const { if (literalMatch || !re_.exists()) { // check as string - return (*this == str); + return (str == *this); } else { diff --git a/src/conversion/meshTables/boundaryRegion.C b/src/conversion/meshTables/boundaryRegion.C index b5c278887f..d933007c6e 100644 --- a/src/conversion/meshTables/boundaryRegion.C +++ b/src/conversion/meshTables/boundaryRegion.C @@ -97,7 +97,7 @@ Foam::Map Foam::boundaryRegion::names() const Foam::Map Foam::boundaryRegion::names ( - const List& patterns + const UList& patterns ) const { Map lookup; diff --git a/src/conversion/meshTables/boundaryRegion.H b/src/conversion/meshTables/boundaryRegion.H index 91826b4d58..32a0d7cba4 100644 --- a/src/conversion/meshTables/boundaryRegion.H +++ b/src/conversion/meshTables/boundaryRegion.H @@ -112,7 +112,7 @@ public: Map names() const; //- Return a Map of (id => names) selected by patterns - Map names(const List& patterns) const; + Map names(const UList& patterns) const; //- Return a Map of (id => type) Map boundaryTypes() const; diff --git a/src/conversion/meshTables/cellTable.C b/src/conversion/meshTables/cellTable.C index 2d640deb7e..6369f98d9d 100644 --- a/src/conversion/meshTables/cellTable.C +++ b/src/conversion/meshTables/cellTable.C @@ -169,7 +169,7 @@ Foam::Map Foam::cellTable::names() const Foam::Map Foam::cellTable::names ( - const List& patterns + const UList& patterns ) const { Map lookup; diff --git a/src/conversion/meshTables/cellTable.H b/src/conversion/meshTables/cellTable.H index 102a134b8c..4d0079f032 100644 --- a/src/conversion/meshTables/cellTable.H +++ b/src/conversion/meshTables/cellTable.H @@ -139,7 +139,7 @@ public: Map names() const; //- Return a Map of (id => names) selected by patterns - Map names(const List& patterns) const; + Map names(const UList& patterns) const; //- Return a Map of (id => name) for materialType (fluid | solid | shell) Map selectType(const word& materialType) const;