ENH: handle keyType type (literal/regex) as enum instead of bool

- makes its use somewhat clearer and allows more future options
This commit is contained in:
Mark Olesen 2019-08-20 13:48:05 +02:00 committed by Andrew Heather
parent dade6957c8
commit b5342c166c
10 changed files with 105 additions and 67 deletions

View File

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

View File

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

View File

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

View File

@ -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<keyType>(keyName), true);
key = keyType
(
string::validate<keyType>(keyName),
keyType::REGEX
);
}
else
{

View File

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

View File

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

View File

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

View File

@ -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<word&>(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<word>(*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<word>(*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<word&>(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;
}

View File

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

View File

@ -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<word>(*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
}