ENH: cleanup wordRe interfaces etc.

- ensure that the string-related classes have consistently similar
  matching methods. Use operator()(const std::string) as an entry
  point for the match() method, which makes it easier to use for
  filters and predicates. In some cases this will also permit using
  a HashSet as a match predicate.

regExp
====
- the set method now returns a bool to signal that the requested
  pattern was compiled.

wordRe
====
- have separate constructors with the compilation option (was previously
  a default parameter). This leaves the single parameter constructor
  explicit, but the two parameter version is now non-explicit, which
  makes it easier to use when building lists.

- renamed compile-option from REGEX (to REGEXP) for consistency with
  with the <regex.h>, <regex> header names etc.

wordRes
====
- renamed from wordReListMatcher -> wordRes. For reduced typing and
  since it behaves as an entity only slightly related to its underlying
  list nature.

- Provide old name as typedef and include for code transition.

- pass through some list methods into wordRes

hashedWordList
====
- hashedWordList[const word& name] now returns a -1 if the name is is
  not found in the list of indices. That has been a pending change
  ever since hashedWordList was generalized out of speciesTable
  (Oct-2010).

- add operator()(const word& name) for easy use as a predicate

STYLE: adjust parameter names in stringListOps

- reflect if the parameter is being used as a primary matcher, or the
  matcher will be derived from the parameter.
  For example,
      (const char* re), which first creates a regExp
      versus (const regExp& matcher) which is used directly.
This commit is contained in:
Mark Olesen 2017-05-16 23:54:43 +02:00
parent c1c6243c3e
commit a8d2ebf298
31 changed files with 556 additions and 336 deletions

View File

@ -180,9 +180,9 @@ int main(int argc, char *argv[])
// A regex with a zero length matcher doesn't work at all:
// eg "(png|jpg|txt|)" regex matcher itself
wordRe matcher0("()", wordRe::REGEXP);
wordRe matcher1("(png|jpg|txt)", wordRe::REGEXP);
wordRe matcher2("(png|txt)", wordRe::REGEXP);
wordRe matcher0("()", wordRe::REGEX);
wordRe matcher1("(png|jpg|txt)", wordRe::REGEX);
wordRe matcher2("(png|txt)", wordRe::REGEX);
Info<<"Has extension(s):" << nl
<< "input: " << endWithDot << nl;

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -30,7 +30,9 @@ Description
#include "IFstream.H"
#include "List.H"
#include "Tuple2.H"
#include "keyType.H"
#include "wordRe.H"
#include "wordRes.H"
using namespace Foam;
@ -44,12 +46,36 @@ int main(int argc, char *argv[])
Foam::string s2("this .* file");
const char * s3 = "this .* file";
keyType keyre("x.*", true);
wordReList wordrelist
{
{"this", wordRe::LITERAL},
{"x.*", wordRe::REGEX},
{"file[a-b]", wordRe::REGEX},
};
wordRes wrelist(wordrelist);
Info<< "re-list:" << wrelist() << endl;
Info<< "match this: " << wrelist("this") << endl;
Info<< "match xyz: " << wrelist("xyz") << endl;
Info<< "match zyx: " << wrelist("zyx") << endl;
Info<< "match xyz: " << wrelist.match("xyz") << endl;
Info<< "keyre match: " << keyre("xyz") << endl;
Info<< "string match: " << string("this").match("xyz") << endl;
Info<< "string match: " << string("x.*")("xyz") << endl;
Info<< "string match: " << string("x.*")(keyre) << endl;
wordRe(s1, wordRe::DETECT).info(Info) << endl;
wordRe(s2).info(Info) << endl;
wordRe(s2, wordRe::DETECT).info(Info) << endl;
wordRe(s3, wordRe::REGEXP).info(Info) << endl;
wordRe(s3, wordRe::REGEX).info(Info) << endl;
wre = "this .* file";
Info<<"substring: " << wre(4) << endl;
wre.info(Info) << endl;
wre = s1;
wre.info(Info) << endl;

View File

