From 2717aa5c7dc2b76701a80e66cdcf95265a30cc3b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Sun, 4 Jan 2009 00:33:27 +0100 Subject: [PATCH] new wordRe class - a word that holds a regExp - a possible future replacement for keyType, but the immediate use is the wordReList for grepping through other lists. - note that the argList treatment of '(' ... ')' yields quoted strings, which we can use for building a wordReList minor cleanup of regExp class - constructor from std::string, match std::string and operator=(std::string&) rely on automatic conversion to Foam::string - ditch partialMatch with sub-groups, it doesn't make much sense --- applications/test/string/stringTest.C | 19 ++ applications/test/wordRe/Make/files | 3 + applications/test/wordRe/Make/options | 0 applications/test/wordRe/testRegexps | 32 +++ applications/test/wordRe/wordReTest.C | 105 +++++++ src/OSspecific/Unix/regExp.C | 76 +++-- src/OSspecific/Unix/regExp.H | 70 +++-- src/OpenFOAM/Make/files | 1 + src/OpenFOAM/primitives/Lists/wordReList.H | 50 ++++ .../primitives/strings/fileName/fileName.C | 31 +-- .../primitives/strings/fileName/fileName.H | 14 +- .../primitives/strings/fileName/fileNameI.H | 10 +- .../primitives/strings/fileName/fileNameIO.C | 4 +- .../primitives/strings/string/string.C | 21 +- .../primitives/strings/string/string.H | 124 +++++---- .../primitives/strings/string/stringI.H | 76 ++++- .../primitives/strings/wordRe/wordRe.H | 206 ++++++++++++++ .../primitives/strings/wordRe/wordReI.H | 263 ++++++++++++++++++ .../primitives/strings/wordRe/wordReIO.C | 113 ++++++++ 19 files changed, 1036 insertions(+), 182 deletions(-) create mode 100644 applications/test/wordRe/Make/files create mode 100644 applications/test/wordRe/Make/options create mode 100644 applications/test/wordRe/testRegexps create mode 100644 applications/test/wordRe/wordReTest.C create mode 100644 src/OpenFOAM/primitives/Lists/wordReList.H create mode 100644 src/OpenFOAM/primitives/strings/wordRe/wordRe.H create mode 100644 src/OpenFOAM/primitives/strings/wordRe/wordReI.H create mode 100644 src/OpenFOAM/primitives/strings/wordRe/wordReIO.C diff --git a/applications/test/string/stringTest.C b/applications/test/string/stringTest.C index e9ac6532f6..7981356760 100644 --- a/applications/test/string/stringTest.C +++ b/applications/test/string/stringTest.C @@ -40,6 +40,25 @@ int main(int argc, char *argv[]) Info<< test << endl; + // test sub-strings via iterators + string::const_iterator iter = test.end(); + string::const_iterator iter2 = test.end(); + string::size_type fnd = test.find('\\'); + + if (fnd != string::npos) + { + iter = test.begin() + fnd; + iter2 = iter + 6; + } + + Info<< "sub-string via iterators : >"; + while (iter != iter2) + { + Info<< *iter; + iter++; + } + Info<< "<\n"; + Info<< string(test).replace("kj", "zzz") << endl; Info<< string(test).replace("kj", "") << endl; Info<< string(test).replaceAll("kj", "zzz") << endl; diff --git a/applications/test/wordRe/Make/files b/applications/test/wordRe/Make/files new file mode 100644 index 0000000000..1cb69f314c --- /dev/null +++ b/applications/test/wordRe/Make/files @@ -0,0 +1,3 @@ +wordReTest.C + +EXE = $(FOAM_USER_APPBIN)/wordReTest diff --git a/applications/test/wordRe/Make/options b/applications/test/wordRe/Make/options new file mode 100644 index 0000000000..e69de29bb2 diff --git a/applications/test/wordRe/testRegexps b/applications/test/wordRe/testRegexps new file mode 100644 index 0000000000..f27cc60b87 --- /dev/null +++ b/applications/test/wordRe/testRegexps @@ -0,0 +1,32 @@ +/*-------------------------------*- C++ -*---------------------------------*\ +| ========= | +| \\ / OpenFOAM | +| \\ / | +| \\ / The Open Source CFD Toolbox | +| \\/ http://www.OpenFOAM.org | +\*-------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// wordRe, string +( + ( "a.*" "abc" ) + ( "a.*" "bac" ) + ( "a.*" "abcd" ) + ( "a.*" "def" ) + ( "d(.*)f" "def" ) + ( "plain" "def" ) + ( "plain" "def" ) + ( "plain\\(0\\)" "def" ) + ( "plain\(0\)" "ghi" ) + ( "regex(0)" "ghi" ) + ( "plain\\\(0\\\)" "ghi" ) + ( "this" "def" ) + ( "this" "this" ) + ( plain\\(0\\) "def" ) + ( plain\(0\) "ghi" ) + ( plain\\\(0\\\) "ghi" ) + ( "done" "done" ) +) + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // diff --git a/applications/test/wordRe/wordReTest.C b/applications/test/wordRe/wordReTest.C new file mode 100644 index 0000000000..54dcf1703c --- /dev/null +++ b/applications/test/wordRe/wordReTest.C @@ -0,0 +1,105 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Description + +\*---------------------------------------------------------------------------*/ + +#include "IOstreams.H" +#include "IOobject.H" +#include "IFstream.H" +#include "List.H" +#include "Tuple2.H" +#include "wordRe.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + wordRe wre; + std::string s1("this .* file"); + Foam::string s2("this .* file"); + const char * s3 = "this .* file"; + + Info<< wordRe(s1).info() << endl; + Info<< wordRe(s2, false).info() << endl; + Info<< wordRe(s2).info() << endl; + Info<< wordRe(s3, true).info() << endl; + + wre = "this .* file"; + Info<< wre.info() << endl; + wre = s1; + Info<< wre.info() << endl; + wre.uncompile(); + Info<< wre.info() << " uncompiled" << endl; + + + wre = "something"; + Info<< wre.info() << " before" << endl; + wre.uncompile(); + Info<< wre.info() << " uncompiled" << endl; + wre.compile(true); + Info<< wre.info() << " after auto-detect" << endl; + + wre = "something .* value"; + Info<< wre.info() << " before" << endl; + wre.uncompile(); + Info<< wre.info() << " uncompiled" << endl; + wre.compile(true); + Info<< wre.info() << " after auto-detect" << endl; + wre.uncompile(); + Info<< wre.info() << " uncompiled" << endl; + wre.recompile(); + Info<< wre.info() << " recompiled" << endl; + + IOobject::writeDivider(Info); + + List > rawList(IFstream("testRegexps")()); + Info<< "input list:" << rawList << endl; + IOobject::writeDivider(Info); + Info<< endl; + + forAll(rawList, elemI) + { + const wordRe& wre = rawList[elemI].first(); + const string& str = rawList[elemI].second(); + + Info<< wre.info() + << " equals:" << (wre == str) + << "(" << wre.match(str, true) << ")" + << " match:" << wre.match(str) + << " str=" << str + << endl; + } + + Info<< endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OSspecific/Unix/regExp.C b/src/OSspecific/Unix/regExp.C index 447fd0a4ae..b2a0ddd5a7 100644 --- a/src/OSspecific/Unix/regExp.C +++ b/src/OSspecific/Unix/regExp.C @@ -25,6 +25,7 @@ License \*---------------------------------------------------------------------------*/ #include + #include "regExp.H" #include "label.H" #include "string.H" @@ -34,38 +35,27 @@ License // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // -void Foam::regExp::compile(const char* pat) const +void Foam::regExp::compile(const char* pattern) const { clear(); - // avoid NULL and zero-length patterns - if (pat && *pat) + // avoid NULL pointer and zero-length patterns + if (pattern && *pattern) { preg_ = new regex_t; - if (regcomp(preg_, pat, REG_EXTENDED) != 0) + if (regcomp(preg_, pattern, REG_EXTENDED) != 0) { FatalErrorIn ( "regExp::compile(const char*)" - ) << "Failed to compile regular expression '" << pat << "'" + ) << "Failed to compile regular expression '" << pattern << "'" << exit(FatalError); } } } -void Foam::regExp::clear() const -{ - if (preg_) - { - regfree(preg_); - delete preg_; - preg_ = 0; - } -} - - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::regExp::regExp() @@ -74,19 +64,19 @@ Foam::regExp::regExp() {} -Foam::regExp::regExp(const string& pat) +Foam::regExp::regExp(const char* pattern) : preg_(0) { - compile(pat.c_str()); + compile(pattern); } -Foam::regExp::regExp(const char* pat) +Foam::regExp::regExp(const std::string& pattern) : preg_(0) { - compile(pat); + compile(pattern.c_str()); } @@ -100,17 +90,22 @@ Foam::regExp::~regExp() // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // -int Foam::regExp::ngroups() const +bool Foam::regExp::clear() const { - return preg_ ? preg_->re_nsub : 0; + if (preg_) + { + regfree(preg_); + delete preg_; + preg_ = 0; + + return true; + } + + return false; } -bool Foam::regExp::match -( - const string& str, - bool partialMatch -) const +bool Foam::regExp::match(const std::string& str, bool partial) const { if (preg_ && str.size()) { @@ -124,7 +119,7 @@ bool Foam::regExp::match regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0 && ( - partialMatch + partial || (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size())) ) ) @@ -137,12 +132,7 @@ bool Foam::regExp::match } -bool Foam::regExp::match -( - const string& str, - List& groups, - bool partialMatch -) const +bool Foam::regExp::match(const string& str, List& groups) const { if (preg_ && str.size()) { @@ -155,11 +145,7 @@ bool Foam::regExp::match if ( regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0 - && - ( - partialMatch - || (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size())) - ) + && (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size())) ) { groups.setSize(ngroups()); @@ -193,16 +179,16 @@ bool Foam::regExp::match // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * // -void Foam::regExp::operator=(const string& pat) -{ - compile(pat.c_str()); -} - - void Foam::regExp::operator=(const char* pat) { compile(pat); } +void Foam::regExp::operator=(const std::string& pat) +{ + compile(pat.c_str()); +} + + // ************************************************************************* // diff --git a/src/OSspecific/Unix/regExp.H b/src/OSspecific/Unix/regExp.H index 2f825e4e1e..553decd7fd 100644 --- a/src/OSspecific/Unix/regExp.H +++ b/src/OSspecific/Unix/regExp.H @@ -44,6 +44,7 @@ SourceFiles #define regExp_H #include +#include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -67,10 +68,7 @@ class regExp // Private member functions - //- release allocated space - void clear() const; - - //- compile into a regular expression + //- Compile into a regular expression void compile(const char*) const; //- Disallow default bitwise copy construct @@ -81,48 +79,72 @@ class regExp public: + //- Is character a regular expression meta-character? + // any character: '.' \n + // quantifiers: '*', '+', '?' \n + // grouping: '(', '|', ')' \n + // range: '[', ']' \n + // + // Don't bother checking for '{digit}' bounds + inline static bool meta(char c) + { + return + ( + (c == '.') // any character + || (c == '*' || c == '+' || c == '?') // quantifiers + || (c == '(' || c == ')' || c == '|') // grouping/branching + || (c == '[' || c == ']') // range + ); + } + + // Constructors //- Construct null regExp(); - //- Construct from string - regExp(const string&); - //- Construct from character array regExp(const char*); + //- Construct from std::string (or string) + regExp(const std::string&); + // Destructor ~regExp(); + // Member functions + //- Is the precompiled expression set? + inline bool exists() const + { + return preg_ ? true : false; + } + //- Return the number of (groups) - int ngroups() const; + inline int ngroups() const + { + return preg_ ? preg_->re_nsub : 0; + } + + //- Release precompiled expression. + // Returns true if precompiled expression existed before clear + bool clear() const; //- Return true if it matches, partial matches are optional - bool match - ( - const string&, - bool partialMatch=false - ) const; + bool match(const std::string&, bool partial=false) const; + + //- Return true if it matches and sets the sub-groups matched + bool match(const string&, List& groups) const; - //- Return true if it matches and sets the sub-groups matched, - // partial matches are optional - bool match - ( - const string&, - List& groups, - bool partialMatch=false - ) const; // Member Operators - //- Assign from a string - void operator=(const string&); + //- Assign from a string and compile regular expression + void operator=(const std::string&); - //- Assign from a character array + //- Assign from a character array and compile regular expression void operator=(const char*); }; diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 6f0ee79faa..a45a5fa411 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -40,6 +40,7 @@ $(strings)/word/wordIO.C $(strings)/fileName/fileName.C $(strings)/fileName/fileNameIO.C $(strings)/keyType/keyTypeIO.C +$(strings)/wordRe/wordReIO.C primitives/random/Random.C diff --git a/src/OpenFOAM/primitives/Lists/wordReList.H b/src/OpenFOAM/primitives/Lists/wordReList.H new file mode 100644 index 0000000000..7b05dd4063 --- /dev/null +++ b/src/OpenFOAM/primitives/Lists/wordReList.H @@ -0,0 +1,50 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Typedef + Foam::wordReList + +Description + List of wordRe (word or regular expression) + +\*---------------------------------------------------------------------------*/ + +#ifndef wordReList_H +#define wordReList_H + +#include "wordRe.H" +#include "List.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + typedef List wordReList; +} + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.C b/src/OpenFOAM/primitives/strings/fileName/fileName.C index fdbaccfb2b..2f669a839a 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.C @@ -37,14 +37,11 @@ const Foam::fileName Foam::fileName::null; // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // -Foam::fileName::fileName(const wordList& wrdList) +Foam::fileName::fileName(const wordList& lst) { - if (wrdList.size() != 0) + forAll(lst, elemI) { - forAll(wrdList, i) - { - operator=((*this)/wrdList[i]); - } + operator=((*this)/lst[elemI]); } } @@ -77,7 +74,7 @@ Foam::word Foam::fileName::name() const } -//- Return directory path name (part before last /) +// Return directory path name (part before last /) // // behaviour compared to /usr/bin/dirname: // input path() dirname @@ -205,35 +202,35 @@ Foam::fileName::Type Foam::fileName::type() const // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // -void Foam::fileName::operator=(const fileName& q) +void Foam::fileName::operator=(const fileName& str) { - string::operator=(q); + string::operator=(str); } -void Foam::fileName::operator=(const word& q) +void Foam::fileName::operator=(const word& str) { - string::operator=(q); + string::operator=(str); } -void Foam::fileName::operator=(const string& q) +void Foam::fileName::operator=(const string& str) { - string::operator=(q); + string::operator=(str); stripInvalid(); } -void Foam::fileName::operator=(const std::string& q) +void Foam::fileName::operator=(const std::string& str) { - string::operator=(q); + string::operator=(str); stripInvalid(); } -void Foam::fileName::operator=(const char* q) +void Foam::fileName::operator=(const char* str) { - string::operator=(q); + string::operator=(str); stripInvalid(); } diff --git a/src/OpenFOAM/primitives/strings/fileName/fileName.H b/src/OpenFOAM/primitives/strings/fileName/fileName.H index e24c21396c..24dd213ef5 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileName.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileName.H @@ -64,7 +64,7 @@ Ostream& operator<<(Ostream&, const fileName&); /*---------------------------------------------------------------------------*\ - Class fileName Declaration + Class fileName Declaration \*---------------------------------------------------------------------------*/ class fileName @@ -73,7 +73,7 @@ class fileName { // Private member functions - //- Strip invalid characters from this word + //- Strip invalid characters inline void stripInvalid(); @@ -102,16 +102,16 @@ public: inline fileName(); //- Construct as copy - inline fileName(const fileName& fn); + inline fileName(const fileName&); //- Construct as copy of word - inline fileName(const word& w); + inline fileName(const word&); //- Construct as copy of string - inline fileName(const string& s); + inline fileName(const string&); //- Construct as copy of std::string - inline fileName(const std::string& s); + inline fileName(const std::string&); //- Construct as copy of character array inline fileName(const char*); @@ -125,7 +125,7 @@ public: // Member functions - //- Is this character valid for a fileName + //- Is this character valid for a fileName? inline static bool valid(char); diff --git a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H index 2b81e67859..f2fb48345b 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileNameI.H +++ b/src/OpenFOAM/primitives/strings/fileName/fileNameI.H @@ -74,17 +74,17 @@ inline Foam::fileName::fileName(const string& str) } -inline Foam::fileName::fileName(const std::string& stdStr) +inline Foam::fileName::fileName(const std::string& str) : - string(stdStr) + string(str) { stripInvalid(); } -inline Foam::fileName::fileName(const char* chars) +inline Foam::fileName::fileName(const char* str) : - string(chars) + string(str) { stripInvalid(); } @@ -94,7 +94,7 @@ inline Foam::fileName::fileName(const char* chars) inline bool Foam::fileName::valid(char c) { - return(!isspace(c) && c != '"'); + return (!isspace(c) && c != '"'); } diff --git a/src/OpenFOAM/primitives/strings/fileName/fileNameIO.C b/src/OpenFOAM/primitives/strings/fileName/fileNameIO.C index 41d08181f2..bed17b9fd1 100644 --- a/src/OpenFOAM/primitives/strings/fileName/fileNameIO.C +++ b/src/OpenFOAM/primitives/strings/fileName/fileNameIO.C @@ -53,9 +53,9 @@ Foam::Istream& Foam::operator>>(Istream& is, fileName& fn) } -Foam::Ostream& Foam::operator<<(Ostream& os, const fileName& s) +Foam::Ostream& Foam::operator<<(Ostream& os, const fileName& fn) { - os.write(s); + os.write(fn); os.check("Ostream& operator<<(Ostream&, const fileName&)"); return os; } diff --git a/src/OpenFOAM/primitives/strings/string/string.C b/src/OpenFOAM/primitives/strings/string/string.C index 794cb91316..f9a863e10f 100644 --- a/src/OpenFOAM/primitives/strings/string/string.C +++ b/src/OpenFOAM/primitives/strings/string/string.C @@ -39,14 +39,9 @@ const Foam::string Foam::string::null; // Count and return the number of a given character in the string Foam::string::size_type Foam::string::count(const char c) const { - register size_type cCount=0; + register size_type cCount = 0; - for - ( - const_iterator iter = begin(); - iter != end(); - ++iter - ) + for (const_iterator iter = begin(); iter != end(); ++iter) { if (*iter == c) { @@ -269,9 +264,9 @@ bool Foam::string::removeRepeated(const char character) // Return string with repeated characters removed Foam::string Foam::string::removeRepeated(const char character) const { - string s(*this); - s.removeRepeated(character); - return s; + string str(*this); + str.removeRepeated(character); + return str; } @@ -294,9 +289,9 @@ bool Foam::string::removeTrailing(const char character) // Return string with trailing character removed Foam::string Foam::string::removeTrailing(const char character) const { - string s(*this); - s.removeTrailing(character); - return s; + string str(*this); + str.removeTrailing(character); + return str; } diff --git a/src/OpenFOAM/primitives/strings/string/string.H b/src/OpenFOAM/primitives/strings/string/string.H index 193cc0f2a7..6e40e83871 100644 --- a/src/OpenFOAM/primitives/strings/string/string.H +++ b/src/OpenFOAM/primitives/strings/string/string.H @@ -88,18 +88,10 @@ public: //- Hashing function class class hash { - public: - inline hash(); - - inline size_type operator()(const string& key) const; - - inline size_type operator() - ( - const string& key, - const size_type tableSize - ) const; + inline size_type operator()(const string&) const; + inline size_type operator()(const string&, const size_type) const; }; @@ -126,70 +118,80 @@ public: // Member Functions - // Access + // Access - //- Count and return the number of a given character in the string - size_type count(const char) const; + //- Count and return the number of a given character in the string + size_type count(const char) const; - //- Is this string type valid? - template - static inline bool valid(const string&); + //- Is this string type valid? + template + static inline bool valid(const string&); + + //- Does this string have particular meta-characters? + // The meta characters can be optionally quoted. + template + static inline bool meta(const string&, const char quote='\\'); - // Edit + // Edit - //- Strip invalid characters from the given string - template - static inline bool stripInvalid(string&); + //- Strip invalid characters from the given string + template + static inline bool stripInvalid(string&); - //- Return a valid String from the given string - template - static inline String validate(const string&); + //- Return a valid String from the given string + template + static inline String validate(const string&); - //- Replace first occurence of sub-string oldStr with newStr - // starting at start - string& replace - ( - const string& oldStr, - const string& newStr, - size_type start = 0 - ); + //- Return a String with quoted meta-characters from the given string + template + static inline string quotemeta(const string&, const char quote='\\'); - //- Replace all occurences of sub-string oldStr with newStr - // starting at start - string& replaceAll - ( - const string& oldStr, - const string& newStr, - size_type start = 0 - ); - //- Expand initial tildes and all occurences of environment variables - // Expansion includes: - // -# environment variables - // - "$VAR", "${VAR}" - // -# current directory - // - leading "./" : the current directory - // -# tilde expansion - // - leading "~/" : home directory - // - leading "~user" : home directory for specified user - // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory - // - // @sa - // Foam::findEtcFile - string& expand(); + //- Replace first occurence of sub-string oldStr with newStr + // starting at start + string& replace + ( + const string& oldStr, + const string& newStr, + size_type start = 0 + ); - //- Remove repeated characters returning true if string changed - bool removeRepeated(const char character); + //- Replace all occurences of sub-string oldStr with newStr + // starting at start + string& replaceAll + ( + const string& oldStr, + const string& newStr, + size_type start = 0 + ); - //- Return string with repeated characters removed - string removeRepeated(const char character) const; + //- Expand initial tildes and all occurences of environment variables + // Expansion includes: + // -# environment variables + // - "$VAR", "${VAR}" + // -# current directory + // - leading "./" : the current directory + // -# tilde expansion + // - leading "~/" : home directory + // - leading "~user" : home directory for specified user + // - leading "~OpenFOAM" : site/user OpenFOAM configuration directory + // + // @sa + // Foam::findEtcFile + string& expand(); - //- Remove trailing character returning true if string changed - bool removeTrailing(const char character); + //- Remove repeated characters returning true if string changed + bool removeRepeated(const char); - //- Return string with trailing character removed - string removeTrailing(const char character) const; + //- Return string with repeated characters removed + string removeRepeated(const char) const; + + //- Remove trailing character returning true if string changed + bool removeTrailing(const char); + + //- Return string with trailing character removed + string removeTrailing(const char) const; // Member Operators diff --git a/src/OpenFOAM/primitives/strings/string/stringI.H b/src/OpenFOAM/primitives/strings/string/stringI.H index e391810720..38f8c80aa0 100644 --- a/src/OpenFOAM/primitives/strings/string/stringI.H +++ b/src/OpenFOAM/primitives/strings/string/stringI.H @@ -62,9 +62,9 @@ inline Foam::string::string(const char c) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -inline bool Foam::string::valid(const string& s) +inline bool Foam::string::valid(const string& str) { - for (const_iterator iter = s.begin(); iter != s.end(); iter++) + for (const_iterator iter = str.begin(); iter != str.end(); iter++) { if (!String::valid(*iter)) { @@ -76,17 +76,17 @@ inline bool Foam::string::valid(const string& s) template -inline bool Foam::string::stripInvalid(string& s) +inline bool Foam::string::stripInvalid(string& str) { - if (!valid(s)) + if (!valid(str)) { - register size_type nValid=0; - iterator iter2 = s.begin(); + register size_type nValid = 0; + iterator iter2 = str.begin(); for ( const_iterator iter1 = iter2; - iter1 != const_cast(s).end(); + iter1 != const_cast(str).end(); iter1++ ) { @@ -100,7 +100,7 @@ inline bool Foam::string::stripInvalid(string& s) } } - s.resize(nValid); + str.resize(nValid); return true; } @@ -109,6 +109,66 @@ inline bool Foam::string::stripInvalid(string& s) } +template +inline bool Foam::string::meta(const string& str, const char quote) +{ + int escaped = 0; + for (const_iterator iter = str.begin(); iter != str.end(); iter++) + { + if (quote && *iter == quote) + { + escaped ^= 1; // toggle state + } + else if (escaped) + { + escaped = false; + } + else if (String::meta(*iter)) + { + return true; + } + } + return false; +} + + +template +inline Foam::string +Foam::string::quotemeta(const string& str, const char quote) +{ + if (!quote) + { + return str; + } + + string sQuoted; + sQuoted.reserve(2*str.length()); + + int escaped = 0; + for (const_iterator iter = str.begin(); iter != str.end(); iter++) + { + if (*iter == quote) + { + escaped ^= 1; // toggle state + } + else if (escaped) + { + escaped = 0; + } + else if (String::meta(*iter)) + { + sQuoted += quote; + } + + sQuoted += *iter; + } + + sQuoted.resize(sQuoted.length()); + + return sQuoted; +} + + template inline String Foam::string::validate(const string& str) { diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordRe.H b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H new file mode 100644 index 0000000000..7433ce8c84 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/wordRe/wordRe.H @@ -0,0 +1,206 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Class + Foam::wordRe + +Description + A wordRe is a word, but can also have a regular expression for matching + words. + +Note + If the string contents are changed - eg, by the operator+=() or by + string::replace(), etc - it will be necessary to use compile() or + recompile() to sychronize the regular expression. + + THIS IS STILL A DRAFT -- NOT YET RELEASED FOR GENERAL USE + +SourceFiles + wordRe.C + wordReIO.C + +\*---------------------------------------------------------------------------*/ + +#ifndef wordRe_H +#define wordRe_H + +#include "word.H" +#include "regExp.H" +#include "InfoProxy.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +// Forward declaration of friend functions and operators +class wordRe; + +class Istream; +class Ostream; + +Istream& operator>>(Istream&, wordRe&); +Ostream& operator<<(Ostream&, const wordRe&); + + +/*---------------------------------------------------------------------------*\ + Class wordRe Declaration +\*---------------------------------------------------------------------------*/ + +class wordRe +: + public word +{ + // Private member data + + //- The regular expression + mutable regExp re_; + +public: + + //- Is this a meta character? + static inline bool meta(char); + + //- Is this character valid for a wordRe + inline static bool valid(char); + + //- Test string for regular expression meta characters + static inline bool isPattern(const string&); + + // Constructors + + //- Construct null + inline wordRe(); + + //- Construct as copy + inline wordRe(const wordRe&); + + //- Construct as copy of word + inline wordRe(const word&); + + //- Construct as copy of character array + // Treat as regular expression specified explicitly. + inline wordRe(const char*, const bool asPattern=false); + + //- Construct as copy of string. + // Treat as regular expression specified explicitly. + inline wordRe(const string&, const bool asPattern); + + //- Construct as copy of string. + // Auto-test for regular expression + inline wordRe(const string&); + + //- Construct as copy of std::string + // Treat as regular expression specified explicitly. + inline wordRe(const std::string&, const bool asPattern); + + //- Construct as copy of std::string + // Auto-test for regular expression + inline wordRe(const std::string&); + + //- Construct from Istream + wordRe(Istream&); + + // Destructor + + ~wordRe(); + + + // Member functions + + //- Should be treated as a match rather than a literal string? + inline bool isPattern() const; + + //- Create and compile the regular expression + // Optionally detect if it appears to be a regular expression + inline bool compile(const bool detect=false) const; + + //- Recompile an existing regular expression + inline bool recompile() const; + + //- Frees precompiled regular expression and makes is a literal string. + // Optionally strips invalid word characters + inline void uncompile(const bool doStripInvalid=false) const; + + //- Clear string and precompiled regular expression + inline void clear(); + + //- Smart match as regular expression or as a string + // Optionally specify a literal match only + inline bool match(const string&, bool literalMatch=false) const; + + //- Return a string with quoted meta-characters + inline string quotemeta() const; + + //- Return info proxy. + InfoProxy info() const + { + return *this; + } + + + // Member operators + + // Assignment + + //- Assign copy + inline void operator=(const wordRe&); + + //- Copy word, never a regular expression + inline void operator=(const word&); + + //- Copy string, auto-test for regular expression + inline void operator=(const string&); + + //- Copy string, auto-test for regular expression + inline void operator=(const std::string&); + + //- Copy string, auto-test for regular expression + inline void operator=(const char*); + + + // IOstream operators + + friend Istream& operator>>(Istream&, wordRe&); + friend Ostream& operator<<(Ostream&, const wordRe&); +}; + + +template<> +Ostream& operator<<(Ostream&, const InfoProxy&); + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#include "wordReI.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReI.H b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H new file mode 100644 index 0000000000..79e2925c91 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/wordRe/wordReI.H @@ -0,0 +1,263 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * // + +inline bool Foam::wordRe::meta(char c) +{ + return regExp::meta(c); +} + + +inline bool Foam::wordRe::valid(char c) +{ + return + ( + !isspace(c) + && c != '"' + && c != '/' + ); +} + +inline bool Foam::wordRe::isPattern(const string& str) +{ + return string::meta(str); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +inline Foam::wordRe::wordRe() +: + word(), + re_() +{} + + +inline Foam::wordRe::wordRe(const wordRe& str) +: + word(str), + re_() +{ + if (str.isPattern()) + { + compile(); + } +} + + +inline Foam::wordRe::wordRe(const word& str) +: + word(str), + re_() +{} + + +inline Foam::wordRe::wordRe(const char* str, const bool asPattern) +: + word(str, false), + re_() +{ + if (asPattern) + { + compile(); + } +} + + +inline Foam::wordRe::wordRe(const string& str, const bool asPattern) +: + word(str, false), + re_() +{ + if (asPattern) + { + compile(); + } +} + + +inline Foam::wordRe::wordRe(const string& str) +: + word(str, false), + re_() +{ + compile(true); // auto-detect regex +} + + +inline Foam::wordRe::wordRe(const std::string& str, const bool asPattern) +: + word(str, false), + re_() +{ + if (asPattern) + { + compile(); + } +} + + +inline Foam::wordRe::wordRe(const std::string& str) +: + word(str, false), + re_() +{ + compile(true); // auto-detect regex +} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::wordRe::~wordRe() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +inline bool Foam::wordRe::isPattern() const +{ + return re_.exists(); +} + + +inline bool Foam::wordRe::compile(const bool detect) const +{ + // appears to be a plain word and not a regex + if (detect && string::valid(*this) && !string::meta(*this)) + { + re_.clear(); + } + else + { + re_ = *this; + } + + return re_.exists(); +} + + +inline bool Foam::wordRe::recompile() const +{ + if (re_.exists()) + { + re_ = *this; + } + + return re_.exists(); +} + + +inline void Foam::wordRe::uncompile(const bool doStripInvalid) const +{ + if (re_.clear()) + { + // skip stripping unless debug is active to avoid costly operations + if (word::debug && doStripInvalid) + { + string::stripInvalid + ( + const_cast(static_cast(*this)) + ); + } + } +} + + +inline void Foam::wordRe::clear() +{ + word::clear(); + re_.clear(); +} + + +inline bool Foam::wordRe::match(const string& str, bool literalMatch) const +{ + if (literalMatch || !re_.exists()) + { + // check as string + return (*this == str); + } + else + { + // check as regex + return re_.match(str); + } +} + + +inline Foam::string Foam::wordRe::quotemeta() const +{ + return string::quotemeta(*this); +} + + +// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // + +inline void Foam::wordRe::operator=(const wordRe& str) +{ + string::operator=(str); + + if (str.isPattern()) + { + compile(); + } + else + { + re_.clear(); + } +} + + +inline void Foam::wordRe::operator=(const word& str) +{ + word::operator=(str); + re_.clear(); +} + + +inline void Foam::wordRe::operator=(const string& str) +{ + string::operator=(str); + compile(true); // auto-detect regex +} + + +inline void Foam::wordRe::operator=(const std::string& str) +{ + string::operator=(str); + compile(true); // auto-detect regex +} + + +inline void Foam::wordRe::operator=(const char* str) +{ + string::operator=(str); + compile(true); // auto-detect regex +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/strings/wordRe/wordReIO.C b/src/OpenFOAM/primitives/strings/wordRe/wordReIO.C new file mode 100644 index 0000000000..9e7c1f2b82 --- /dev/null +++ b/src/OpenFOAM/primitives/strings/wordRe/wordReIO.C @@ -0,0 +1,113 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd. + \\/ M anipulation | +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM; if not, write to the Free Software Foundation, + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +\*---------------------------------------------------------------------------*/ + +#include "wordRe.H" +#include "IOstreams.H" +#include "InfoProxy.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +Foam::wordRe::wordRe(Istream& is) +: + word(), + re_(NULL) +{ + is >> *this; +} + + +Foam::Istream& Foam::operator>>(Istream& is, wordRe& w) +{ + token t(is); + + if (!t.good()) + { + is.setBad(); + return is; + } + + if (t.isWord()) + { + w = t.wordToken(); + } + else if (t.isString()) + { + // Auto-tests for regular expression + w = t.stringToken(); + } + else + { + is.setBad(); + FatalIOErrorIn("operator>>(Istream&, wordRe&)", is) + << "wrong token type - expected word or string found " + << t.info() + << exit(FatalIOError); + + return is; + } + + // Check state of IOstream + is.check("Istream& operator>>(Istream&, wordRe&)"); + + return is; +} + + +Foam::Ostream& Foam::operator<<(Ostream& os, const wordRe& w) +{ + if (w.isPattern()) + { + os.write(static_cast(w)); + } + else + { + os.write(static_cast(w)); + } + os.check("Ostream& operator<<(Ostream&, const wordRe&)"); + return os; +} + + +template<> +Foam::Ostream& Foam::operator<<(Ostream& os, const InfoProxy& ip) +{ + const wordRe& wre = ip.t_; + + if (wre.isPattern()) + { + os << "wordRe(regex) " << wre; + } + else + { + os << "wordRe(plain) '" << wre << "'"; + } + os.flush(); + + return os; +} + + +// ************************************************************************* //