Commit c45ea2c4 authored by Mark Olesen's avatar Mark Olesen
Browse files

reworked stringListOps to improve the flexibility

- added subsetStrings and inplaceSubsetString

- added class wordReListMatcher to wrap a match() for a UList<wordRe>
parent 1194af6a
......@@ -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<< " -> " << strLst[matches[i]] << nl;
}
Info<< endl;
matches = findStrings(reLst, strLst);
Info<< "matches found for " << reLst << nl << matches << nl;
forAll(matches, i)
{
Info<< " " << sl[matches[i]];
Info<< " -> " << strLst[matches[i]] << nl;
}
Info<< endl;
Info << "End\n" << 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;
}
......
......@@ -66,7 +66,9 @@ bool Foam::functionEntries::removeEntry::execute
)
{
wordList dictKeys = parentDict.toc();
labelList indices = findStrings<word>(readList<wordRe>(is), dictKeys);
wordReList patterns(is);
labelList indices = findStrings(patterns, dictKeys);
forAll(indices, indexI)
{
......
......@@ -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
{
// single-string matches:
//- Return true if string matches one of the regular expressions
inline bool findStrings
(
const wordReListMatcher& matcher,
const std::string& str
)
{
return matcher.match(str);
}
// multi-string matches:
//- Return list indices for matching strings
template<class Matcher, class StringType>
labelList findMatchingStrings
(
const Matcher&,
const UList<StringType>&,
const bool invert=false
);
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const char* regexpPattern,
const UList<StringType>&
);
const regExp& re,
const UList<StringType>& 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<class StringType>
labelList findStrings
(
const std::string& regexpPattern,
const UList<StringType>&
);
const char* rePattern,
const UList<StringType>& 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<class StringType>
labelList findStrings
(
const wordRe&,
const UList<StringType>&
);
const std::string& rePattern,
const UList<StringType>& 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<class StringType>
labelList findStrings
(
const wordRe& wre,
const UList<StringType>& 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<class StringType>
labelList findStrings
(
const UList<wordRe>&,
const UList<StringType>&
const wordReListMatcher& matcher,
const UList<StringType>& 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<regExp, stringList>(myRegExp, lst);
template<class Matcher, class StringListType>
StringListType subsetMatchingStrings
(
const Matcher&,
const StringListType&,
const bool invert=false
);
//- Return true if string matches one of the regular expressions
template<class StringType>
bool findStrings
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const UList<wordRe>&,
const StringType& str
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<class StringListType>
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<class StringListType>
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<class StringListType>
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<class StringListType>
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<regExp, stringList>(myRegExp, lst);
template<class Matcher, class StringListType>
void inplaceSubsetMatchingStrings
(
const Matcher&,
StringListType&,
const bool invert=false
);
//- Inplace extract elements of StringList when regular expression matches
// Template partial specialization of inplaceSubsetMatchingStrings
template<class StringListType>
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<class StringListType>
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<class StringListType>
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<class StringListType>
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<class StringListType>
void inplaceSubsetStrings
(
const wordReListMatcher& matcher,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(matcher, lst, invert);
}
}
......
......@@ -24,126 +24,73 @@ License
\*---------------------------------------------------------------------------*/
#include "labelList.H"
#include "regExp.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class StringType>
Foam::labelList Foam::findStrings
(
const char* pattern,
const UList<StringType>& lst
)
{
regExp re(pattern);
labelList matched(lst.size());
label matchI = 0;
forAll(lst, elemI)
{
if (re.match(lst[elemI]))
{
matched[matchI++] = elemI;
}
}
matched.setSize(matchI);
return matched;
}
template<class StringType>
Foam::labelList Foam::findStrings
template<class Matcher, class StringType>
Foam::labelList Foam::findMatchingStrings
(
const std::string& pattern,
const UList<StringType>& lst
const Matcher& matcher,
const UList<StringType>& 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<class StringType>
Foam::labelList Foam::findStrings
template<class Matcher, class StringListType>
StringListType Foam::subsetMatchingStrings
(
const wordRe& wre,
const UList<StringType>& lst
const Matcher& matcher,
const StringListType& lst,
const bool invert
)
{
labelList matched(lst.size());
StringListType newLst(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;
newLst[nElem++] = lst[elemI];
}
}
matched.setSize(matchI);
newLst.setSize(nElem);
return matched;
return newLst;
}
template<class StringType>
Foam::labelList Foam::findStrings
template<class Matcher, class StringListType>
void Foam::inplaceSubsetMatchingStrings
(
const UList<wordRe>& wreLst,
const UList<StringType>& lst
const Matcher& matcher,
StringListType& lst,
const bool invert
)
{
labelList matched(lst.size());
label matchI = 0;
label nElem = 0;
forAll(lst, elemI)
{
forAll(wreLst, reI)
{
if (wreLst[reI].match(lst[elemI]))
{
matched[matchI++] = elemI;
break;
}
}
}
matched.setSize(matchI);
return matched;
}
template<class StringType>
bool Foam::findStrings
(
const UList<wordRe>& wreLst,
const StringType& str
)
{
forAll(wreLst, reI)
{
if (wreLst[reI].match(str))
if (matcher.match(lst[elemI]) ? !invert : invert)
{
return true;
lst[nElem++] = lst[elemI];
}
}
return false;
lst.setSize(nElem);
}
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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\<wordRe\> 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<wordRe>& reList_;
public:
// Constructors
//- Construct from a List of wordRe
inline wordReListMatcher(const UList<wordRe>&);
// Member Functions
// Access
inline label size() const;
inline bool empty() const;
//- Return underlying list of wordRe
inline const UList<wordRe>& operator()() const;
// Searching
//- Return true if string matches any of the regular expressions
// Smart match as regular expression or as a string.