Commit c973b75f authored by Mark OLESEN's avatar Mark OLESEN
Browse files

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.
parent 9fda726a
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
......@@ -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);
......
......@@ -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);
......
......@@ -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
......
......@@ -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());
......
......@@ -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
}
}
......
......@@ -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
......
......@@ -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
......
......@@ -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
//- Return name corresponding to specified index
inline const word& operator[](const label index) const;
//- 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
//- Assignment operator from initializer list. Rehashes the indices.
inline void operator=(std::initializer_list<word> lst);
//- Assignment operator.
//- Assignment operator. Rehashes the indices.
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
inline label operator[](const word& name) const;
// 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);
};
......
......@@ -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())
{
label nElem = 0;
for
(
typename HashTable<AnyType, word, AnyHash>::const_iterator
iter = h.cbegin();
iter != h.cend();
++iter
)
List<word>(tbl.size())
{
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,39 +156,51 @@ inline void Foam::hashedWordList::sort()
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
inline void Foam::hashedWordList::operator=(const UList<word>& lst)
inline const Foam::word& Foam::hashedWordList::operator[]
(
const label index
) const
{
List<word>::operator=(lst);
rehash();
return List<word>::operator[](index);
}
inline void Foam::hashedWordList::operator=(std::initializer_list<word> lst)
inline Foam::label Foam::hashedWordList::operator[](const word& name) const
{
List<word>::operator=(lst);
rehash();
auto iter = indices_.find(name);
if (iter.found())
{
return iter.object();
}
return -1; // Not found (or not hashed?)
}
inline void Foam::hashedWordList::operator=(const hashedWordList& lst)
inline bool Foam::hashedWordList::operator()(const word& name) const
{
operator=(static_cast<const UList<word>&>(lst));
return indices_.found(name);
}
inline const Foam::word& Foam::hashedWordList::operator[]
(
const label index
) const
inline void Foam::hashedWordList::operator=(const UList<word>& lst)
{
return List<word>::operator[](index);
List<word>::operator=(lst);
rehash();
}
inline Foam::label Foam::hashedWordList::operator[](const word& name) const
inline void Foam::hashedWordList::operator=(std::initializer_list<word> lst)
{
// Could return -1 instead of bombing out
return indices_[name];
List<word>::operator=(lst);
rehash();
}
inline void Foam::hashedWordList::operator=(const hashedWordList& lst)
{
operator=(static_cast<const UList<word>&>(lst));
}
......
......@@ -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 regExp& matcher,
const UList<StringType>& lst,
const bool invert=false
)
{
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 char* 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);