ENH: add keyType::null and wordRe::null, add match method to string classes

- provides a more uniform interface to string list operations etc

STYLE: more complete docs for keyType
This commit is contained in:
Mark Olesen
2010-08-04 10:35:16 +02:00
parent 692aa4abba
commit b89a3dc2c0
11 changed files with 98 additions and 32 deletions

View File

@ -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);

View File

@ -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

View File

@ -93,6 +93,8 @@ public:
static const char* const typeName;
static int debug;
//- An empty fileName
static const fileName null;

View File

@ -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;
}
Foam::Istream& Foam::operator>>(Istream& is, keyType& w)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::keyType::match
(
const std::string& str,
bool literalMatch
) const
{
if (literalMatch || !isPattern_)
{
// check as string
return (str == *this);
}
else
{
// check as regex
return regExp(*this).match(str);
}
}
// * * * * * * * * * * * * * * * 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;
}

View File

@ -32,7 +32,6 @@ Description
SourceFiles
keyType.C
keyTypeIO.C
\*---------------------------------------------------------------------------*/
@ -59,7 +58,7 @@ 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*);

View File

@ -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,

View File

@ -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;

View File

@ -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 * * * * * * * * * * * * * //

View File

@ -73,6 +73,8 @@ public:
static const char* const typeName;
static int debug;
//- An empty word
static const word null;

View File

@ -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);

View File

@ -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