Skip to content
Snippets Groups Projects
Commit 511adf49 authored by Brad King's avatar Brad King Committed by Kitware Robot
Browse files

Merge topic 'regex2'


c8f5f4bf RegularExpression: Add an options parameter to find()
d022423b RegularExpression: Add a method to query the number of capture groups
46fc21ef RegularExpression: Remove redundant overloads

Acked-by: default avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !321
parents 2fbdf7e5 c8f5f4bf
No related branches found
No related tags found
1 merge request!321RegularExpression: A few small features for CMake
......@@ -60,6 +60,7 @@ RegularExpression::RegularExpression(RegularExpression const& rxp)
this->regstart = rxp.regstart; // Copy starting index
this->reganch = rxp.reganch; // Copy remaining private data
this->regmlen = rxp.regmlen; // Copy remaining private data
this->regnpar = rxp.regnpar;
}
// operator= -- Copies the given regular expression.
......@@ -93,6 +94,7 @@ RegularExpression& RegularExpression::operator=(RegularExpression const& rxp)
this->regstart = rxp.regstart; // Copy starting index
this->reganch = rxp.reganch; // Copy remaining private data
this->regmlen = rxp.regmlen; // Copy remaining private data
this->regnpar = rxp.regnpar;
return *this;
}
......@@ -371,6 +373,7 @@ bool RegularExpression::compile(char const* exp)
// #endif
this->program = new char[comp.regsize];
this->progsize = static_cast<int>(comp.regsize);
this->regnpar = comp.regnpar;
if (!this->program) {
// RAISE Error, SYM(RegularExpression), SYM(Out_Of_Memory),
......@@ -852,6 +855,7 @@ public:
char const* regbol; // Beginning of input, for ^ check.
char const** regstartp; // Pointer to startp array.
char const** regendp; // Ditto for endp.
char const* regreject; // Reject matches ending here, for NONEMPTY_AT_OFFSET.
int regtry(char const*, char const**, char const**, char const*);
int regmatch(char const*);
......@@ -862,7 +866,8 @@ public:
// Returns true if found, and sets start and end indexes accordingly.
bool RegularExpression::find(char const* string,
RegularExpressionMatch& rmatch,
std::string::size_type offset) const
std::string::size_type offset,
unsigned options) const
{
char const* s;
......@@ -894,10 +899,11 @@ bool RegularExpression::find(char const* string,
}
RegExpFind regFind;
s = string + offset;
// Mark beginning of line for ^ .
regFind.regbol = string;
s = string + offset;
regFind.regbol = (options & BOL_AT_OFFSET) ? s : string;
regFind.regreject = (options & NONEMPTY_AT_OFFSET) ? s : nullptr;
// Simplest case: anchored match need be tried only once.
if (this->reganch)
......@@ -1164,7 +1170,9 @@ int RegExpFind::regmatch(char const* prog)
}
// break;
case END:
return (1); // Success!
if (reginput == regreject)
return (0); // Can't end a match here
return (1); // Success!
default:
// RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
......
......@@ -42,11 +42,9 @@ public:
bool isValid() const;
void clear();
std::string::size_type start() const;
std::string::size_type end() const;
std::string::size_type start(int n) const;
std::string::size_type end(int n) const;
std::string match(int n) const;
std::string::size_type start(int n = 0) const;
std::string::size_type end(int n = 0) const;
std::string match(int n = 0) const;
enum
{
......@@ -99,22 +97,6 @@ inline void RegularExpressionMatch::clear()
searchstring = nullptr;
}
/**
* \brief Returns the start index of the full match.
*/
inline std::string::size_type RegularExpressionMatch::start() const
{
return static_cast<std::string::size_type>(this->startp[0] - searchstring);
}
/**
* \brief Returns the end index of the full match.
*/
inline std::string::size_type RegularExpressionMatch::end() const
{
return static_cast<std::string::size_type>(this->endp[0] - searchstring);
}
/**
* \brief Returns the start index of nth submatch.
* start(0) is the start of the full match.
......@@ -299,6 +281,14 @@ inline std::string RegularExpressionMatch::match(int n) const
class @KWSYS_NAMESPACE@_EXPORT RegularExpression
{
public:
enum Options : unsigned
{
// Match ^ at offset instead of the input start.
BOL_AT_OFFSET = 1,
// If an empty match is found at offset, continue searching.
NONEMPTY_AT_OFFSET = 2,
};
/**
* Instantiate RegularExpression with program=nullptr.
*/
......@@ -345,33 +335,33 @@ public:
* RegularExpressionMatch instances.
*/
bool find(char const*, RegularExpressionMatch&,
std::string::size_type offset = 0) const;
std::string::size_type offset = 0, unsigned options = 0) const;
/**
* Matches the regular expression to the given string.
* Returns true if found, and sets start and end indexes accordingly.
*/
inline bool find(char const*, std::string::size_type offset = 0);
inline bool find(char const*, std::string::size_type offset = 0,
unsigned options = 0);
/**
* Matches the regular expression to the given std string.
* Returns true if found, and sets start and end indexes accordingly.
*/
inline bool find(std::string const&, std::string::size_type offset = 0);
inline bool find(std::string const&, std::string::size_type offset = 0,
unsigned options = 0);
/**
* Match indices
*/
inline RegularExpressionMatch const& regMatch() const;
inline std::string::size_type start() const;
inline std::string::size_type end() const;
inline std::string::size_type start(int n) const;
inline std::string::size_type end(int n) const;
inline std::string::size_type start(int n = 0) const;
inline std::string::size_type end(int n = 0) const;
/**
* Match strings
*/
inline std::string match(int n) const;
inline std::string match(int n = 0) const;
/**
* Copy the given regular expression.
......@@ -406,6 +396,11 @@ public:
*/
inline void set_invalid();
/**
* The number of capture groups.
*/
inline int num_groups();
private:
RegularExpressionMatch regmatch;
char regstart; // Internal use only
......@@ -414,6 +409,7 @@ private:
std::string::size_type regmlen; // Internal use only
char* program;
int progsize;
int regnpar;
};
/**
......@@ -425,6 +421,7 @@ inline RegularExpression::RegularExpression()
, regmust{}
, program{ nullptr }
, progsize{}
, regnpar{}
{
}
......@@ -438,6 +435,7 @@ inline RegularExpression::RegularExpression(char const* s)
, regmust{}
, program{ nullptr }
, progsize{}
, regnpar{}
{
if (s) {
this->compile(s);
......@@ -454,6 +452,7 @@ inline RegularExpression::RegularExpression(std::string const& s)
, regmust{}
, program{ nullptr }
, progsize{}
, regnpar{}
{
this->compile(s);
}
......@@ -482,9 +481,10 @@ inline bool RegularExpression::compile(std::string const& s)
* Returns true if found, and sets start and end indexes accordingly.
*/
inline bool RegularExpression::find(char const* s,
std::string::size_type offset)
std::string::size_type offset,
unsigned options)
{
return this->find(s, this->regmatch, offset);
return this->find(s, this->regmatch, offset, options);
}
/**
......@@ -492,9 +492,10 @@ inline bool RegularExpression::find(char const* s,
* Returns true if found, and sets start and end indexes accordingly.
*/
inline bool RegularExpression::find(std::string const& s,
std::string::size_type offset)
std::string::size_type offset,
unsigned options)
{
return this->find(s.c_str(), offset);
return this->find(s.c_str(), this->regmatch, offset, options);
}
/**
......@@ -505,22 +506,6 @@ inline RegularExpressionMatch const& RegularExpression::regMatch() const
return this->regmatch;
}
/**
* Returns the start index of the full match.
*/
inline std::string::size_type RegularExpression::start() const
{
return regmatch.start();
}
/**
* Returns the end index of the full match.
*/
inline std::string::size_type RegularExpression::end() const
{
return regmatch.end();
}
/**
* Return start index of nth submatch. start(0) is the start of the full match.
*/
......@@ -571,6 +556,11 @@ inline void RegularExpression::set_invalid()
this->program = nullptr;
}
inline int RegularExpression::num_groups()
{
return this->regnpar - 1;
}
} // namespace @KWSYS_NAMESPACE@
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment