From a8d2ebf2983fdd8abbf028acf8a884d957773f7e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 16 May 2017 23:54:43 +0200 Subject: [PATCH] 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 , 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. --- applications/test/fileName/Test-fileName.C | 6 +- applications/test/wordRe/Test-wordRe.C | 30 +++- .../doxygenXmlParser/doxygenXmlParser.C | 8 +- .../dataConversion/foamToVTK/foamToVTK.C | 6 +- .../createZeroDirectory/boundaryInfo.C | 2 +- src/OSspecific/POSIX/regExp.C | 12 +- src/OSspecific/POSIX/regExp.H | 16 +- src/OSspecific/POSIX/regExpI.H | 6 + src/OpenFOAM/Make/files | 2 +- src/OpenFOAM/db/IOobjectList/IOobjectList.C | 2 +- .../primitives/strings/keyType/keyType.C | 6 +- .../primitives/strings/keyType/keyType.H | 25 ++- .../primitives/strings/keyType/keyTypeI.H | 6 + .../primitives/strings/lists/hashedWordList.H | 41 +++-- .../strings/lists/hashedWordListI.H | 60 ++++--- .../primitives/strings/lists/stringListOps.H | 167 ++++++++++-------- .../strings/lists/stringListOpsTemplates.C | 52 +++--- .../primitives/strings/string/string.H | 14 +- .../primitives/strings/string/stringI.H | 14 +- src/OpenFOAM/primitives/strings/word/word.H | 14 +- src/OpenFOAM/primitives/strings/word/wordI.H | 4 +- .../primitives/strings/wordRe/wordRe.H | 52 +++--- .../primitives/strings/wordRe/wordReI.H | 138 +++++++++------ .../strings/wordRes/wordReListMatcher.H | 51 ++++++ .../wordReListMatcher.C => wordRes/wordRes.C} | 10 +- .../wordReListMatcher.H => wordRes/wordRes.H} | 78 +++++--- .../wordResI.H} | 52 ++++-- src/conversion/ensight/mesh/ensightMesh.C | 4 +- src/functionObjects/field/ddt2/ddt2.C | 4 +- .../field/zeroGradient/zeroGradient.C | 4 +- .../surfMeshSamplers/surfMeshSamplers.C | 6 +- 31 files changed, 556 insertions(+), 336 deletions(-) create mode 100644 src/OpenFOAM/primitives/strings/wordRes/wordReListMatcher.H rename src/OpenFOAM/primitives/strings/{lists/wordReListMatcher.C => wordRes/wordRes.C} (89%) rename src/OpenFOAM/primitives/strings/{lists/wordReListMatcher.H => wordRes/wordRes.H} (57%) rename src/OpenFOAM/primitives/strings/{lists/wordReListMatcherI.H => wordRes/wordResI.H} (67%) diff --git a/applications/test/fileName/Test-fileName.C b/applications/test/fileName/Test-fileName.C index 18f89b6db0..5de8912bd8 100644 --- a/applications/test/fileName/Test-fileName.C +++ b/applications/test/fileName/Test-fileName.C @@ -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; diff --git a/applications/test/wordRe/Test-wordRe.C b/applications/test/wordRe/Test-wordRe.C index f5dfcda831..f3ab4a3719 100644 --- a/applications/test/wordRe/Test-wordRe.C +++ b/applications/test/wordRe/Test-wordRe.C @@ -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; diff --git a/applications/utilities/miscellaneous/foamHelp/helpTypes/doxygenXmlParser/doxygenXmlParser.C b/applications/utilities/miscellaneous/foamHelp/helpTypes/doxygenXmlParser/doxygenXmlParser.C index 0de175b4b7..bc67aabd7f 100644 --- a/applications/utilities/miscellaneous/foamHelp/helpTypes/doxygenXmlParser/doxygenXmlParser.C +++ b/applications/utilities/miscellaneous/foamHelp/helpTypes/doxygenXmlParser/doxygenXmlParser.C @@ -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); diff --git a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C index c2a6f3b298..602f68b2d0 100644 --- a/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C +++ b/applications/utilities/postProcessing/dataConversion/foamToVTK/foamToVTK.C @@ -199,7 +199,7 @@ void print(Ostream& os, const wordList& flds) labelList getSelectedPatches ( const polyBoundaryMesh& patches, - const List& excludePatches + const wordRes& excludePatches ) { DynamicList