@ -24,7 +24,7 @@ License
\*---------------------------------------------------------------------------*/
#include "doxygenXmlParser.H"
#include "wordRe.H"
#include "regExp.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
@ -40,8 +40,8 @@ Foam::doxygenXmlParser::doxygenXmlParser
dictionary(dictionary::null)
{
// Pre-construct and compile regular expressions
const wordRe nameRe(".*.H", wordRe::DETECT);
const wordRe searchStrRe(searchStr, wordRe::DETECT);
const regExp nameRe(".*.H");
const regExp searchStrRe(searchStr);
// Pre-construct constant strings and names to speed-up comparisons
const string slashStartTag('/' + startTag);
@ -163,7 +163,7 @@ Foam::doxygenXmlParser::doxygenXmlParser
(
!exactMatch
&& !found(tName) // not already added
&& wordRe(".*" + tName + ".*", wordRe::DETECT).match(name)
&& regExp(".*" + tName + ".*").match(name)
)
{
dictionary dict(dictionary::null);

View File

@ -199,7 +199,7 @@ void print(Ostream& os, const wordList& flds)
labelList getSelectedPatches
(
const polyBoundaryMesh& patches,
const List<wordRe>& excludePatches
const wordRes& excludePatches
)
{
DynamicList<label> patchIDs(patches.size());
@ -219,7 +219,7 @@ labelList getSelectedPatches
Info<< " discarding empty/processor patch " << patchi
<< " " << pp.name() << endl;
}
else if (findStrings(excludePatches, pp.name()))
else if (excludePatches.match(pp.name()))
{
Info<< " excluding patch " << patchi
<< " " << pp.name() << endl;
@ -379,7 +379,7 @@ int main(int argc, char *argv[])
const bool allPatches = args.optionFound("allPatches");
List<wordRe> excludePatches;
wordReList excludePatches;
if (args.optionFound("excludePatches"))
{
args.optionLookup("excludePatches")() >> excludePatches;

View File

@ -169,7 +169,7 @@ void Foam::boundaryInfo::setType(const label patchI, const word& condition)
return;
}
if (wordRe(".*[Mm]apped.*", wordRe::REGEXP).match(types_[patchI]))
if (regExp(".*[Mm]apped.*").match(types_[patchI]))
{
// ugly hack to avoid overriding mapped types
return;

View File

@ -111,7 +111,7 @@ Foam::regExp::~regExp()
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
void Foam::regExp::set(const char* pattern, bool ignoreCase)
bool Foam::regExp::set(const char* pattern, bool ignoreCase)
{
clear();
@ -137,7 +137,7 @@ void Foam::regExp::set(const char* pattern, bool ignoreCase)
// avoid zero-length patterns
if (!*pat)
{
return;
return false;
}
}
@ -154,11 +154,15 @@ void Foam::regExp::set(const char* pattern, bool ignoreCase)
<< nl << errbuf
<< exit(FatalError);
}
return true;
}
return false; // Was cleared and nothing was set
}
void Foam::regExp::set(const std::string& pattern, bool ignoreCase)
bool Foam::regExp::set(const std::string& pattern, bool ignoreCase)
{
return set(pattern.c_str(), ignoreCase);
}
@ -208,7 +212,7 @@ bool Foam::regExp::match(const std::string& text) const
if
(
regexec(preg_, text.c_str(), nmatch, pmatch, 0) == 0
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(text.size()))
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == regoff_t(text.size()))
)
{
return true;

View File

@ -128,20 +128,23 @@ public:
// Editing
//- Compile pattern into a regular expression, optionally ignore case
void set(const char* pattern, bool ignoreCase=false);
//- Compile pattern into a regular expression, optionally ignore case.
// \return True if the pattern was compiled
bool set(const char* pattern, bool ignoreCase=false);
//- Compile pattern into a regular expression, optionally ignore case
void set(const std::string& pattern, bool ignoreCase=false);
// \return True if the pattern was compiled
bool set(const std::string& pattern, bool ignoreCase=false);
//- Clear expression, return true if expression had existed.
//- Clear expression.
// \return True if expression had existed prior to the clear.
bool clear();
// Matching/Searching
//- Find position within string.
// Returns the index where it begins or string::npos if not found
// \Return The index where it begins or string::npos if not found
std::string::size_type find(const std::string& text) const;
//- Return true if it matches the entire string
@ -158,6 +161,9 @@ public:
// Member Operators
//- Perform match on text
inline bool operator()(const std::string& text) const;
//- Assign and compile pattern from a character array
// Always case sensitive
inline void operator=(const char* pattern);

View File

@ -66,6 +66,12 @@ inline bool Foam::regExp::search(const std::string& text) const
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
inline bool Foam::regExp::operator()(const std::string& text) const
{
return match(text);
}
inline void Foam::regExp::operator=(const char* pattern)
{
set(pattern);

View File

@ -97,8 +97,8 @@ $(strings)/fileName/fileName.C
$(strings)/fileName/fileNameIO.C
$(strings)/keyType/keyType.C
$(strings)/wordRe/wordRe.C
$(strings)/wordRes/wordRes.C
$(strings)/lists/hashedWordList.C
$(strings)/lists/wordReListMatcher.C
$(strings)/stringOps/stringOps.C
ops = primitives/ops

View File

@ -169,7 +169,7 @@ Foam::IOobjectList Foam::IOobjectList::lookup(const wordRe& matcher) const
Foam::IOobjectList Foam::IOobjectList::lookup(const wordReList& matcher) const
{
wordReListMatcher mat(matcher);
wordRes mat(matcher);
IOobjectList results(size());

View File

@ -52,13 +52,11 @@ bool Foam::keyType::match(const std::string& text, bool literal) const
{
if (literal || !isPattern_)
{
// check as string
return (text == *this);
return !compare(text); // Compare as literal string
}
else
{
// check as regex
return regExp(*this).match(text);
return regExp(*this).match(text); // Match as regex
}
}

View File

@ -121,19 +121,26 @@ public:
// Member operators
// Assignment
//- Avoid masking the normal operator()
using word::operator();
//- Assignment operator, retaining type (literal or regex)
inline void operator=(const keyType& s);
//- Perform smart match on text
inline bool operator()(const std::string& text) const;
//- Assign as word, not treated as a regular expression.
inline void operator=(const word& s);
//- Assign as regular expression
inline void operator=(const string& s);
// Assignment
//- Assign as word, not treated as a regular expression.
inline void operator=(const char* s);
//- Assignment operator, retaining type (literal or regex)
inline void operator=(const keyType& s);
//- Assign as word, not treated as a regular expression.
inline void operator=(const word& s);
//- Assign as regular expression
inline void operator=(const string& s);
//- Assign as word, not treated as a regular expression.
inline void operator=(const char* s);
// IOstream operators

View File

@ -81,6 +81,12 @@ inline bool Foam::keyType::isPattern() const
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline bool Foam::keyType::operator()(const std::string& text) const
{
return match(text); // Use smart match
}
inline void Foam::keyType::operator=(const keyType& s)
{
string::operator=(s); // Bypass checking

View File

@ -47,8 +47,8 @@ namespace Foam
class hashedWordList;
// Forward declaration of friend functions and operators
Istream& operator>>(Istream&, hashedWordList&);
Ostream& operator<<(Ostream&, const hashedWordList&);
Istream& operator>>(Istream& is, hashedWordList& lst);
Ostream& operator<<(Ostream& os, const hashedWordList& lst);
/*---------------------------------------------------------------------------*\
@ -97,7 +97,7 @@ public:
);
//- Construct from an initializer list
inline hashedWordList(std::initializer_list<word>);
inline hashedWordList(std::initializer_list<word> lst);
//- Construct from the word keys of any HashTable, sorting immediately.
// This also handles a wordHashSet, which is derived from a HashTable.
@ -105,7 +105,7 @@ public:
template<class AnyType, class AnyHash>
explicit inline hashedWordList
(
const HashTable<AnyType, word, AnyHash>& h
const HashTable<AnyType, word, AnyHash>& tbl
);
//- Construct from number and list of words,
@ -122,7 +122,7 @@ public:
hashedWordList(const char** lst, const bool removeDuplicates=false);
//- Construct from Istream
hashedWordList(Istream&);
hashedWordList(Istream& is);
// Member Functions
@ -161,26 +161,33 @@ public:
// Member Operators
//- Assignment operator from list of words
inline void operator=(const UList<word>& lst);
//- Assignment operator from initializer list
inline void operator=(std::initializer_list<word> lst);
//- Assignment operator.
inline void operator=(const hashedWordList& lst);
//- Return name corresponding to specified index
inline const word& operator[](const label index) const;
//- Return index corresponding to specified name
//- Return index corresponding to specified name, or -1 on failure
inline label operator[](const word& name) const;
//- Does the list contain the specified name - same as found.
// Makes hashedWordList suitable as a unary predicate.
inline bool operator()(const word& name) const;
// Assignment
//- Assignment operator from list of words. Rehashes the indices.
inline void operator=(const UList<word>& lst);
//- Assignment operator from initializer list. Rehashes the indices.
inline void operator=(std::initializer_list<word> lst);
//- Assignment operator. Rehashes the indices.
inline void operator=(const hashedWordList& lst);
// Istream operators
friend Istream& operator>>(Istream&, hashedWordList&);
friend Ostream& operator<<(Ostream&, const hashedWordList&);
friend Istream& operator>>(Istream& is, hashedWordList& lst);
friend Ostream& operator<<(Ostream& os, const hashedWordList& lst);
};

View File

@ -90,21 +90,15 @@ inline Foam::hashedWordList::hashedWordList(std::initializer_list<word> lst)
template<class AnyType, class AnyHash>
inline Foam::hashedWordList::hashedWordList
(
const HashTable<AnyType, word, AnyHash>& h
const HashTable<AnyType, word, AnyHash>& tbl
)
:
List<word>(h.size())
List<word>(tbl.size())
{
label nElem = 0;
for
(
typename HashTable<AnyType, word, AnyHash>::const_iterator
iter = h.cbegin();
iter != h.cend();
++iter
)
label count = 0;
for (auto iter = tbl.cbegin(); iter != tbl.cend(); ++iter)
{
List<word>::operator[](nElem++) = iter.key();
List<word>::operator[](count++) = iter.key();
}
this->sort();
@ -162,6 +156,34 @@ inline void Foam::hashedWordList::sort()
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline const Foam::word& Foam::hashedWordList::operator[]
(
const label index
) const
{
return List<word>::operator[](index);
}
inline Foam::label Foam::hashedWordList::operator[](const word& name) const
{
auto iter = indices_.find(name);
if (iter.found())
{
return iter.object();
}
return -1; // Not found (or not hashed?)
}
inline bool Foam::hashedWordList::operator()(const word& name) const
{
return indices_.found(name);
}
inline void Foam::hashedWordList::operator=(const UList<word>& lst)
{
List<word>::operator=(lst);
@ -182,20 +204,4 @@ inline void Foam::hashedWordList::operator=(const hashedWordList& lst)
}
inline const Foam::word& Foam::hashedWordList::operator[]
(
const label index
) const
{
return List<word>::operator[](index);
}
inline Foam::label Foam::hashedWordList::operator[](const word& name) const
{
// Could return -1 instead of bombing out
return indices_[name];
}
// ************************************************************************* //

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -35,91 +35,99 @@ SourceFiles
#ifndef stringListOps_H
#define stringListOps_H
#include "regExp.H"
#include "labelList.H"
#include "stringList.H"
#include "wordReList.H"
#include "wordReListMatcher.H"
#include "regExp.H"
#include "wordRes.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Single-string matches:
// Single-string matches:
//- Return true if string matches one of the regular expressions
inline bool findStrings
(
const wordReListMatcher& matcher,
const std::string& str
)
//- Return true if text matches one of the regular expressions
// The primary purpose of this function is to automatically convert
// a wordReList to a wordRes for matching.
inline bool findStrings(const wordRes& matcher, const std::string& text)
{
return matcher.match(str);
return matcher(text);
}
// Multi-string matches:
//- Return list indices for matching strings
template<class Matcher, class StringType>
// Multi-string matches:
//- Extract list indices
// The unary match predicate has the following signature:
// \code
// bool operator()(const std::string& text);
// \endcode
//
// \return List indices for matching strings
template<class UnaryMatchPredicate, class StringType>
labelList findMatchingStrings
(
const Matcher& matcher,
const UnaryMatchPredicate& matcher,
const UList<StringType>& lst,
const bool invert=false
);
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
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 char* rePattern,
const regExp& matcher,
const UList<StringType>& lst,
const bool invert=false
)
{
const regExp re(rePattern);
return findMatchingStrings(re, lst, invert);
return findMatchingStrings(matcher, lst, invert);
}
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const std::string& rePattern,
const char* re,
const UList<StringType>& lst,
const bool invert=false
)
{
const regExp re(rePattern);
return findMatchingStrings(re, lst, invert);
const regExp matcher(re);
return findMatchingStrings(matcher, 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 std::string& re,
const UList<StringType>& lst,
const bool invert=false
)
{
return findMatchingStrings(wre, lst, invert);
const regExp matcher(re);
return findMatchingStrings(matcher, lst, invert);
}
//- Return list indices for strings matching the regular expression
// Template partial specialization of findMatchingStrings
template<class StringType>
labelList findStrings
(
const wordRe& matcher,
const UList<StringType>& lst,
const bool invert=false
)
{
return findMatchingStrings(matcher, lst, invert);
}
@ -128,7 +136,7 @@ namespace Foam
template<class StringType>
labelList findStrings
(
const wordReListMatcher& matcher,
const wordRes& matcher,
const UList<StringType>& lst,
const bool invert=false
)
@ -136,31 +144,50 @@ namespace Foam
return findMatchingStrings(matcher, lst, invert);
}
// subsetting multi-string matches (similar to ListOp):
// 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:
// \code
// subsetMatchingStrings<regExp, stringList>(myRegExp, lst);
template<class Matcher, class StringListType>
// \endcode
template<class UnaryMatchPredicate, class StringListType>
StringListType subsetMatchingStrings
(
const Matcher&,
const StringListType&,
const UnaryMatchPredicate& matcher,
const StringListType& lst,
const bool invert=false
);
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const regExp& re,
const regExp& matcher,
const StringListType& lst,
const bool invert=false
)
{
return subsetMatchingStrings(re, lst, invert);
return subsetMatchingStrings(matcher, lst, invert);
}
//- Extract elements of StringList when regular expression matches
// Template partial specialization of subsetMatchingStrings
template<class StringListType>
StringListType subsetStrings
(
const char* re,
const StringListType& lst,
const bool invert=false
)
{
const regExp matcher(re);
return subsetMatchingStrings(matcher, lst, invert);
}
//- Extract elements of StringList when regular expression matches
@ -168,13 +195,13 @@ namespace Foam
template<class StringListType>
StringListType subsetStrings
(
const char* rePattern,
const std::string& re,
const StringListType& lst,
const bool invert=false
)
{
const regExp re(rePattern);
return subsetMatchingStrings(re, lst, invert);
const regExp matcher(re);
return subsetMatchingStrings(matcher, lst, invert);
}
//- Extract elements of StringList when regular expression matches
@ -182,13 +209,12 @@ namespace Foam
template<class StringListType>
StringListType subsetStrings
(
const std::string& rePattern,
const wordRe& matcher,
const StringListType& lst,
const bool invert=false
)
{
const regExp re(rePattern);
return subsetMatchingStrings(re, lst, invert);
return subsetMatchingStrings(matcher, lst, invert);
}
//- Extract elements of StringList when regular expression matches
@ -196,20 +222,7 @@ namespace Foam
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 wordRes& matcher,
const StringListType& lst,
const bool invert=false
)
@ -222,11 +235,11 @@ namespace Foam
// optionally invert the match
// eg, to extract all selected elements:
// inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
template<class Matcher, class StringListType>
template<class UnaryMatchPredicate, class StringListType>
void inplaceSubsetMatchingStrings
(
const Matcher&,
StringListType&,
const UnaryMatchPredicate& matcher,
StringListType& lst,
const bool invert=false
);
@ -235,12 +248,12 @@ namespace Foam
template<class StringListType>
void inplaceSubsetStrings
(
const regExp& re,
const regExp& matcher,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(re, lst, invert);
inplaceSubsetMatchingStrings(matcher, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
@ -248,13 +261,13 @@ namespace Foam
template<class StringListType>
void inplaceSubsetStrings
(
const char* rePattern,
const char* re,
StringListType& lst,
const bool invert=false
)
{
const regExp re(rePattern);
inplaceSubsetMatchingStrings(re, lst, invert);
const regExp matcher(re);
inplaceSubsetMatchingStrings(matcher, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
@ -262,13 +275,13 @@ namespace Foam
template<class StringListType>
void inplaceSubsetStrings
(
const std::string& rePattern,
const std::string& re,
StringListType& lst,
const bool invert=false
)
{
const regExp re(rePattern);
inplaceSubsetMatchingStrings(re, lst, invert);
const regExp matcher(re);
inplaceSubsetMatchingStrings(matcher, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
@ -276,12 +289,12 @@ namespace Foam
template<class StringListType>
void inplaceSubsetStrings
(
const wordRe& wre,
const wordRe& matcher,
StringListType& lst,
const bool invert=false
)
{
inplaceSubsetMatchingStrings(wre, lst, invert);
inplaceSubsetMatchingStrings(matcher, lst, invert);
}
//- Inplace extract elements of StringList when regular expression matches
@ -289,7 +302,7 @@ namespace Foam
template<class StringListType>
void inplaceSubsetStrings
(
const wordReListMatcher& matcher,
const wordRes& matcher,
StringListType& lst,
const bool invert=false
)

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -25,75 +25,79 @@ License
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Matcher, class StringType>
template<class UnaryMatchPredicate, class StringType>
Foam::labelList Foam::findMatchingStrings
(
const Matcher& matcher,
const UnaryMatchPredicate& matcher,
const UList<StringType>& lst,
const bool invert
)
{
labelList indices(lst.size());
label nElem = 0;
forAll(lst, elemI)
label count = 0;
forAll(lst, elemi)
{
if (matcher.match(lst[elemI]) ? !invert : invert)
if (matcher(lst[elemi]) ? !invert : invert)
{
indices[nElem++] = elemI;
indices[count++] = elemi;
}
}
indices.setSize(nElem);
indices.setSize(count);
return indices;
}
template<class Matcher, class StringListType>
template<class UnaryMatchPredicate, class StringListType>
StringListType Foam::subsetMatchingStrings
(
const Matcher& matcher,
const UnaryMatchPredicate& matcher,
const StringListType& lst,
const bool invert
)
{
// Create copy
// Create as a copy
StringListType newLst(lst.size());
// ensure consistent addressable size (eg, DynamicList)
// Ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
label nElem = 0;
forAll(lst, elemI)
label count = 0;
forAll(lst, elemi)
{
if (matcher.match(lst[elemI]) ? !invert : invert)
if (matcher(lst[elemi]) ? !invert : invert)
{
newLst[nElem++] = lst[elemI];
newLst[count++] = lst[elemi];
}
}
newLst.setSize(nElem);
newLst.setSize(count);
return newLst;
}
template<class Matcher, class StringListType>
template<class UnaryMatchPredicate, class StringListType>
void Foam::inplaceSubsetMatchingStrings
(
const Matcher& matcher,
const UnaryMatchPredicate& matcher,
StringListType& lst,
const bool invert
)
{
label nElem = 0;
forAll(lst, elemI)
label count = 0;
forAll(lst, elemi)
{
if (matcher.match(lst[elemI]) ? !invert : invert)
if (matcher(lst[elemi]) ? !invert : invert)
{
lst[nElem++] = lst[elemI];
if (count != elemi)
{
lst[count] = lst[elemi];
}
++count;
}
}
lst.setSize(nElem);
lst.setSize(count);
}

View File

@ -94,7 +94,9 @@ public:
hash()
{}
inline unsigned operator()(const string&, unsigned seed = 0) const;
//- Hash for string.
// Uses Foam::string instead of std::string for automatic conversions.
inline unsigned operator()(const string& str, unsigned seed = 0) const;
};
@ -109,7 +111,7 @@ public:
//- Construct as copy of character array
inline string(const char* str);
//- Construct as copy of specified number of characters
//- Construct as copy with a maximum number of characters
inline string(const char* str, const size_type len);
//- Construct from a single character
@ -222,14 +224,18 @@ public:
// Member Operators
//- Return the sub-string from the i-th character for \a n characters
//- Match text
// \return True when strings match literally.
inline bool operator()(const std::string& text) const;
//- Return sub-string from the i-th character for \a n characters
inline string operator()
(
const size_type i,
const size_type n
) const;
//- Return the sub-string from the first character for \a n characters
//- Return sub-string from the first character for \a n characters
inline string operator()
(
const size_type n

View File

@ -178,15 +178,21 @@ inline String Foam::string::validate(const std::string& str)
return ss;
}
inline bool Foam::string::match(const std::string& text) const
{
// check as string
return (text == *this);
return !compare(text); // Always compare as literal string
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline bool Foam::string::operator()(const std::string& text) const
{
return !compare(text); // Always compare as literal string
}
inline Foam::string Foam::string::operator()
(
const size_type i,
@ -205,11 +211,11 @@ inline Foam::string Foam::string::operator()(const size_type n) const
inline unsigned Foam::string::hash::operator()
(
const string& key,
const string& str,
unsigned seed
) const
{
return Hasher(key.data(), key.size(), seed);
return Hasher(str.data(), str.size(), seed);
}
// ************************************************************************* //

View File

@ -25,7 +25,7 @@ Class
Foam::word
Description
A class for handling words, derived from string.
A class for handling words, derived from Foam::string.
A word is a string of characters without whitespace, quotes, slashes,
semicolons or brace brackets. Words are delimited by whitespace.
@ -93,7 +93,7 @@ public:
inline word
(
const char* s,
const size_type,
const size_type len,
const bool doStripInvalid
);
@ -119,12 +119,12 @@ public:
// Member operators
// Assignment
// Assignment
inline void operator=(const word& w);
inline void operator=(const string& s);
inline void operator=(const std::string& s);
inline void operator=(const char* s);
inline void operator=(const word& w);
inline void operator=(const string& s);
inline void operator=(const std::string& s);
inline void operator=(const char* s);
// Friend Operators

View File

@ -99,11 +99,11 @@ inline Foam::word::word(const char* s, const bool doStripInvalid)
inline Foam::word::word
(
const char* s,
const size_type n,
const size_type len,
const bool doStripInvalid
)
:
string(s, n)
string(s, len)
{
if (doStripInvalid)
{

View File

@ -92,15 +92,15 @@ public:
// Public data types
//- Enumeration with compile options
// Note that 'REGEXP' is implicit if 'NOCASE' is specified alone.
// Note that 'REGEX' is implicit if 'NOCASE' is specified alone.
enum compOption
{
LITERAL = 0, //!< Treat as a string literal
DETECT = 1, //!< Detect if the string contains meta-characters
REGEXP = 2, //!< Treat as regular expression
REGEX = 2, //!< Treat as regular expression
NOCASE = 4, //!< Ignore case in regular expression
DETECT_NOCASE = DETECT | NOCASE, //!< Combined DETECT and NOCASE
REGEXP_NOCASE = REGEXP | NOCASE //!< Combined REGEXP and NOCASE
DETECT_NOCASE = DETECT|NOCASE, //!< Combined DETECT and NOCASE
REGEX_NOCASE = REGEX|NOCASE //!< Combined REGEX and NOCASE
};
@ -119,30 +119,35 @@ public:
//- Construct as copy
inline wordRe(const wordRe& str);
//- Construct from keyType
//- Construct from keyType, using its compile information
inline explicit wordRe(const keyType& str);
//- Construct as copy of character array, treat as a literal
inline explicit wordRe(const char* str);
//- Construct as copy of std::string, treat as a literal
inline explicit wordRe(const std::string& str);
//- Construct as copy of string, treat as a literal
inline explicit wordRe(const string& str);
//- Construct as copy of word, treat as a literal
inline explicit wordRe(const word& str);
//- Construct from keyType, use specified compile option
inline wordRe(const keyType& str, const compOption);
//- Construct as copy of word
inline explicit wordRe(const word& str);
//- Construct as copy of character array, use specified compile option
inline wordRe(const char* str, const compOption);
//- Construct as copy of character array
// Optionally specify how it should be treated.
inline explicit wordRe(const char* str, const compOption = LITERAL);
//- Construct as copy of std::string, use specified compile option
inline wordRe(const std::string& str, const compOption);
//- Construct as copy of string.
// Optionally specify how it should be treated.
inline explicit wordRe(const string& str, const compOption = LITERAL);
//- Construct as copy of string, use specified compile option
inline wordRe(const string& str, const compOption);
//- Construct as copy of std::string
// Optionally specify how it should be treated.
inline explicit wordRe
(
const std::string& str,
const compOption = LITERAL
);
//- Construct as copy of word, use specified compile option
inline wordRe(const word& str, const compOption);
//- Construct from Istream
// Words are treated as literals, strings with an auto-test
@ -200,6 +205,13 @@ public:
// Member operators
//- Avoid masking the normal operator()
using word::operator();
//- Perform smart match on text, as per match()
inline bool operator()(const std::string& text) const;
// Assignment
//- Copy wordRe and its type (literal or regex)

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -48,7 +48,7 @@ inline Foam::wordRe::wordRe()
inline Foam::wordRe::wordRe(const wordRe& str)
:
word(str),
word(str, false),
re_()
{
if (str.isPattern())
@ -58,13 +58,6 @@ inline Foam::wordRe::wordRe(const wordRe& str)
}
inline Foam::wordRe::wordRe(const word& str)
:
word(str),
re_()
{}
inline Foam::wordRe::wordRe(const keyType& str)
:
word(str, false),
@ -77,6 +70,34 @@ inline Foam::wordRe::wordRe(const keyType& str)
}
inline Foam::wordRe::wordRe(const char* str)
:
word(str, false),
re_()
{}
inline Foam::wordRe::wordRe(const std::string& str)
:
word(str, false),
re_()
{}
inline Foam::wordRe::wordRe(const string& str)
:
word(str, false),
re_()
{}
inline Foam::wordRe::wordRe(const word& str)
:
word(str, false),
re_()
{}
inline Foam::wordRe::wordRe(const keyType& str, const compOption opt)
:
word(str, false),
@ -91,17 +112,7 @@ inline Foam::wordRe::wordRe(const keyType& str, const compOption opt)
inline Foam::wordRe::wordRe(const char* str, const compOption opt)
:
word(str, false),
re_()
{
compile(opt);
}
inline Foam::wordRe::wordRe(const string& str, const compOption opt)
:
word(str, false),
re_()
wordRe(str)
{
compile(opt);
}
@ -109,8 +120,23 @@ inline Foam::wordRe::wordRe(const string& str, const compOption opt)
inline Foam::wordRe::wordRe(const std::string& str, const compOption opt)
:
word(str, false),
re_()
wordRe(str)
{
compile(opt);
}
inline Foam::wordRe::wordRe(const string& str, const compOption opt)
:
wordRe(str)
{
compile(opt);
}
inline Foam::wordRe::wordRe(const word& str, const compOption opt)
:
wordRe(str)
{
compile(opt);
}
@ -126,42 +152,38 @@ inline bool Foam::wordRe::isPattern() const
inline bool Foam::wordRe::compile(const compOption opt) const
{
bool doCompile = false;
if (opt)
{
bool comp = false;
if (opt & wordRe::REGEXP)
{
doCompile = true;
}
else if (opt & wordRe::DETECT)
{
if (string::meta<regExp>(*this) || !string::valid<word>(*this))
if (opt & wordRe::REGEX)
{
doCompile = true;
comp = true;
}
else if (opt & wordRe::DETECT)
{
comp = string::meta<regExp>(*this) || !string::valid<word>(*this);
}
else if (opt & wordRe::NOCASE)
{
comp = true;
}
if (comp)
{
return re_.set(*this, (opt & wordRe::NOCASE));
}
}
else if (opt & wordRe::NOCASE)
{
doCompile = true;
}
if (doCompile)
{
re_.set(*this, (opt & wordRe::NOCASE));
}
else
{
re_.clear();
}
return re_.exists();
// Fall-through behaviour - not a regex
re_.clear();
return false;
}
inline bool Foam::wordRe::compile() const
{
re_ = *this;
return re_.exists();
return re_.set(*this);
}
@ -169,7 +191,7 @@ inline void Foam::wordRe::uncompile(const bool doStripInvalid) const
{
if (re_.clear() && doStripInvalid)
{
// skip stripping unless debug is active to avoid costly operations
// Skip stripping unless debug is active to avoid costly operations
if (word::debug)
{
string::stripInvalid<word>
@ -192,13 +214,11 @@ inline bool Foam::wordRe::match(const std::string& text, bool literal) const
{
if (literal || !re_.exists())
{
// check as string
return (text == *this);
return !compare(text); // Compare as literal string
}
else
{
// check as regex
return re_.match(text);
return re_.match(text); // Match as regex
}
}
@ -225,6 +245,12 @@ inline void Foam::wordRe::set(const char* str, const compOption opt)
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline bool Foam::wordRe::operator()(const std::string& text) const
{
return match(text);
}
inline void Foam::wordRe::operator=(const wordRe& str)
{
string::operator=(str);
@ -263,21 +289,21 @@ inline void Foam::wordRe::operator=(const keyType& str)
inline void Foam::wordRe::operator=(const string& str)
{
string::operator=(str);
compile(wordRe::DETECT); // auto-detect regex
compile(wordRe::DETECT); // Auto-detect regex
}
inline void Foam::wordRe::operator=(const std::string& str)
{
string::operator=(str);
compile(wordRe::DETECT); // auto-detect regex
compile(wordRe::DETECT); // Auto-detect regex
}
inline void Foam::wordRe::operator=(const char* str)
{
string::operator=(str);
compile(wordRe::DETECT); // auto-detect regex
compile(wordRe::DETECT); // Auto-detect regex
}

View File

@ -0,0 +1,51 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 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 3 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, see <http://www.gnu.org/licenses/>.
Typedef
Foam::wordReListMatcher
Description
The older name for Foam::wordRes, which is a wrapper for matching
a std::string against wordRe list.
\*---------------------------------------------------------------------------*/
#ifndef wordReListMatcher_H
#define wordReListMatcher_H
#include "wordRes.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef wordRes wordReListMatcher;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
#endif
// ************************************************************************* //

View File

@ -23,17 +23,17 @@ License
\*---------------------------------------------------------------------------*/
#include "wordReListMatcher.H"
#include "wordRes.H"
#include "HashSet.H"
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::wordReList Foam::wordReListMatcher::uniq(const UList<wordRe>& input)
Foam::wordReList Foam::wordRes::uniq(const UList<wordRe>& input)
{
wordReList retain(input.size());
wordHashSet uniqWord;
label nUniq = 0;
label count = 0;
forAll(input, i)
{
const wordRe& select = input[i];
@ -44,11 +44,11 @@ Foam::wordReList Foam::wordReListMatcher::uniq(const UList<wordRe>& input)
|| uniqWord.insert(static_cast<const word&>(select))
)
{
retain[nUniq++] = select;
retain[count++] = select;
}
}
retain.setSize(nUniq);
retain.setSize(count);
return retain;
}

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -22,23 +22,23 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::wordReListMatcher
Foam::wordRes
Description
A wrapper for matching a List of wordRe.
A wrapper for matching a std::string against a wordRe list.
Note
The constructor should remain non-explicit. This allows automatic
conversion from UList\<wordRe\> to wordReListMatcher in search
functions.
conversion from UList\<wordRe\> to wordRes in search functions.
SourceFiles
wordReListMatcherI.H
wordResI.H
wordRes.C
\*---------------------------------------------------------------------------*/
#ifndef wordReListMatcher_H
#define wordReListMatcher_H
#ifndef wordRes_H
#define wordRes_H
#include "wordReList.H"
@ -48,48 +48,70 @@ namespace Foam
{
/*---------------------------------------------------------------------------*\
Class wordReListMatcher Declaration
Class wordRes Declaration
\*---------------------------------------------------------------------------*/
class wordReListMatcher
class wordRes
{
// Private data
//- Reference to underlying list
const UList<wordRe>& reList_;
const UList<wordRe>& list_;
public:
// STL type definitions
//- Type of values the list contains
typedef wordRe value_type;
// Constructors
//- Construct from a List of wordRe
inline wordReListMatcher(const UList<wordRe>&);
//- Construct from a list of wordRe
inline wordRes(const UList<wordRe>& lst);
// Static Constructors, Helpers
//- Return a wordReList with duplicate words filtered out.
// No filtering is done on regular expressions.
static wordReList uniq(const UList<wordRe>& input);
// Member Functions
// Access
//- The number of elements in the list
inline label size() const;
inline bool empty() const;
//- True if the list is empty
inline bool empty() 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 std::string& text,
const bool literal = false
) const;
// Member operators
//- Return underlying list of wordRe
inline const UList<wordRe>& operator()() const;
//- Perform smart match on text, as per match()
inline bool operator()(const std::string& text) 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 std::string&, bool literalMatch=false) const;
// Helpers
//- Return a wordReList with duplicate words filtered out.
// No filtering is done on regular expressions.
static wordReList uniq(const UList<wordRe>& input);
//- Return element of constant list
inline const wordRe& operator[](const label i) const;
};
@ -100,7 +122,7 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "wordReListMatcherI.H"
#include "wordResI.H"
#endif

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -26,46 +26,40 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::wordReListMatcher::wordReListMatcher
inline Foam::wordRes::wordRes
(
const UList<wordRe>& lst
const UList<wordRe>& list
)
:
reList_(lst)
list_(list)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline Foam::label Foam::wordReListMatcher::size() const
inline Foam::label Foam::wordRes::size() const
{
return reList_.size();
return list_.size();
}
inline bool Foam::wordReListMatcher::empty() const
inline bool Foam::wordRes::empty() const
{
return reList_.empty();
return list_.empty();
}
inline const Foam::UList<Foam::wordRe>&
Foam::wordReListMatcher::operator()() const
{
return reList_;
}
inline bool Foam::wordReListMatcher::match
inline bool Foam::wordRes::match
(
const std::string& text,
bool literalMatch
const bool literal
) const
{
const label n = reList_.size();
const label n = list_.size();
for (label i = 0; i < n; ++i)
{
if (reList_[i].match(text, literalMatch))
if (list_[i].match(text, literal))
{
return true;
}
@ -75,4 +69,24 @@ inline bool Foam::wordReListMatcher::match
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline const Foam::UList<Foam::wordRe>& Foam::wordRes::operator()() const
{
return list_;
}
inline bool Foam::wordRes::operator()(const std::string& text) const
{
return match(text);
}
inline const Foam::wordRe& Foam::wordRes::operator[](const label i) const
{
return list_[i];
}
// ************************************************************************* //

View File

@ -151,7 +151,7 @@ void Foam::ensightMesh::correct()
useAll = false;
matched = findMatchingStrings
(
wordReListMatcher(matcher),
wordRes(matcher),
patchNames
);
}
@ -250,7 +250,7 @@ void Foam::ensightMesh::correct()
wordList selectZones = mesh_.faceZones().names();
inplaceSubsetMatchingStrings
(
wordReListMatcher(matcher),
wordRes(matcher),
selectZones
);

View File

@ -27,7 +27,7 @@ License
#include "volFields.H"
#include "dictionary.H"
#include "wordReListMatcher.H"
#include "wordRes.H"
#include "steadyStateDdtScheme.H"
#include "addToRunTimeSelectionTable.H"
@ -142,7 +142,7 @@ bool Foam::functionObjects::ddt2::read(const dictionary& dict)
return false;
}
selectFields_ = wordReListMatcher::uniq
selectFields_ = wordRes::uniq
(
wordReList(dict.lookup("fields"))
);

View File

@ -27,7 +27,7 @@ License
#include "volFields.H"
#include "dictionary.H"
#include "wordReListMatcher.H"
#include "wordRes.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -120,7 +120,7 @@ bool Foam::functionObjects::zeroGradient::read(const dictionary& dict)
{
fvMeshFunctionObject::read(dict);
selectFields_ = wordReListMatcher::uniq
selectFields_ = wordRes::uniq
(
wordReList(dict.lookup("fields"))
);

View File

@ -31,7 +31,7 @@ License
#include "volPointInterpolation.H"
#include "PatchTools.H"
#include "mapPolyMesh.H"
#include "wordReListMatcher.H"
#include "wordRes.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -268,7 +268,7 @@ bool Foam::surfMeshSamplers::write()
}
// avoid duplicate entries
select = wordReListMatcher::uniq(select);
select = wordRes::uniq(select);
forAll(*this, surfI)
{
@ -290,7 +290,7 @@ bool Foam::surfMeshSamplers::read(const dictionary& dict)
if (dict.found("surfaces"))
{
fieldSelection_ = wordReListMatcher::uniq
fieldSelection_ = wordRes::uniq
(
wordReList(dict.lookup("fields"))
);