From b5342c166c2bf776b06a16fbe98b17973a1ce69a Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Tue, 20 Aug 2019 13:48:05 +0200 Subject: [PATCH] ENH: handle keyType type (literal/regex) as enum instead of bool - makes its use somewhat clearer and allows more future options --- .../test/dictionary/Test-dictionary.C | 2 +- .../Test-fvSolutionCombine.C | 2 +- applications/test/wordRe/Test-wordRe.C | 2 +- src/OpenFOAM/db/dictionary/entry/entryIO.C | 6 +- .../processorCyclicPolyPatch.C | 4 +- .../primitives/strings/keyType/keyType.C | 12 ++-- .../primitives/strings/keyType/keyType.H | 61 +++++++++++----- .../primitives/strings/keyType/keyTypeI.H | 71 +++++++++++-------- .../primitives/strings/wordRe/wordRe.H | 2 +- .../primitives/strings/wordRe/wordReI.H | 10 +-- 10 files changed, 105 insertions(+), 67 deletions(-) diff --git a/applications/test/dictionary/Test-dictionary.C b/applications/test/dictionary/Test-dictionary.C index c07087658b..368e8ec93e 100644 --- a/applications/test/dictionary/Test-dictionary.C +++ b/applications/test/dictionary/Test-dictionary.C @@ -96,7 +96,7 @@ int main(int argc, char *argv[]) { dictionary dict(IFstream("testDictRegex")()); - dict.add(keyType("fooba[rz]", true), "anything"); + dict.add(keyType("fooba[rz]", keyType::REGEX), "anything"); dict.writeEntry("testDictRegex", Info); Info<< nl diff --git a/applications/test/fvSolutionCombine/Test-fvSolutionCombine.C b/applications/test/fvSolutionCombine/Test-fvSolutionCombine.C index fc7b1d9c48..85781f84b0 100644 --- a/applications/test/fvSolutionCombine/Test-fvSolutionCombine.C +++ b/applications/test/fvSolutionCombine/Test-fvSolutionCombine.C @@ -184,7 +184,7 @@ int main(int argc, char *argv[]) if (names[nameI] != oldNames[nameI]) { // make "(abc|def)" pattern - keyType renamed( "(" + names[nameI] + ")", true); + keyType renamed("(" + names[nameI] + ")", keyType::REGEX); solverDict.changeKeyword(oldNames[nameI], renamed); diff --git a/applications/test/wordRe/Test-wordRe.C b/applications/test/wordRe/Test-wordRe.C index 8917e84764..f8cb84ec35 100644 --- a/applications/test/wordRe/Test-wordRe.C +++ b/applications/test/wordRe/Test-wordRe.C @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) Foam::string s2("this .* file"); const char * s3 = "this .* file"; - keyType keyre("x.*", true); + keyType keyre("x.*", keyType::REGEX); wordReList wordrelist { diff --git a/src/OpenFOAM/db/dictionary/entry/entryIO.C b/src/OpenFOAM/db/dictionary/entry/entryIO.C index fbd2978828..b12f304367 100644 --- a/src/OpenFOAM/db/dictionary/entry/entryIO.C +++ b/src/OpenFOAM/db/dictionary/entry/entryIO.C @@ -404,7 +404,11 @@ bool Foam::entry::New if (keyName.find_first_of("\"'") == 0) { // Begins with a quote - treat as pattern - key = keyType(string::validate(keyName), true); + key = keyType + ( + string::validate(keyName), + keyType::REGEX + ); } else { diff --git a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C index c6b019e1b5..f0d31ff02d 100644 --- a/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C +++ b/src/OpenFOAM/meshes/polyMesh/polyPatches/constraint/processorCyclic/processorCyclicPolyPatch.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | + \\ / A nd | Copyright (C) 2019 OpenCFD Ltd. \\/ M anipulation | ------------------------------------------------------------------------------- | Copyright (C) 2011-2016 OpenFOAM Foundation @@ -183,7 +183,7 @@ Foam::labelList Foam::processorCyclicPolyPatch::patchIDs keyType ( "procBoundary.*to.*through" + cyclicPolyPatchName, - true // isPattern + keyType::REGEX ) ); } diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.C b/src/OpenFOAM/primitives/strings/keyType/keyType.C index d15e2497e3..f15595037d 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyType.C +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.C @@ -39,7 +39,7 @@ const Foam::keyType Foam::keyType::null; Foam::keyType::keyType(Istream& is) : word(), - isPattern_(false) + type_(option::LITERAL) { is >> *this; } @@ -49,12 +49,12 @@ Foam::keyType::keyType(Istream& is) bool Foam::keyType::match(const std::string& text, bool literal) const { - if (literal || !isPattern_) + if (!literal && isPattern()) { - return !compare(text); // Compare as literal string + return regExp(*this).match(text); // Match as regex } - return regExp(*this).match(text); // Match as regex + return !compare(text); // Compare as literal } @@ -76,13 +76,13 @@ Foam::Istream& Foam::operator>>(Istream& is, keyType& val) if (t.isWord()) { val = t.wordToken(); - val.uncompile(); // Non-regex + val.setType(keyType::LITERAL); } else if (t.isString()) { // Assign from string, treat as regular expression val = t.stringToken(); - val.compile(); // As regex + val.setType(keyType::REGEX); // Flag empty strings as an error if (val.empty()) diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.H b/src/OpenFOAM/primitives/strings/keyType/keyType.H index ba04851f4e..471af87d4f 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyType.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.H @@ -42,6 +42,7 @@ SourceFiles #define keyType_H #include "word.H" +#include "stdFoam.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -60,10 +61,32 @@ class keyType : public word { +public: + + // Public Data Types + + //- Enumeration for the data type and search/match modes (bitmask) + // eg, (keyType::REGEX | keyType::RECURSIVE) + enum option : unsigned char + { + // Base stored types + LITERAL = 0, //!< String literal + REGEX = 1, //!< Regular expression + + // Variants for search/match only + RECURSIVE = 0x80, //!< Recursive search (eg, in dictionary) + LITERAL_RECURSIVE = (LITERAL | RECURSIVE), + REGEX_RECURSIVE = (REGEX | RECURSIVE) + }; + + +private: + // Private Data - //- Treat keyType as a pattern (regular expression) - bool isPattern_; + //- Treat keyType as literal, regex etc. + // Never contains RECURSIVE values. + option type_; // Private Member Functions @@ -80,20 +103,6 @@ public: static const keyType null; - // Public Data Types - - //- Enumeration for search/match modes as bitmask - // eg, (keyType::REGEX | keyType::RECURSIVE) - enum option - { - LITERAL = 0, //!< String literal - REGEX = 1, //!< Regular expression - RECURSIVE = 0x10, //!< Recursive search (eg, in dictionary) - LITERAL_RECURSIVE = (LITERAL | RECURSIVE), - REGEX_RECURSIVE = (REGEX | RECURSIVE) - }; - - // Constructors //- Construct null @@ -112,7 +121,7 @@ public: inline keyType(const char* s); //- Copy construct from std::string with specified treatment - inline keyType(const std::string& s, bool isPattern); + inline keyType(const std::string& s, option opt); //- Move construct, retaining type (literal or regex) inline keyType(keyType&& s); @@ -124,7 +133,7 @@ public: inline keyType(string&& s); //- Move construct from std::string with specified treatment - inline keyType(std::string&& s, bool isPattern); + inline keyType(std::string&& s, option opt); //- Construct from Istream // Treat as regular expression if surrounded by quotation marks. @@ -150,12 +159,15 @@ public: // Infrastructure + //- Change the representation + inline void setType(option opt, bool adjust = false); + //- Mark as regular expression inline bool compile(); //- Mark as literal, instead of a regular expression. // Optionally strip invalid word characters. - inline void uncompile(bool doStrip = false); + inline void uncompile(bool adjust = false); // Editing @@ -197,6 +209,17 @@ public: //- Assign as word, treat as literal inline void operator=(const char* s); + + + // Housekeeping + + //- Deprecated(2019-08) construct as literal/regex + // \deprecated(2019-08) - use Construct with option + FOAM_DEPRECATED_FOR(2019-08, "Construct with option") + keyType(const std::string& s, bool isPattern) + : + keyType(s, (isPattern ? option::REGEX : option::LITERAL)) + {} }; diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H index 0039ded58a..75d056b431 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H @@ -47,72 +47,72 @@ inline bool Foam::keyType::valid(char c) inline Foam::keyType::keyType() : word(), - isPattern_(false) + type_(option::LITERAL) {} inline Foam::keyType::keyType(const keyType& s) : word(s, false), - isPattern_(s.isPattern()) + type_(s.type_) {} inline Foam::keyType::keyType(const word& s) : word(s, false), - isPattern_(false) + type_(option::LITERAL) {} inline Foam::keyType::keyType(const string& s) : word(s, false), - isPattern_(true) + type_(option::REGEX) {} inline Foam::keyType::keyType(const char* s) : word(s, false), - isPattern_(false) + type_(option::LITERAL) {} -inline Foam::keyType::keyType(const std::string& s, bool isPattern) +inline Foam::keyType::keyType(const std::string& s, option opt) : word(s, false), - isPattern_(isPattern) + type_(option(opt & 0x0F)) {} inline Foam::keyType::keyType(keyType&& s) : word(std::move(static_cast(s)), false), - isPattern_(s.isPattern()) + type_(s.type_) { - s.isPattern_ = false; + s.type_ = option::LITERAL; } inline Foam::keyType::keyType(word&& s) : word(std::move(s), false), - isPattern_(false) + type_(option::LITERAL) {} inline Foam::keyType::keyType(string&& s) : word(std::move(s), false), - isPattern_(true) + type_(option::REGEX) {} -inline Foam::keyType::keyType(std::string&& s, bool isPattern) +inline Foam::keyType::keyType(std::string&& s, option opt) : word(std::move(s), false), - isPattern_(isPattern) + type_(option(opt & 0x0F)) {} @@ -120,39 +120,50 @@ inline Foam::keyType::keyType(std::string&& s, bool isPattern) inline bool Foam::keyType::isLiteral() const { - return !isPattern_; + return (type_ != option::REGEX); } inline bool Foam::keyType::isPattern() const { - return isPattern_; + return (type_ & option::REGEX); +} + + +inline void Foam::keyType::setType(option opt, bool adjust) +{ + opt = option(opt & 0x0F); + + if (type_ != opt) + { + // Only strip when debug is active (potentially costly operation) + if (isPattern() && adjust && word::debug) + { + string::stripInvalid(*this); + } + + type_ = opt; + } } inline bool Foam::keyType::compile() { - isPattern_ = true; + type_ = option::REGEX; return true; } -inline void Foam::keyType::uncompile(bool doStrip) +inline void Foam::keyType::uncompile(bool adjust) { - // Only strip when debug is active (potentially costly operation) - if (isPattern_ && doStrip && word::debug) - { - string::stripInvalid(*this); - } - - isPattern_ = false; + setType(option::LITERAL, adjust); } inline void Foam::keyType::clear() { word::clear(); - isPattern_ = false; + type_ = option::LITERAL; } @@ -165,7 +176,7 @@ inline void Foam::keyType::swap(keyType& s) } word::swap(static_cast(s)); - std::swap(isPattern_, s.isPattern_); + std::swap(type_, s.type_); } @@ -186,7 +197,7 @@ inline void Foam::keyType::operator=(const keyType& s) } assign(s); // Bypasses char checking - isPattern_ = s.isPattern_; + type_ = s.type_; } @@ -206,21 +217,21 @@ inline void Foam::keyType::operator=(keyType&& s) inline void Foam::keyType::operator=(const word& s) { assign(s); // Bypasses char checking - isPattern_ = false; + type_ = option::LITERAL; } inline void Foam::keyType::operator=(const string& s) { assign(s); // Bypasses char checking - isPattern_ = true; + type_ = option::REGEX; } inline void Foam::keyType::operator=(const char* s) { assign(s); // Bypasses char checking - isPattern_ = false; + type_ = option::LITERAL; } diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H index 50496ec98a..dc74bad061 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H @@ -183,7 +183,7 @@ public: //- Make wordRe a literal again, instead of a regular expression. // Optionally strip invalid word characters. - inline void uncompile(bool doStrip = false); + inline void uncompile(bool adjust = false); // Editing diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H index 02a5df8943..0ef69debaa 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H @@ -208,10 +208,10 @@ inline bool Foam::wordRe::compile() } -inline void Foam::wordRe::uncompile(bool doStrip) +inline void Foam::wordRe::uncompile(bool adjust) { // Only strip when debug is active (potentially costly operation) - if (re_.clear() && doStrip && word::debug) + if (re_.clear() && adjust && word::debug) { string::stripInvalid(*this); } @@ -227,12 +227,12 @@ inline void Foam::wordRe::clear() inline bool Foam::wordRe::match(const std::string& text, bool literal) const { - if (literal || !re_.exists()) + if (!literal && re_.exists()) { - return !compare(text); // Compare as literal string + return re_.match(text); // Match as regex } - return re_.match(text); // Match as regex + return !compare(text); // Compare as literal }