Commit 8dae0191 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: regex matching on std::string, not Foam::string

- avoids unneeded promotion of types.
  Easier to switch regex engines in the future.
parent 03d18072
......@@ -24,16 +24,14 @@ License
\*---------------------------------------------------------------------------*/
#include "regExp.H"
#include "string.H"
#include "List.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class StringType>
bool Foam::regExp::matchGrouping
(
const std::string& text,
List<StringType>& groups
List<std::string>& groups
) const
{
if (preg_ && !text.empty())
......@@ -194,7 +192,7 @@ std::string::size_type Foam::regExp::find(const std::string& text) const
}
}
return string::npos;
return std::string::npos;
}
......@@ -231,28 +229,4 @@ bool Foam::regExp::match
}
bool Foam::regExp::match
(
const std::string& text,
List<Foam::string>& groups
) const
{
return matchGrouping(text, groups);
}
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
void Foam::regExp::operator=(const char* pattern)
{
set(pattern);
}
void Foam::regExp::operator=(const std::string& pattern)
{
set(pattern);
}
// ************************************************************************* //
......@@ -51,7 +51,6 @@ namespace Foam
{
// Forward declaration of classes
class string;
template<class T> class List;
......@@ -76,12 +75,10 @@ class 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
template<class StringType>
bool matchGrouping
(
const std::string&,
List<StringType>& groups
List<std::string>& groups
) const;
......@@ -96,16 +93,7 @@ public:
// range: '[', ']' \n
//
// Don't bother checking for '{digit}' bounds
inline static bool meta(const char c)
{
return
(
(c == '.') // any character
|| (c == '*' || c == '+' || c == '?') // quantifiers
|| (c == '(' || c == ')' || c == '|') // grouping/branching
|| (c == '[' || c == ']') // range
);
}
inline static bool meta(const char c);
// Constructors
......@@ -129,22 +117,13 @@ public:
// Access
//- Return true if a precompiled expression does not exist
inline bool empty() const
{
return !preg_;
}
inline bool empty() const;
//- Does a precompiled expression exist?
inline bool exists() const
{
return preg_ ? true : false;
}
inline bool exists() const;
//- The number of capture groups for a non-empty expression
inline unsigned ngroups() const
{
return preg_ ? preg_->re_nsub : 0;
}
inline unsigned ngroups() const;
// Editing
......@@ -173,26 +152,19 @@ public:
// 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& text, List<string>& groups) const;
//- Return true if the regex was found within string
bool search(const std::string& text) const
{
return std::string::npos != find(text);
}
bool search(const std::string& text) const;
// Member Operators
//- Assign and compile pattern from a character array
// Always case sensitive
void operator=(const char* pattern);
inline void operator=(const char* pattern);
//- Assign and compile pattern from string
// Always case sensitive
void operator=(const std::string& pattern);
inline void operator=(const std::string& pattern);
};
......@@ -202,6 +174,8 @@ public:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "regExpI.H"
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 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 3 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, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
inline bool Foam::regExp::meta(const char c)
{
return
(
(c == '.') // any character
|| (c == '*' || c == '+' || c == '?') // quantifiers
|| (c == '(' || c == ')' || c == '|') // grouping/branching
|| (c == '[' || c == ']') // range
);
}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
inline bool Foam::regExp::empty() const
{
return !preg_;
}
inline bool Foam::regExp::exists() const
{
return preg_ ? true : false;
}
inline unsigned Foam::regExp::ngroups() const
{
return preg_ ? preg_->re_nsub : 0;
}
inline bool Foam::regExp::search(const std::string& text) const
{
return std::string::npos != find(text);
}
// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
inline void Foam::regExp::operator=(const char* pattern)
{
set(pattern);
}
inline void Foam::regExp::operator=(const std::string& pattern)
{
set(pattern);
}
// ************************************************************************* //
......@@ -68,28 +68,28 @@ public:
// Member Functions
// Access
// Access
inline label size() const;
inline bool empty() const;
inline label size() const;
inline bool empty() const;
//- Return underlying list of wordRe
inline const UList<wordRe>& operator()() const;
//- Return underlying list of wordRe
inline const UList<wordRe>& operator()() const;
// Searching
// Searching
//- Return true if string matches any of the regular expressions
// 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 true if string matches any of the regular expressions
// Smart match as regular expression or as a string.
// Optionally specify a literal match only.
inline bool match(const std::string&, bool literalMatch=false) const;
// Helpers
// Helpers
//- Return a wordReList with duplicate words filtered out.
// No filtering is done on regular expressions.
static wordReList uniq(const UList<wordRe>& input);
//- Return a wordReList with duplicate words filtered out.
// No filtering is done on regular expressions.
static wordReList uniq(const UList<wordRe>& input);
};
......
......@@ -58,14 +58,14 @@ Foam::wordReListMatcher::operator()() const
inline bool Foam::wordReListMatcher::match
(
const string& str,
const std::string& text,
bool literalMatch
) const
{
const label nElem = reList_.size();
for (label elemI = 0; elemI < nElem; ++elemI)
const label n = reList_.size();
for (label i = 0; i < n; ++i)
{
if (reList_[elemI].match(str, literalMatch))
if (reList_[i].match(text, literalMatch))
{
return true;
}
......
......@@ -63,9 +63,9 @@ class Ostream;
// Forward declaration of friend functions and operators
class string;
Istream& operator>>(Istream&, string&);
Ostream& operator<<(Ostream&, const string&);
Ostream& operator<<(Ostream&, const std::string&);
Istream& operator>>(Istream& is, string& s);
Ostream& operator<<(Ostream& os, const string& s);
Ostream& operator<<(Ostream& os, const std::string& s);
/*---------------------------------------------------------------------------*\
......@@ -124,33 +124,33 @@ public:
//- Count and return the number of a given character in the string
size_type count(const char c) const;
//- Is this string type valid?
//- Does the string contain valid characters only?
template<class String>
static inline bool valid(const string& str);
static inline bool valid(const std::string& str);
//- Does this string have particular meta-characters?
//- Does this string contain meta-characters?
// The meta characters can be optionally quoted.
template<class String>
static inline bool meta(const string& str, const char quote = '\\');
static inline bool meta(const std::string& str, const char quote='\\');
//- Strip invalid characters from the given string
template<class String>
static inline bool stripInvalid(string& str);
static inline bool stripInvalid(std::string& str);
//- Return a valid String from the given string
template<class String>
static inline String validate(const string& str);
static inline String validate(const std::string& str);
//- Return a String with quoted meta-characters from the given string
template<class String>
static inline string quotemeta
(
const string& str,
const std::string& str,
const char quote = '\\'
);
//- True when strings match literally
inline bool match(const std::string& str) const;
inline bool match(const std::string& text) const;
//- Avoid masking the normal std::string replace
using std::string::replace;
......
......@@ -61,7 +61,7 @@ inline Foam::string::string(const char c)
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class String>
inline bool Foam::string::valid(const string& str)
inline bool Foam::string::valid(const std::string& str)
{
for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
{
......@@ -75,7 +75,7 @@ inline bool Foam::string::valid(const string& str)
template<class String>
inline bool Foam::string::stripInvalid(string& str)
inline bool Foam::string::stripInvalid(std::string& str)
{
if (!valid<String>(str))
{
......@@ -85,11 +85,11 @@ inline bool Foam::string::stripInvalid(string& str)
for
(
const_iterator iter1 = iter2;
iter1 != const_cast<const string&>(str).end();
iter1 != const_cast<const std::string&>(str).end();
iter1++
)
{
char c = *iter1;
const char c = *iter1;
if (String::valid(c))
{
......@@ -109,20 +109,21 @@ inline bool Foam::string::stripInvalid(string& str)
template<class String>
inline bool Foam::string::meta(const string& str, const char quote)
inline bool Foam::string::meta(const std::string& str, const char quote)
{
int escaped = 0;
for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
{
if (quote && *iter == quote)
const char c = *iter;
if (quote && c == quote)
{
escaped ^= 1; // toggle state
}
else if (escaped)
{
escaped = false;
escaped = 0;
}
else if (String::meta(*iter))
else if (String::meta(c))
{
return true;
}
......@@ -133,7 +134,7 @@ inline bool Foam::string::meta(const string& str, const char quote)
template<class String>
inline Foam::string
Foam::string::quotemeta(const string& str, const char quote)
Foam::string::quotemeta(const std::string& str, const char quote)
{
if (!quote)
{
......@@ -146,7 +147,8 @@ Foam::string::quotemeta(const string& str, const char quote)
int escaped = 0;
for (const_iterator iter = str.begin(); iter != str.end(); ++iter)
{
if (*iter == quote)
const char c = *iter;
if (c == quote)
{
escaped ^= 1; // toggle state
}
......@@ -154,12 +156,12 @@ Foam::string::quotemeta(const string& str, const char quote)
{
escaped = 0;
}
else if (String::meta(*iter))
else if (String::meta(c))
{
sQuoted += quote;
}
sQuoted += *iter;
sQuoted += c;
}
sQuoted.resize(sQuoted.length());
......@@ -169,17 +171,17 @@ Foam::string::quotemeta(const string& str, const char quote)
template<class String>
inline String Foam::string::validate(const string& str)
inline String Foam::string::validate(const std::string& str)
{
string ss = str;
stripInvalid<String>(ss);
return ss;
}
inline bool Foam::string::match(const std::string& str) const
inline bool Foam::string::match(const std::string& text) const
{
// check as string
return (str == *this);
return (text == *this);
}
......
......@@ -58,7 +58,7 @@ Foam::fileFormats::STARCDsurfaceFormatCore::readInpCellTable
);
string line;
List<string> groups;
List<std::string> groups;
while (is.good() && is.getLine(line).good())
{
if (ctnameRE.match(line, groups))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment