diff --git a/Source/kwsys/RegularExpression.cxx b/Source/kwsys/RegularExpression.cxx index e6e16e52ea..cb7e0fb527 100644 --- a/Source/kwsys/RegularExpression.cxx +++ b/Source/kwsys/RegularExpression.cxx @@ -861,7 +861,8 @@ public: // find -- Matches the regular expression to the given string. // Returns true if found, and sets start and end indexes accordingly. bool RegularExpression::find(char const* string, - RegularExpressionMatch& rmatch) const + RegularExpressionMatch& rmatch, + std::string::size_type offset) const { char const* s; @@ -882,7 +883,7 @@ bool RegularExpression::find(char const* string, // If there is a "must appear" string, look for it. if (this->regmust) { - s = string; + s = string + offset; while ((s = strchr(s, this->regmust[0]))) { if (strncmp(s, this->regmust, this->regmlen) == 0) break; // Found it. @@ -896,14 +897,13 @@ bool RegularExpression::find(char const* string, // Mark beginning of line for ^ . regFind.regbol = string; + s = string + offset; // Simplest case: anchored match need be tried only once. if (this->reganch) - return ( - regFind.regtry(string, rmatch.startp, rmatch.endp, this->program) != 0); + return (regFind.regtry(s, rmatch.startp, rmatch.endp, this->program) != 0); // Messy cases: unanchored match. - s = string; if (this->regstart != '\0') // We know what char it must start with. while ((s = strchr(s, this->regstart))) { diff --git a/Source/kwsys/RegularExpression.hxx.in b/Source/kwsys/RegularExpression.hxx.in index b9eef35413..184d6acddc 100644 --- a/Source/kwsys/RegularExpression.hxx.in +++ b/Source/kwsys/RegularExpression.hxx.in @@ -121,6 +121,9 @@ inline std::string::size_type RegularExpressionMatch::end() const */ inline std::string::size_type RegularExpressionMatch::start(int n) const { + if (!this->startp[n]) { + return std::string::npos; + } return static_cast(this->startp[n] - this->searchstring); } @@ -131,6 +134,9 @@ inline std::string::size_type RegularExpressionMatch::start(int n) const */ inline std::string::size_type RegularExpressionMatch::end(int n) const { + if (!this->endp[n]) { + return std::string::npos; + } return static_cast(this->endp[n] - this->searchstring); } @@ -338,19 +344,20 @@ public: * This method is thread safe when called with different * RegularExpressionMatch instances. */ - bool find(char const*, RegularExpressionMatch&) const; + bool find(char const*, RegularExpressionMatch&, + std::string::size_type offset = 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*); + inline bool find(char const*, std::string::size_type offset = 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&); + inline bool find(std::string const&, std::string::size_type offset = 0); /** * Match indices @@ -474,18 +481,20 @@ inline bool RegularExpression::compile(std::string const& s) * Matches the regular expression to the given std string. * Returns true if found, and sets start and end indexes accordingly. */ -inline bool RegularExpression::find(char const* s) +inline bool RegularExpression::find(char const* s, + std::string::size_type offset) { - return this->find(s, this->regmatch); + return this->find(s, this->regmatch, offset); } /** * Matches the regular expression to the given std string. * Returns true if found, and sets start and end indexes accordingly. */ -inline bool RegularExpression::find(std::string const& s) +inline bool RegularExpression::find(std::string const& s, + std::string::size_type offset) { - return this->find(s.c_str()); + return this->find(s.c_str(), offset); } /**