diff --git a/src/OSspecific/POSIX/regExp.H b/src/OSspecific/POSIX/regExp.H index edc8ddd6c01f792f80188a031fc0d5669633ba62..cd140fe8522b5a35863be5aadce340e00d2d80b5 100644 --- a/src/OSspecific/POSIX/regExp.H +++ b/src/OSspecific/POSIX/regExp.H @@ -157,7 +157,7 @@ public: // The begin-of-line (^) and end-of-line ($) anchors are implicit bool match(const string&, List<string>& groups) const; - //- Return true if the regex was found in within string + //- Return true if the regex was found within string bool search(const std::string& str) const { return std::string::npos != find(str); diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index e57d3ab2a136da54b54eaf6e9fec6832412515dd..4e356a2f05c381c2cbb32adb978374ddf3143677 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -52,8 +52,8 @@ $(strings)/word/word.C $(strings)/word/wordIO.C $(strings)/fileName/fileName.C $(strings)/fileName/fileNameIO.C -$(strings)/keyType/keyTypeIO.C -$(strings)/wordRe/wordReIO.C +$(strings)/keyType/keyType.C +$(strings)/wordRe/wordRe.C primitives/hashes/Hasher/Hasher.C diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index dce6d6a90730e871073990a75a4ad782ed507642..8eae249c52ceba16c67bbf61481ac52332aeb3aa 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -93,6 +93,8 @@ public: static const char* const typeName; static int debug; + + //- An empty fileName static const fileName null; diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C b/src/OpenFOAM/primitives/strings/keyType/keyType.C similarity index 67% rename from src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C rename to src/OpenFOAM/primitives/strings/keyType/keyType.C index 9ba61ee510db713dd77080e3e24655cbe0cf361e..5f8c6956b748e117c08d7ac10fc372365bb606b2 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyTypeIO.C +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.C @@ -22,24 +22,54 @@ License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. Description - Istream constructor and IOstream operators for word. + Istream constructor and IOstream operators for keyType. \*---------------------------------------------------------------------------*/ #include "keyType.H" +#include "regExp.H" #include "IOstreams.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const Foam::keyType Foam::keyType::null; + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::keyType::keyType(Istream& is) : - word() + word(), + isPattern_(false) +{ + is >> *this; +} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +bool Foam::keyType::match +( + const std::string& str, + bool literalMatch +) const { - is >> *this; + if (literalMatch || !isPattern_) + { + // check as string + return (str == *this); + } + else + { + // check as regex + return regExp(*this).match(str); + } } -Foam::Istream& Foam::operator>>(Istream& is, keyType& w) +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + +Foam::Istream& Foam::operator>>(Istream& is, keyType& kw) { token t(is); @@ -51,15 +81,16 @@ Foam::Istream& Foam::operator>>(Istream& is, keyType& w) if (t.isWord()) { - w = t.wordToken(); + kw = t.wordToken(); } else if (t.isString()) { - // Assign from string. Sets regular expression. - w = t.stringToken(); + // Assign from string. Set as regular expression. + kw = t.stringToken(); + kw.isPattern_ = true; // flag empty strings as an error - if (w.empty()) + if (kw.empty()) { is.setBad(); FatalIOErrorIn("operator>>(Istream&, keyType&)", is) @@ -86,9 +117,9 @@ Foam::Istream& Foam::operator>>(Istream& is, keyType& w) } -Foam::Ostream& Foam::operator<<(Ostream& os, const keyType& w) +Foam::Ostream& Foam::operator<<(Ostream& os, const keyType& kw) { - os.write(w); + os.write(kw); os.check("Ostream& operator<<(Ostream&, const keyType&)"); return os; } diff --git a/src/OpenFOAM/primitives/strings/keyType/keyType.H b/src/OpenFOAM/primitives/strings/keyType/keyType.H index 8cf22f9473b8832be0d4397a926b22ae2c14262c..964044150fcaf45c81c125118b1f6e35463f5c0f 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyType.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyType.H @@ -32,7 +32,6 @@ Description SourceFiles keyType.C - keyTypeIO.C \*---------------------------------------------------------------------------*/ @@ -52,14 +51,14 @@ class Ostream; /*---------------------------------------------------------------------------*\ - Class keyType Declaration + Class keyType Declaration \*---------------------------------------------------------------------------*/ class keyType : public word { - // Private member data + // Private data //- Is the keyType a pattern (regular expression) bool isPattern_; @@ -71,6 +70,11 @@ class keyType public: + // Static data members + + //- An empty keyType + static const keyType null; + // Constructors @@ -80,19 +84,21 @@ public: //- Construct as copy inline keyType(const keyType&); - //- Construct as copy of word + //- Construct as copy of word. Not treated as a regular expression inline keyType(const word&); - //- Construct as copy of string. Expect it to be regular expression. + //- Construct as copy of string. Treat as regular expression. inline keyType(const string&); - //- Construct as copy of character array + //- Construct as copy of character array. + // Not treated as a regular expression inline keyType(const char*); - //- Construct as copy of std::string + //- Construct as copy of std::string with specified treatment inline keyType(const std::string&, const bool isPattern); //- Construct from Istream + // Treat as regular expression if surrounded by quotation marks. keyType(Istream&); @@ -101,15 +107,24 @@ public: //- Should be treated as a match rather than a literal string inline bool isPattern() const; + //- Smart match as regular expression or as a string + // Optionally force a literal match only + bool match(const std::string&, bool literalMatch=false) const; + // Member operators // Assignment + //- Assignment operator inline const keyType& operator=(const keyType&); + + //- Assign as word, not as non regular expression inline const keyType& operator=(const word&); - //- Assign from regular expression. + //- Assign as regular expression inline const keyType& operator=(const string&); + + //- Assign as word, not as non regular expression inline const keyType& operator=(const char*); diff --git a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H index 653ea8786ef4deceaec95196dfe9135ab623b20f..72752f3ddd34dc79174db2951fdba472febcf168 100644 --- a/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H +++ b/src/OpenFOAM/primitives/strings/keyType/keyTypeI.H @@ -23,10 +23,6 @@ License \*---------------------------------------------------------------------------*/ - -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // inline Foam::keyType::keyType() @@ -50,7 +46,6 @@ inline Foam::keyType::keyType(const word& s) {} -// Construct as copy of string. Expect it to be regular expression inline Foam::keyType::keyType(const string& s) : word(s, false), @@ -58,7 +53,6 @@ inline Foam::keyType::keyType(const string& s) {} -// Construct as copy of character array inline Foam::keyType::keyType(const char* s) : word(s, false), @@ -66,7 +60,6 @@ inline Foam::keyType::keyType(const char* s) {} -//- Construct as copy of std::string inline Foam::keyType::keyType ( const std::string& s, diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index fe50c5b530f12f9042cbeb00d961b82b26ba549f..254ffbba0f9e608ff77a610148b27c30c0dea9f5 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -82,6 +82,8 @@ public: static const char* const typeName; static int debug; + + //- An empty string static const string null; @@ -143,6 +145,9 @@ public: template<class String> static inline string quotemeta(const string&, const char quote='\\'); + //- True when strings match literally + inline bool match(const std::string&) const; + //- Avoid masking the normal std::string replace using std::string::replace; diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index 70a8b6f972d8e52cc6cb34d3c813269107c1922a..83f4ed0c45bdc036db5650bf8349e54cdb706242 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -176,6 +176,12 @@ inline String Foam::string::validate(const string& str) return ss; } +inline bool Foam::string::match(const std::string& str) const +{ + // check as string + return (str == *this); +} + // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // diff --git a/src/OpenFOAM/primitives/strings/word/word.H b/src/OpenFOAM/primitives/strings/word/word.H index a30eb10d1bccf315e00fae1588eb25dc77036412..032803084dca6644a2c2df3ee215ce5212325474 100644 --- a/src/OpenFOAM/primitives/strings/word/word.H +++ b/src/OpenFOAM/primitives/strings/word/word.H @@ -73,6 +73,8 @@ public: static const char* const typeName; static int debug; + + //- An empty word static const word null; diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReIO.C b/src/OpenFOAM/primitives/strings/wordRe/wordRe.C similarity index 91% rename from src/OpenFOAM/primitives/strings/wordRe/wordReIO.C rename to src/OpenFOAM/primitives/strings/wordRe/wordRe.C index eff08be77b0ac93d0afb18f1aede40073cd0fb17..693ae0535dbffb97ed6c5eb1499584a75a58b679 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordReIO.C +++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.C @@ -27,7 +27,12 @@ License #include "IOstreams.H" #include "InfoProxy.H" -// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +const Foam::wordRe Foam::wordRe::null; + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::wordRe::wordRe(Istream& is) : @@ -38,6 +43,8 @@ Foam::wordRe::wordRe(Istream& is) } +// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // + Foam::Istream& Foam::operator>>(Istream& is, wordRe& w) { token t(is); diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H index 99bfb1188a39672eac85e3fb112f3ac3ed16a165..16d86b7fcfccc2ab66f761a51143e3e8ceea48a4 100644 --- a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H +++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H @@ -44,7 +44,6 @@ Note SourceFiles wordRe.C - wordReIO.C \*---------------------------------------------------------------------------*/ @@ -84,6 +83,12 @@ class wordRe public: + // Static data members + + //- An empty wordRe + static const wordRe null; + + // Public data types //- Enumeration with compile options @@ -168,7 +173,7 @@ public: //- Searching //- Smart match as regular expression or as a string - // Optionally specify a literal match only + // Optionally force a literal match only inline bool match(const std::string&, bool literalMatch=false) const; //- Miscellaneous