From 8d6f83e6667f744fa494dbf475986cc32e449365 Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Sun, 7 Oct 2018 17:28:11 +0200
Subject: [PATCH] ENH: isLiteral() method for keyType and wordRe

- same as !isPattern(), but can be more readable.

- add wordRe enum state 'UNKNOWN', which has the identical value as
  'DETECT' but used for a return value.
---
 .../mesh/manipulation/subsetMesh/subsetMesh.C |  2 +-
 .../functionEntries/removeEntry/removeEntry.C |  2 +-
 .../GeometricField/GeometricBoundaryField.C   |  4 ++--
 .../primitives/strings/keyType/keyType.H      | 23 ++++++++++---------
 .../primitives/strings/keyType/keyTypeI.H     |  7 +++++-
 .../primitives/strings/wordRe/wordRe.H        | 13 +++++++----
 .../primitives/strings/wordRe/wordReI.H       |  8 ++++++-
 src/functionObjects/field/ddt2/ddt2.C         |  4 ++--
 .../field/zeroGradient/zeroGradient.C         |  4 ++--
 .../utilities/ensightWrite/ensightWrite.C     |  2 +-
 10 files changed, 43 insertions(+), 26 deletions(-)

diff --git a/applications/utilities/mesh/manipulation/subsetMesh/subsetMesh.C b/applications/utilities/mesh/manipulation/subsetMesh/subsetMesh.C
index 78df05d1927..546b53d74db 100644
--- a/applications/utilities/mesh/manipulation/subsetMesh/subsetMesh.C
+++ b/applications/utilities/mesh/manipulation/subsetMesh/subsetMesh.C
@@ -440,7 +440,7 @@ int main(int argc, char *argv[])
     {
         const wordRes patchNames(args.getList<wordRe>("patches"));
 
-        if (patchNames.size() == 1 && !patchNames.first().isPattern())
+        if (patchNames.size() == 1 && patchNames.first().isLiteral())
         {
             exposedPatchIDs.first() =
                 getExposedPatchId(mesh, patchNames.first());
diff --git a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C
index 6a41293989e..25e452fce36 100644
--- a/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C
+++ b/src/OpenFOAM/db/dictionary/functionEntries/removeEntry/removeEntry.C
@@ -58,7 +58,7 @@ bool Foam::functionEntries::removeEntry::execute
 
     for (const keyType& key : patterns)
     {
-        if (key.find('/') != string::npos || !key.isPattern())
+        if (key.isLiteral() && key.find('/') != string::npos)
         {
             // Remove scoped keyword, or keyword in the local scope
             dictionary::searcher finder =
diff --git a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C
index 53faf5c0469..ee127be2788 100644
--- a/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C
+++ b/src/OpenFOAM/fields/GeometricFields/GeometricField/GeometricBoundaryField.C
@@ -53,7 +53,7 @@ readField
     //    patch name since is key of dictionary.
     forAllConstIter(dictionary, dict, iter)
     {
-        if (iter().isDict() && !iter().keyword().isPattern())
+        if (iter().isDict() && iter().keyword().isLiteral())
         {
             const label patchi = bmesh_.findPatchID(iter().keyword());
 
@@ -96,7 +96,7 @@ readField
         {
             const entry& e = iter();
 
-            if (e.isDict() && !e.keyword().isPattern())
+            if (e.isDict() && e.keyword().isLiteral())
             {
                 const labelList patchIds =
                     bmesh_.indices(e.keyword(), true); // use patchGroups
diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.H b/src/OpenFOAM/primitives/strings/keyType/keyType.H
index 188c66e738f..608468105f9 100644
--- a/src/OpenFOAM/primitives/strings/keyType/keyType.H
+++ b/src/OpenFOAM/primitives/strings/keyType/keyType.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -29,8 +29,7 @@ Description
 
     A keyType is the keyword of a dictionary.
     It differs from word in that it also accepts patterns (regular expressions).
-    It is very similar to wordRe, but doesn't store the regular expression
-    separately.
+    It is very similar to wordRe, but doesn't store a regular expression.
 
 SourceFiles
     keyType.C
@@ -47,12 +46,10 @@ SourceFiles
 namespace Foam
 {
 
-// Forward declaration of classes
+// Forward declarations
+class keyType;
 class Istream;
 class Ostream;
-
-// Forward declaration of friend functions and operators
-class keyType;
 Istream& operator>>(Istream& is, keyType& kw);
 Ostream& operator<<(Ostream& os, const keyType& kw);
 
@@ -70,6 +67,7 @@ class keyType
         //- Is the keyType a pattern (regular expression)
         bool isPattern_;
 
+
     // Private Member Functions
 
         //- No assignment where we cannot determine string/word type
@@ -121,14 +119,17 @@ public:
         keyType(Istream& is);
 
 
-    // Member functions
+    // Member Functions
 
         //- Is this character valid for a keyType?
         //  This is largely identical with what word accepts, but also
         //  permit brace-brackets, which are valid for some regexs.
         inline static bool valid(char c);
 
-        //- Treat as a pattern rather than a literal string?
+        //- The keyType is treated as literal, not as pattern.
+        inline bool isLiteral() const;
+
+        //- The keyType is treated as a pattern, not as literal string.
         inline bool isPattern() const;
 
         //- Swap contents
@@ -139,7 +140,7 @@ public:
         bool match(const std::string& text, bool literal = false) const;
 
 
-    // Member operators
+    // Member Operators
 
         //- Perform smart match on text, as per match()
         //  Allows use as a predicate.
@@ -162,7 +163,7 @@ public:
         inline void operator=(const char* s);
 
 
-    // IOstream operators
+    // IOstream Operators
 
         friend Istream& operator>>(Istream& is, keyType& kw);
         friend Ostream& operator<<(Ostream& os, const keyType& kw);
diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H
index 3885e6ecf2b..bf84de6a238 100644
--- a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H
+++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H
@@ -84,7 +84,6 @@ inline Foam::keyType::keyType(const std::string& s, const bool isPattern)
 {}
 
 
-
 inline Foam::keyType::keyType(keyType&& s)
 :
     word(std::move(static_cast<word&>(s)), false),
@@ -117,6 +116,12 @@ inline Foam::keyType::keyType(std::string&& s, const bool isPattern)
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
+inline bool Foam::keyType::isLiteral() const
+{
+    return !isPattern_;
+}
+
+
 inline bool Foam::keyType::isPattern() const
 {
     return isPattern_;
diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H
index 4c7294aab98..9976ecb6dce 100644
--- a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H
+++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H
@@ -59,7 +59,7 @@ SourceFiles
 namespace Foam
 {
 
-// Forward declaration of friend functions and operators
+// Forward declarations
 class wordRe;
 class Istream;
 class Ostream;
@@ -81,6 +81,7 @@ class wordRe
         //- The regular expression
         mutable regExp re_;
 
+
 public:
 
     // Static data members
@@ -97,6 +98,7 @@ public:
         {
             LITERAL = 0, //!< Treat as a string literal
             DETECT  = 1, //!< Detect if the string contains meta-characters
+            UNKNOWN = 1, //!< Unknown content.
             REGEX   = 2, //!< Treat as regular expression
             ICASE   = 4, //!< Ignore case in regular expression
             NOCASE  = 4, //!< \deprecated Alias for ICASE (deprecated APR-2018)
@@ -163,11 +165,14 @@ public:
         wordRe(Istream& is);
 
 
-    // Member functions
+    // Member Functions
 
     // Access
 
-        //- Treat as a pattern rather than a literal string?
+        //- The wordRe is treated as literal, not as pattern.
+        inline bool isLiteral() const;
+
+        //- The wordRe is treated as a pattern, not as literal string.
         inline bool isPattern() const;
 
 
@@ -248,7 +253,7 @@ public:
         inline void operator=(wordRe&& str);
 
 
-    // IOstream operators
+    // IOstream Operators
 
         friend Istream& operator>>(Istream& is, wordRe& w);
         friend Ostream& operator<<(Ostream& os, const wordRe& w);
diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H
index 5e9532c7b92..13579d76c72 100644
--- a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H
+++ b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H
@@ -3,7 +3,7 @@
   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
    \\    /   O peration     |
     \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-     \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
+     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
 -------------------------------------------------------------------------------
 License
     This file is part of OpenFOAM.
@@ -157,6 +157,12 @@ inline Foam::wordRe::wordRe(const word& str, const compOption opt)
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
+inline bool Foam::wordRe::isLiteral() const
+{
+    return !re_.exists();
+}
+
+
 inline bool Foam::wordRe::isPattern() const
 {
     return re_.exists();
diff --git a/src/functionObjects/field/ddt2/ddt2.C b/src/functionObjects/field/ddt2/ddt2.C
index d00fb6d0c6f..73a846f372e 100644
--- a/src/functionObjects/field/ddt2/ddt2.C
+++ b/src/functionObjects/field/ddt2/ddt2.C
@@ -174,9 +174,9 @@ bool Foam::functionObjects::ddt2::execute()
     // Check exact matches first
     for (const wordRe& select : selectFields_)
     {
-        if (!select.isPattern())
+        if (select.isLiteral())
         {
-            const word& fieldName = static_cast<const word&>(select);
+            const word& fieldName = select;
 
             if (!candidates.erase(fieldName))
             {
diff --git a/src/functionObjects/field/zeroGradient/zeroGradient.C b/src/functionObjects/field/zeroGradient/zeroGradient.C
index 1575a9be394..f5b36b79a7d 100644
--- a/src/functionObjects/field/zeroGradient/zeroGradient.C
+++ b/src/functionObjects/field/zeroGradient/zeroGradient.C
@@ -133,9 +133,9 @@ bool Foam::functionObjects::zeroGradient::execute()
     // Check exact matches first
     for (const wordRe& select : selectFields_)
     {
-        if (!select.isPattern())
+        if (select.isLiteral())
         {
-            const word& fieldName = static_cast<const word&>(select);
+            const word& fieldName = select;
 
             if (!candidates.erase(fieldName))
             {
diff --git a/src/functionObjects/utilities/ensightWrite/ensightWrite.C b/src/functionObjects/utilities/ensightWrite/ensightWrite.C
index 75e741c2cbd..710bee74fe9 100644
--- a/src/functionObjects/utilities/ensightWrite/ensightWrite.C
+++ b/src/functionObjects/utilities/ensightWrite/ensightWrite.C
@@ -284,7 +284,7 @@ bool Foam::functionObjects::ensightWrite::write()
     // Check exact matches first
     for (const wordRe& select : selectFields_)
     {
-        if (!select.isPattern())
+        if (select.isLiteral())
         {
             const word& fieldName = select;
 
-- 
GitLab