diff --git a/applications/test/stringList/Test-stringList.C b/applications/test/stringList/Test-stringList.C index 0ed3416202..e336c54231 100644 --- a/applications/test/stringList/Test-stringList.C +++ b/applications/test/stringList/Test-stringList.C @@ -26,6 +26,7 @@ Description \*---------------------------------------------------------------------------*/ #include "stringListOps.H" +#include "FlatOutput.H" #include "IOstreams.H" #include "StringStream.H" @@ -64,7 +65,10 @@ int main(int argc, char *argv[]) matches = findStrings(reLst, strLst); - Info<< "matches found for " << reLst << nl << matches << nl; + Info<< "matching " << flatOutput(reLst) << " => " + << reLst.matching(strLst) << nl; + Info<< "matches found for " << flatOutput(reLst) << " => " + << matches << nl; forAll(matches, i) { Info<< " -> " << strLst[matches[i]] << nl; diff --git a/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C b/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C index 08b32ac370..2d1d725d14 100644 --- a/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C +++ b/applications/utilities/surface/surfaceFeatureExtract/surfaceFeatureExtract.C @@ -518,7 +518,8 @@ int main(int argc, char *argv[]) patchNames[patchi] = surf.patches()[patchi].name(); } - labelList indices = findStrings(baffleSelect, patchNames); + labelList indices(baffleSelect.matching(patchNames)); + for (const label patchId : indices) { surfBaffleRegions[patchId] = true; diff --git a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C index 2a216f5c5f..c7b8bd11b2 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C @@ -54,9 +54,9 @@ bool Foam::functionEntries::removeEntry::execute Istream& is ) { - const List patterns = functionEntry::readStringList(is); + const wordRes patterns(functionEntry::readStringList(is)); - for (const keyType& key : patterns) + for (const wordRe& key : patterns) { if (key.isLiteral() && key.find('/') != string::npos) { @@ -72,7 +72,7 @@ bool Foam::functionEntries::removeEntry::execute { // Remove by pattern const wordList dictKeys = parentDict.toc(); - const labelList indices = findStrings(regExp(key), dictKeys); + const labelList indices = findStrings(key, dictKeys); for (const auto idx : indices) { diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index b7de1c978f..e6cd41aca3 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -1160,14 +1160,17 @@ void Foam::argList::parse roots.resize(Pstream::nProcs()-1, fileName::null); source = "-hostRoots"; - ITstream is = this->lookup("hostRoots"); + ITstream is(source, options_["hostRoots"]); + List> hostRoots(is); checkITstream(is, "hostRoots"); for (const auto& hostRoot : hostRoots) { - const wordRe& re = hostRoot.first(); - labelList matched(findStrings(re, slaveMachine)); + labelList matched + ( + findStrings(hostRoot.first(), slaveMachine) + ); for (const label slavei : matched) { if (!roots[slavei].empty()) diff --git a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C index f150ec6ecc..a432ef7bd6 100644 --- a/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C +++ b/src/OpenFOAM/meshes/polyMesh/zones/ZoneMesh/ZoneMesh.C @@ -26,7 +26,6 @@ License #include "ZoneMesh.H" #include "entry.H" #include "demandDrivenData.H" -#include "stringListOps.H" #include "Pstream.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/strings/lists/stringListOps.H b/src/OpenFOAM/primitives/strings/lists/stringListOps.H index 485eb4bfb0..931de2ca66 100644 --- a/src/OpenFOAM/primitives/strings/lists/stringListOps.H +++ b/src/OpenFOAM/primitives/strings/lists/stringListOps.H @@ -27,6 +27,12 @@ InNamspace Description Operations on lists of strings. +Namespace + Foam::stringListOps + +Description + Various utility functions to work on lists of strings. + SourceFiles stringListOpsTemplates.C @@ -133,7 +139,7 @@ namespace Foam } - // 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 @@ -306,6 +312,50 @@ namespace Foam } + + +/*---------------------------------------------------------------------------*\ + Namespace stringListOps Declaration +\*---------------------------------------------------------------------------*/ + +namespace stringListOps +{ + +//- Functor to determine if a string is exists in a list of strings. +// For example, +// +// \code +// reduce(text, stringListOps::foundOp(myNames)); +// \endcode +template +struct foundOp +{ + const UList& values; + + foundOp(const UList& list) + : + values(list) + {} + + bool operator()(const std::string& text) const + { + return values.found(text); + } +}; + +} // End namespace stringListOps + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Housekeeping + +namespace Foam +{ //- Find using C-string as a regex // \deprecated (FEB-2018) Treating string as regex may be inefficient // and lead to unintended results. @@ -378,8 +428,7 @@ namespace Foam const bool invert=false ) = delete; -} - +} // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/strings/wordRes/wordRes.H b/src/OpenFOAM/primitives/strings/wordRes/wordRes.H index 197823682c..ac0fa9c9c9 100644 --- a/src/OpenFOAM/primitives/strings/wordRes/wordRes.H +++ b/src/OpenFOAM/primitives/strings/wordRes/wordRes.H @@ -126,7 +126,7 @@ public: // // \param literal Force literal match only. // \return True if text matches ANY of the entries. - inline bool match(const std::string& text, bool literal = false) const; + inline bool match(const std::string& text, bool literal=false) const; //- Smart match in the list of matchers, returning the match type. // It stops if there is a literal match, or continues to examine @@ -135,6 +135,19 @@ public: // match was found and UNKNOWN otherwise. inline wordRe::compOption matched(const std::string& text) const; + //- Extract list indices for all matches. + // + // \param input A list of string inputs to match against + // \param invert invert the matching logic + // \return The locations (indices) in the input list where match() + // is true + template + inline labelList matching + ( + const UList& input, + const bool invert=false + ) const; + // Member Operators diff --git a/src/OpenFOAM/primitives/strings/wordRes/wordResI.H b/src/OpenFOAM/primitives/strings/wordRes/wordResI.H index 65cbe2fe46..7b45f02009 100644 --- a/src/OpenFOAM/primitives/strings/wordRes/wordResI.H +++ b/src/OpenFOAM/primitives/strings/wordRes/wordResI.H @@ -95,6 +95,32 @@ Foam::wordRes::matched(const std::string& text) const } +template +inline Foam::labelList Foam::wordRes::matching +( + const UList& input, + const bool invert +) const +{ + const label len = input.size(); + + labelList indices(len); + + label count = 0; + for (label i=0; i < len; ++i) + { + if (match(input[i]) ? !invert : invert) + { + indices[count] = i; + ++count; + } + } + indices.resize(count); + + return indices; +} + + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // inline bool Foam::wordRes::operator()(const std::string& text) const diff --git a/src/conversion/ensight/mesh/ensightMesh.C b/src/conversion/ensight/mesh/ensightMesh.C index 918ef73253..2679a62cfd 100644 --- a/src/conversion/ensight/mesh/ensightMesh.C +++ b/src/conversion/ensight/mesh/ensightMesh.C @@ -146,7 +146,7 @@ void Foam::ensightMesh::correct() if (!matcher.empty()) { useAll = false; - matched = findStrings(matcher, patchNames); + matched = matcher.matching(patchNames); } } diff --git a/src/functionObjects/field/ddt2/ddt2.C b/src/functionObjects/field/ddt2/ddt2.C index 73a846f372..9fe5cb4728 100644 --- a/src/functionObjects/field/ddt2/ddt2.C +++ b/src/functionObjects/field/ddt2/ddt2.C @@ -24,7 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "ddt2.H" - +#include "stringListOps.H" #include "volFields.H" #include "dictionary.H" #include "wordRes.H" diff --git a/src/functionObjects/field/externalCoupled/externalCoupled.C b/src/functionObjects/field/externalCoupled/externalCoupled.C index c318b3cb9f..8f05a20e29 100644 --- a/src/functionObjects/field/externalCoupled/externalCoupled.C +++ b/src/functionObjects/field/externalCoupled/externalCoupled.C @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "externalCoupled.H" +#include "stringListOps.H" #include "addToRunTimeSelectionTable.H" #include "OSspecific.H" #include "Fstream.H" diff --git a/src/functionObjects/field/zeroGradient/zeroGradient.C b/src/functionObjects/field/zeroGradient/zeroGradient.C index 719b38d900..e1e79010c8 100644 --- a/src/functionObjects/field/zeroGradient/zeroGradient.C +++ b/src/functionObjects/field/zeroGradient/zeroGradient.C @@ -24,7 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "zeroGradient.H" - +#include "stringListOps.H" #include "volFields.H" #include "dictionary.H" #include "wordRes.H" diff --git a/src/parallel/decompose/decompositionMethods/decompositionConstraints/preserveFaceZones/preserveFaceZonesConstraint.C b/src/parallel/decompose/decompositionMethods/decompositionConstraints/preserveFaceZones/preserveFaceZonesConstraint.C index cc5c495a64..27b5fd598d 100644 --- a/src/parallel/decompose/decompositionMethods/decompositionConstraints/preserveFaceZones/preserveFaceZonesConstraint.C +++ b/src/parallel/decompose/decompositionMethods/decompositionConstraints/preserveFaceZones/preserveFaceZonesConstraint.C @@ -96,7 +96,7 @@ void Foam::decompositionConstraints::preserveFaceZones::add const faceZoneMesh& fZones = mesh.faceZones(); - const labelList zoneIDs(findStrings(zones_, fZones.names())); + const labelList zoneIDs(zones_.matching(fZones.names())); label nUnblocked = 0; @@ -167,7 +167,7 @@ void Foam::decompositionConstraints::preserveFaceZones::apply const faceZoneMesh& fZones = mesh.faceZones(); - const labelList zoneIDs(findStrings(zones_, fZones.names())); + const labelList zoneIDs(zones_.matching(fZones.names())); label nChanged = 0;