STYLE: removed mutable variable/methods from regExp

- tends to obscure what is going on and isn't needed either.

STYLE: adjust documentation. Accept std::string as parameter in more places.
This commit is contained in:
Mark Olesen 2017-03-10 12:42:59 +01:00
parent aa6b835104
commit f8e0231672
8 changed files with 191 additions and 203 deletions

View File

@ -32,11 +32,11 @@ License
template<class StringType>
bool Foam::regExp::matchGrouping
(
const std::string& str,
const std::string& text,
List<StringType>& groups
) const
{
if (preg_ && str.size())
if (preg_ && !text.empty())
{
size_t nmatch = ngroups() + 1;
regmatch_t pmatch[nmatch];
@ -46,8 +46,8 @@ bool Foam::regExp::matchGrouping
// pmatch[1..] are the (...) sub-groups
if
(
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
regexec(preg_, text.c_str(), nmatch, pmatch, 0) == 0
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(text.size()))
)
{
groups.setSize(ngroups());
@ -57,7 +57,7 @@ bool Foam::regExp::matchGrouping
{
if (pmatch[matchI].rm_so != -1 && pmatch[matchI].rm_eo != -1)
{
groups[groupI] = str.substr
groups[groupI] = text.substr
(
pmatch[matchI].rm_so,
pmatch[matchI].rm_eo - pmatch[matchI].rm_so
@ -83,21 +83,21 @@ bool Foam::regExp::matchGrouping
Foam::regExp::regExp()
:
preg_(0)
preg_(nullptr)
{}
Foam::regExp::regExp(const char* pattern, const bool ignoreCase)
Foam::regExp::regExp(const char* pattern, bool ignoreCase)
:
preg_(0)
preg_(nullptr)
{
set(pattern, ignoreCase);
}
Foam::regExp::regExp(const std::string& pattern, const bool ignoreCase)
Foam::regExp::regExp(const std::string& pattern, bool ignoreCase)
:
preg_(0)
preg_(nullptr)
{
set(pattern.c_str(), ignoreCase);
}
@ -113,7 +113,7 @@ Foam::regExp::~regExp()
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
void Foam::regExp::set(const char* pattern, bool ignoreCase)
{
clear();
@ -160,19 +160,19 @@ void Foam::regExp::set(const char* pattern, const bool ignoreCase) const
}
void Foam::regExp::set(const std::string& pattern, const bool ignoreCase) const
void Foam::regExp::set(const std::string& pattern, bool ignoreCase)
{
return set(pattern.c_str(), ignoreCase);
}
bool Foam::regExp::clear() const
bool Foam::regExp::clear()
{
if (preg_)
{
regfree(preg_);
delete preg_;
preg_ = 0;
preg_ = nullptr;
return true;
}
@ -181,14 +181,14 @@ bool Foam::regExp::clear() const
}
std::string::size_type Foam::regExp::find(const std::string& str) const
std::string::size_type Foam::regExp::find(const std::string& text) const
{
if (preg_ && str.size())
if (preg_ && !text.empty())
{
size_t nmatch = 1;
regmatch_t pmatch[1];
if (regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0)
if (regexec(preg_, text.c_str(), nmatch, pmatch, 0) == 0)
{
return pmatch[0].rm_so;
}
@ -198,9 +198,9 @@ std::string::size_type Foam::regExp::find(const std::string& str) const
}
bool Foam::regExp::match(const std::string& str) const
bool Foam::regExp::match(const std::string& text) const
{
if (preg_ && str.size())
if (preg_ && !text.empty())
{
size_t nmatch = 1;
regmatch_t pmatch[1];
@ -209,8 +209,8 @@ bool Foam::regExp::match(const std::string& str) const
// pmatch[0] is the entire match
if
(
regexec(preg_, str.c_str(), nmatch, pmatch, 0) == 0
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(str.size()))
regexec(preg_, text.c_str(), nmatch, pmatch, 0) == 0
&& (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == label(text.size()))
)
{
return true;
@ -223,35 +223,35 @@ bool Foam::regExp::match(const std::string& str) const
bool Foam::regExp::match
(
const std::string& str,
const std::string& text,
List<std::string>& groups
) const
{
return matchGrouping(str, groups);
return matchGrouping(text, groups);
}
bool Foam::regExp::match
(
const std::string& str,
const std::string& text,
List<Foam::string>& groups
) const
{
return matchGrouping(str, groups);
return matchGrouping(text, groups);
}
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
void Foam::regExp::operator=(const char* pat)
void Foam::regExp::operator=(const char* pattern)
{
set(pat);
set(pattern);
}
void Foam::regExp::operator=(const std::string& pat)
void Foam::regExp::operator=(const std::string& pattern)
{
set(pat);
set(pattern);
}

View File

@ -64,16 +64,16 @@ class regExp
// Private data
//- Precompiled regular expression
mutable regex_t* preg_;
regex_t* preg_;
// Private Member Functions
//- Disallow default bitwise copy construct
regExp(const regExp&);
regExp(const regExp&) = delete;
//- Disallow default bitwise assignment
void operator=(const regExp&);
void operator=(const regExp&) = delete;
//- Return true if it matches and sets the sub-groups matched.
// Templated to support both std::string and Foam::string
@ -96,7 +96,7 @@ public:
// range: '[', ']' \n
//
// Don't bother checking for '{digit}' bounds
inline static bool meta(char c)
inline static bool meta(const char c)
{
return
(
@ -113,11 +113,11 @@ public:
//- Construct null
regExp();
//- Construct from character array, optionally ignoring case
regExp(const char*, const bool ignoreCase=false);
//- Construct from character array, optionally ignore case
regExp(const char* pattern, bool ignoreCase=false);
//- Construct from std::string (or string), optionally ignoring case
regExp(const std::string&, const bool ignoreCase=false);
//- Construct from string, optionally ignore case
regExp(const std::string& pattern, bool ignoreCase=false);
//- Destructor
@ -126,76 +126,73 @@ public:
// Member functions
// Access
// Access
//- Return true if a precompiled expression does not exist
inline bool empty() const
{
return !preg_;
}
//- Return true if a precompiled expression does not exist
inline bool empty() const
{
return !preg_;
}
//- Does a precompiled expression exist?
inline bool exists() const
{
return preg_ ? true : false;
}
//- Does a precompiled expression exist?
inline bool exists() const
{
return preg_ ? true : false;
}
//- Return the number of (groups)
inline int ngroups() const
{
return preg_ ? preg_->re_nsub : 0;
}
//- The number of capture groups for a non-empty expression
inline unsigned ngroups() const
{
return preg_ ? preg_->re_nsub : 0;
}
// Editing
// Editing
//- Compile pattern into a regular expression,
// optionally ignoring case
void set(const char*, const bool ignoreCase=false) const;
//- Compile pattern into a regular expression, optionally ignore case
void set(const char* pattern, bool ignoreCase=false);
//- Compile pattern into a regular expression,
// optionally ignoring case
void set(const std::string&, const bool ignoreCase=false) const;
//- Compile pattern into a regular expression, optionally ignore case
void set(const std::string& pattern, bool ignoreCase=false);
//- Release precompiled expression.
// Returns true if precompiled expression existed before clear
bool clear() const;
//- Clear expression, return true if expression had existed.
bool clear();
// Searching
// Matching/Searching
//- Find position within string.
// Returns the index where it begins or string::npos if not found
std::string::size_type find(const std::string& str) const;
//- Find position within string.
// Returns the index where it begins or string::npos if not found
std::string::size_type find(const std::string& text) const;
//- Return true if it matches the entire string
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string&) const;
//- Return true if it matches the entire string
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string& text) const;
//- Return true if it matches and sets the sub-groups matched
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string&, List<std::string>& groups) const;
//- Return true if it matches and sets the sub-groups matched
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string& text, List<std::string>& groups) const;
//- Return true if it matches and sets the sub-groups matched
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string&, List<string>& groups) const;
//- Return true if it matches and sets the sub-groups matched
// The begin-of-line (^) and end-of-line ($) anchors are implicit
bool match(const std::string& text, List<string>& groups) const;
//- Return true if the regex was found within string
bool search(const std::string& str) const
{
return std::string::npos != find(str);
}
//- Return true if the regex was found within string
bool search(const std::string& text) const
{
return std::string::npos != find(text);
}
// Member Operators
//- Assign and compile pattern from a character array
// Always case sensitive
void operator=(const char*);
void operator=(const char* pattern);
//- Assign and compile pattern from string
// Always case sensitive
void operator=(const std::string&);
void operator=(const std::string& pattern);
};

View File

@ -48,21 +48,17 @@ Foam::keyType::keyType(Istream& is)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::keyType::match
(
const std::string& str,
bool literalMatch
) const
bool Foam::keyType::match(const std::string& text, bool literal) const
{
if (literalMatch || !isPattern_)
if (literal || !isPattern_)
{
// check as string
return (str == *this);
return (text == *this);
}
else
{
// check as regex
return regExp(*this).match(str);
return regExp(*this).match(text);
}
}

View File

@ -28,7 +28,9 @@ Description
A class for handling keywords in dictionaries.
A keyType is the keyword of a dictionary.
It differs from word in that it accepts patterns (regular expressions).
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.
SourceFiles
keyType.C
@ -49,13 +51,10 @@ namespace Foam
class Istream;
class Ostream;
// Forward declaration of friend functions and operators
class keyType;
Istream& operator>>(Istream&, keyType&);
Ostream& operator<<(Ostream&, const keyType&);
Istream& operator>>(Istream& is, keyType& kw);
Ostream& operator<<(Ostream& os, const keyType& kw);
/*---------------------------------------------------------------------------*\
@ -74,7 +73,7 @@ class keyType
// Private Member Functions
//- Disallow assignments where we cannot determine string/word type
void operator=(const std::string&);
void operator=(const std::string&) = delete;
public:
@ -89,57 +88,58 @@ public:
//- Construct null
inline keyType();
//- Construct as copy
inline keyType(const keyType&);
//- Construct as copy, retaining type (literal or regex)
inline keyType(const keyType& s);
//- Construct as copy of word. Not treated as a regular expression
inline keyType(const word&);
inline keyType(const word& s);
//- Construct as copy of string. Treat as regular expression.
inline keyType(const string&);
inline keyType(const string& s);
//- Construct as copy of character array.
// Not treated as a regular expression
inline keyType(const char*);
inline keyType(const char* s);
//- Construct as copy of std::string with specified treatment
inline keyType(const std::string&, const bool isPattern);
inline keyType(const std::string& s, const bool isPattern);
//- Construct from Istream
// Treat as regular expression if surrounded by quotation marks.
keyType(Istream&);
keyType(Istream& is);
// Member functions
//- Should be treated as a match rather than a literal string
//- Treat as a pattern rather than a literal string?
inline bool isPattern() const;
//- Smart match as regular expression or as a string
//- Smart match as regular expression or as a string.
// Optionally force a literal match only
bool match(const std::string&, bool literalMatch=false) const;
bool match(const std::string& text, bool literal = false) const;
// Member operators
// Assignment
//- Assignment operator
inline void operator=(const keyType&);
//- Assignment operator, retaining type (literal or regex)
inline void operator=(const keyType& s);
//- Assign as word, not as non regular expression
inline void operator=(const word&);
//- Assign as word, not treated as a regular expression.
inline void operator=(const word& s);
//- Assign as regular expression
inline void operator=(const string&);
inline void operator=(const string& s);
//- Assign as word, not as non regular expression
inline void operator=(const char*);
//- Assign as word, not treated as a regular expression.
inline void operator=(const char* s);
// IOstream operators
friend Istream& operator>>(Istream&, keyType&);
friend Ostream& operator<<(Ostream&, const keyType&);
friend Istream& operator>>(Istream& is, keyType& kw);
friend Ostream& operator<<(Ostream& os, const keyType& kw);
};

View File

@ -83,31 +83,28 @@ inline bool Foam::keyType::isPattern() const
inline void Foam::keyType::operator=(const keyType& s)
{
// Bypass checking
string::operator=(s);
string::operator=(s); // Bypass checking
isPattern_ = s.isPattern_;
}
inline void Foam::keyType::operator=(const word& s)
{
word::operator=(s);
string::operator=(s); // Bypass checking
isPattern_ = false;
}
inline void Foam::keyType::operator=(const string& s)
{
// Bypass checking
string::operator=(s);
string::operator=(s); // Bypass checking
isPattern_ = true;
}
inline void Foam::keyType::operator=(const char* s)
{
// Bypass checking
string::operator=(s);
string::operator=(s); // Bypass checking
isPattern_ = false;
}

View File

@ -110,7 +110,6 @@ Foam::Ostream& Foam::wordRe::info(Ostream& os) const
{
os << "wordRe(plain) \"" << *this << '"';
}
os.flush();
return os;
}

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -64,8 +64,8 @@ class wordRe;
class Istream;
class Ostream;
Istream& operator>>(Istream&, wordRe&);
Ostream& operator<<(Ostream&, const wordRe&);
Istream& operator>>(Istream& is, wordRe& w);
Ostream& operator<<(Ostream& os, const wordRe& w);
/*---------------------------------------------------------------------------*\
@ -105,10 +105,10 @@ public:
//- Is this a meta character?
static inline bool meta(char);
inline static bool meta(const char c);
//- Test string for regular expression meta characters
static inline bool isPattern(const string&);
inline static bool isPattern(const std::string& str);
// Constructors
@ -117,119 +117,119 @@ public:
inline wordRe();
//- Construct as copy
inline wordRe(const wordRe&);
inline wordRe(const wordRe& str);
//- Construct from keyType
inline explicit wordRe(const keyType&);
inline explicit wordRe(const keyType& str);
//- Construct from keyType
inline wordRe(const keyType&, const compOption);
//- Construct from keyType, use specified compile option
inline wordRe(const keyType& str, const compOption);
//- Construct as copy of word
inline explicit wordRe(const word&);
inline explicit wordRe(const word& str);
//- Construct as copy of character array
// Optionally specify how it should be treated.
inline explicit wordRe(const char*, const compOption = LITERAL);
inline explicit wordRe(const char* str, const compOption = LITERAL);
//- Construct as copy of string.
// Optionally specify how it should be treated.
inline explicit wordRe(const string&, const compOption = LITERAL);
inline explicit wordRe(const string& str, const compOption = LITERAL);
//- Construct as copy of std::string
// Optionally specify how it should be treated.
inline explicit wordRe(const std::string&, const compOption = LITERAL);
inline explicit wordRe
(
const std::string& str,
const compOption = LITERAL
);
//- Construct from Istream
// Words are treated as literals, strings with an auto-test
wordRe(Istream&);
wordRe(Istream& is);
// Member functions
// Access
// Access
//- Should be treated as a match rather than a literal string?
inline bool isPattern() const;
//- Treat as a pattern rather than a literal string?
inline bool isPattern() const;
// Infrastructure
// Infrastructure
//- Compile the regular expression
inline bool compile() const;
//- Compile the regular expression
inline bool compile() const;
//- Possibly compile the regular expression, with greater control
inline bool compile(const compOption) const;
//- Possibly compile the regular expression, with greater control
inline bool compile(const compOption) const;
//- Frees precompiled regular expression, making wordRe a literal.
// Optionally strips invalid word characters
inline void uncompile(const bool doStripInvalid = false) const;
//- Make wordRe a literal again, instead of a regular expression.
// Optionally strip invalid word characters.
inline void uncompile(const bool doStripInvalid = false) const;
// Editing
// Editing
//- Copy string, auto-test for regular expression or other options
inline void set(const std::string&, const compOption = DETECT);
//- Copy string, auto-test for regular expression or other options
inline void set(const std::string& str, const compOption = DETECT);
//- Copy string, auto-test for regular expression or other options
inline void set(const char*, const compOption = DETECT);
//- Copy string, auto-test for regular expression or other options
inline void set(const char* str, const compOption = DETECT);
//- Clear string and precompiled regular expression
inline void clear();
//- Clear string and regular expression
inline void clear();
// Searching
// Matching/Searching
//- Smart match as regular expression or as a string
// Optionally force a literal match only
inline bool match
(
const std::string&,
bool literalMatch = false
) const;
//- Smart match as regular expression or as a string.
// Optionally force a literal match only
inline bool match(const std::string& text, bool literal = false) const;
// Miscellaneous
// Miscellaneous
//- Return a string with quoted meta-characters
inline string quotemeta() const;
//- Return a string with quoted meta-characters
inline string quotemeta() const;
//- Output some basic info
Ostream& info(Ostream&) const;
//- Output some basic info
Ostream& info(Ostream& os) const;
// Member operators
// Assignment
// Assignment
//- Assign copy
// Always case sensitive
inline void operator=(const wordRe&);
//- Copy wordRe and its type (literal or regex)
// Always case sensitive
inline void operator=(const wordRe& str);
//- Copy word, never a regular expression
inline void operator=(const word&);
//- Copy word, never a regular expression
inline void operator=(const word& str);
//- Copy keyType, auto-test for regular expression
// Always case sensitive
inline void operator=(const keyType&);
//- Copy keyType and its type (literal or regex)
// Always case sensitive
inline void operator=(const keyType& str);
//- Copy string, auto-test for regular expression
// Always case sensitive
inline void operator=(const string&);
//- Copy string, auto-test for regular expression
// Always case sensitive
inline void operator=(const string& str);
//- Copy string, auto-test for regular expression
// Always case sensitive
inline void operator=(const std::string&);
//- Copy string, auto-test for regular expression
// Always case sensitive
inline void operator=(const std::string& str);
//- Copy string, auto-test for regular expression
// Always case sensitive
inline void operator=(const char*);
//- Copy string, auto-test for regular expression
// Always case sensitive
inline void operator=(const char* str);
// IOstream operators
friend Istream& operator>>(Istream&, wordRe&);
friend Ostream& operator<<(Ostream&, const wordRe&);
friend Istream& operator>>(Istream& is, wordRe& w);
friend Ostream& operator<<(Ostream& os, const wordRe& w);
};

View File

@ -25,13 +25,13 @@ License
// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
inline bool Foam::wordRe::meta(char c)
inline bool Foam::wordRe::meta(const char c)
{
return regExp::meta(c);
}
inline bool Foam::wordRe::isPattern(const string& str)
inline bool Foam::wordRe::isPattern(const std::string& str)
{
return string::meta<regExp>(str);
}
@ -167,10 +167,10 @@ inline bool Foam::wordRe::compile() const
inline void Foam::wordRe::uncompile(const bool doStripInvalid) const
{
if (re_.clear())
if (re_.clear() && doStripInvalid)
{
// skip stripping unless debug is active to avoid costly operations
if (word::debug && doStripInvalid)
if (word::debug)
{
string::stripInvalid<word>
(
@ -188,17 +188,17 @@ inline void Foam::wordRe::clear()
}
inline bool Foam::wordRe::match(const std::string& str, bool literalMatch) const
inline bool Foam::wordRe::match(const std::string& text, bool literal) const
{
if (literalMatch || !re_.exists())
if (literal || !re_.exists())
{
// check as string
return (str == *this);
return (text == *this);
}
else
{
// check as regex
return re_.match(str);
return re_.match(text);
}
}
@ -228,7 +228,6 @@ inline void Foam::wordRe::set(const char* str, const compOption opt)
inline void Foam::wordRe::operator=(const wordRe& str)
{
string::operator=(str);
if (str.isPattern())
{
compile();
@ -242,7 +241,7 @@ inline void Foam::wordRe::operator=(const wordRe& str)
inline void Foam::wordRe::operator=(const word& str)
{
word::operator=(str);
string::operator=(str);
re_.clear();
}
@ -264,21 +263,21 @@ inline void Foam::wordRe::operator=(const keyType& str)
inline void Foam::wordRe::operator=(const string& str)
{
string::operator=(str);
compile(DETECT); // auto-detect regex
compile(wordRe::DETECT); // auto-detect regex
}
inline void Foam::wordRe::operator=(const std::string& str)
{
string::operator=(str);
compile(DETECT); // auto-detect regex
compile(wordRe::DETECT); // auto-detect regex
}
inline void Foam::wordRe::operator=(const char* str)
{
string::operator=(str);
compile(DETECT); // auto-detect regex
compile(wordRe::DETECT); // auto-detect regex
}