X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2FLRegex.C;h=a82b6642301e3e666388108a78d20247b5a851d7;hb=5296e9e9952cf6e2f306077c62a1caeaf8479eed;hp=6f7af728ac64cbe5c0aa77f50bb797992e61c22b;hpb=2cf98b74fc52411dfe61eed649ae755fc1b25df8;p=lyx.git diff --git a/src/support/LRegex.C b/src/support/LRegex.C index 6f7af728ac..a82b664230 100644 --- a/src/support/LRegex.C +++ b/src/support/LRegex.C @@ -1,74 +1,82 @@ -#ifdef __GNUG__ -#pragma implementation -#endif - #include #include + +#ifdef HAVE_REGEX_H #include +#else +#include "lyxregex.h" +#endif + +#ifdef __GNUG__ +#pragma implementation +#endif + #include "LRegex.h" +using std::make_pair; + /// struct LRegex::Impl { /// - re_pattern_buffer * preg; - + regex_t * preg; + /// int error_code; - + /// mutable LRegex::SubMatches matches; - + /// - Impl(string const & regex) - : preg(new re_pattern_buffer), error_code(0) + Impl(string const & regex) + : preg(new regex_t), error_code(0) { error_code = regcomp(preg, regex.c_str(), REG_EXTENDED); } - + /// ~Impl() { regfree(preg); delete preg; } - + /// bool exact_match(string const & str) const { regmatch_t tmp; if (!regexec(preg, str.c_str(), 1, &tmp, 0)) { - if (tmp.rm_so == 0 && + if (tmp.rm_so == 0 && tmp.rm_eo == static_cast(str.length())) return true; } // no match return false; } - + /// - LRegex::MatchPair first_match(string const & str) const + LRegex::MatchPair const first_match(string const & str) const { regmatch_t tmp; regexec(preg, str.c_str(), 1, &tmp, 0); - unsigned int first = tmp.rm_so != -1 ? - static_cast(tmp.rm_so) : string::npos; - unsigned int second = tmp.rm_eo != -1 ? - static_cast(tmp.rm_eo) : string::npos; + string::size_type const first = tmp.rm_so != -1 ? + tmp.rm_so : string::npos; + string::size_type const second = tmp.rm_eo != -1 ? + tmp.rm_eo : string::npos; return make_pair(first, second - first); } - + /// - string getError() const + string const getError() const { size_t nr = regerror(error_code, preg, 0, 0); char * tmp = new char[nr]; regerror(error_code, preg, tmp, nr); - string ret(tmp); + string const ret(tmp); delete [] tmp; return ret; } - + /// LRegex::SubMatches const & exec(string const & str) const { @@ -80,20 +88,19 @@ struct LRegex::Impl { // func much faster, but client code will be simpler, // because then it will only be needed to scan through // all the entries in matches. - size_t subs = (preg->re_nsub != 0 ? (preg->re_nsub + 1) : 1); + size_t const subs = + (preg->re_nsub != 0 ? (preg->re_nsub + 1) : 1); regmatch_t * mat = new regmatch_t[subs]; - unsigned int first = 0; - unsigned int second = 0; + string::size_type first = 0; + string::size_type second = 0; matches.erase(matches.begin(), matches.end()); if (!regexec(preg, str.c_str(), subs, mat, 0)) { // some match matches.reserve(subs); for (size_t i = 0; i < subs; ++i) { first = mat[i].rm_so != -1 ? - static_cast - (mat[i].rm_so) : string::npos; + mat[i].rm_so : string::npos; second = mat[i].rm_eo != -1 ? - static_cast - (mat[i].rm_eo) : string::npos; + mat[i].rm_eo : string::npos; matches.push_back(make_pair(first, second - first)); } @@ -126,13 +133,13 @@ bool LRegex::exact_match(string const & str) const } -LRegex::MatchPair LRegex::first_match(string const & str) const +LRegex::MatchPair const LRegex::first_match(string const & str) const { return impl->first_match(str); } -string LRegex::getError() const +string const LRegex::getError() const { return impl->getError(); }