From: Duncan Mac-Vicar P Date: Fri, 10 Aug 2007 14:08:41 +0000 (+0000) Subject: - adapting regex code X-Git-Tag: BASE-SuSE-Linux-10_3-Branch~303 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b7a35e806014cd38086bf472355ce54a41aed302;p=platform%2Fupstream%2Flibzypp.git - adapting regex code --- diff --git a/zypp/Locks.cc b/zypp/Locks.cc index 2656352..3846762 100644 --- a/zypp/Locks.cc +++ b/zypp/Locks.cc @@ -9,9 +9,10 @@ #include #include -#include #include +#include +#include #include "zypp/base/Logger.h" #include "zypp/base/IOStream.h" #include "zypp/PoolItem.h" @@ -27,8 +28,7 @@ using namespace std; using namespace zypp; -using namespace boost; -using boost::regex; +using namespace zypp::str; namespace zypp { @@ -76,18 +76,18 @@ wildcards2regex(const string & str) { string regexed; - regex all("\\*"); // regex to search for '*' - regex one("\\?"); // regex to search for '?' + string all("*"); // regex to search for '*' + string one("?"); // regex to search for '?' string r_all(".*"); // regex equivalent of '*' string r_one("."); // regex equivalent of '?' // replace all "*" in input with ".*" - regexed = regex_replace(str, all, r_all); + regexed = str::gsub( str, all, r_all ); MIL << "wildcards2regex: " << str << " -> " << regexed; // replace all "?" in input with "." - regexed = regex_replace(regexed, one, r_one); - MIL << " -> " << regexed << endl; + regexed = str::gsub(regexed, one, r_one); + MIL << " -> " << regexed << endl; return regexed; } @@ -176,7 +176,7 @@ struct AddLockToPool // regex flags unsigned int flags = regex::normal; - flags |= regex_constants::icase; + flags |= regex::icase; regex reg; // create regex object @@ -188,7 +188,7 @@ struct AddLockToPool } catch (regex_error & e) { - ERR << "locks: " << regstr << " is not a valid regular expression: \"" << e.what() << "\"" << endl; + ERR << "locks: " << regstr << " is not a valid regular expression: \"" << e.msg() << "\"" << endl; ERR << "This is a bug, please file a bug report against libzypp-zmd-backend" << endl; // ignore this lock and continue return true;; diff --git a/zypp/Locks.h b/zypp/Locks.h index f3789d3..98212a2 100644 --- a/zypp/Locks.h +++ b/zypp/Locks.h @@ -14,4 +14,5 @@ namespace zypp } } -#endif \ No newline at end of file +#endif + diff --git a/zypp/base/Regex.cc b/zypp/base/Regex.cc index 33d2af2..b6eb990 100644 --- a/zypp/base/Regex.cc +++ b/zypp/base/Regex.cc @@ -16,13 +16,35 @@ #include "zypp/base/Regex.h" +using namespace zypp; using namespace zypp::str; -regex::regex(const std::string& str, int flags) +regex::regex() + : m_valid(false) +{ + +} + +void regex::assign(const std::string& str,int flags) { m_valid = true; - if (regcomp(&m_preg, str.c_str(), REG_EXTENDED | flags)) + int err; + char errbuff[100]; + if (!(flags & normal)) { + flags |= match_extended; + flags &= ~(normal); + } + + if ((err = regcomp(&m_preg, str.c_str(), flags))) { m_valid = false; + regerror(err, &m_preg, errbuff, sizeof(errbuff)); + ZYPP_THROW(regex_error(std::string(errbuff))); + } +} + +regex::regex(const std::string& str, int flags) +{ + assign(str, flags); } regex::~regex() throw() @@ -51,7 +73,7 @@ smatch::smatch() std::string smatch::operator[](unsigned i) const { - if (i < 12 && pmatch[i].rm_so != -1) + if (i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1) return match_str.substr(pmatch[i].rm_so, pmatch[i].rm_eo-pmatch[i].rm_so); return std::string(); } @@ -60,7 +82,7 @@ std::string smatch::operator[](unsigned i) const unsigned smatch::size() const { unsigned matches = 0; - while (matches < 12 && pmatch[matches+1].rm_so != -1) { + while (matches < ((sizeof(pmatch)/sizeof(*pmatch))-1) && pmatch[matches+1].rm_so != -1) { // std::cout << "match[" << matches << "]: *" << (*this)[matches // +1] << "*" << std::endl; matches++; diff --git a/zypp/base/Regex.h b/zypp/base/Regex.h index 6189fd5..4d4cdad 100644 --- a/zypp/base/Regex.h +++ b/zypp/base/Regex.h @@ -17,6 +17,8 @@ #include +#include "zypp/base/Exception.h" + /////////////////////////////////////////////////////////////////// namespace zypp { ///////////////////////////////////////////////////////////////// @@ -27,6 +29,14 @@ namespace zypp namespace str { ///////////////////////////////////////////////////////////////// + typedef Exception regex_error; + + class smatch; + class regex; + + bool regex_match(const std::string& s, str::smatch& matches, const regex& regex); + bool regex_match(const std::string& s, const regex& regex); + class regex { public: @@ -35,18 +45,24 @@ namespace zypp match_extra = 0, icase = REG_ICASE, nosubs = REG_NOSUB, - match_extended = REG_EXTENDED + match_extended = REG_EXTENDED, + normal = 1<<16 }; - + + regex(); regex(const std::string& s,int flags = match_extended); ~regex() throw(); - public: + void assign(const std::string& s,int flags = match_extended); + + private: + friend class smatch; + friend bool regex_match(const std::string& s, str::smatch& matches, const regex& regex); + friend bool regex_match(const std::string& s, const regex& regex); regex_t m_preg; bool m_valid; }; - class smatch { public: smatch(); @@ -59,8 +75,7 @@ namespace zypp regmatch_t pmatch[12]; }; - bool regex_match(const std::string& s, str::smatch& matches, const regex& regex); - bool regex_match(const std::string& s, const regex& regex); + ///////////////////////////////////////////////////////////////// } // namespace str diff --git a/zypp/base/String.cc b/zypp/base/String.cc index 7bb7948..cac28ad 100644 --- a/zypp/base/String.cc +++ b/zypp/base/String.cc @@ -16,6 +16,8 @@ #include "zypp/base/String.h" +using std::string; + /////////////////////////////////////////////////////////////////// namespace zypp { ///////////////////////////////////////////////////////////////// @@ -155,6 +157,29 @@ namespace zypp return ret; } + string gsub(const string& sData, const string& sFrom, const string& sTo) + { + string sNew = sData; + + if (! sNew.empty()) + { + string::size_type toLen = sTo.length(); + string::size_type frLen = sFrom.length(); + string::size_type loc = 0; + + while (string::npos != (loc = sNew.find(sFrom, loc))) + { + sNew.replace(loc, frLen, sTo); + loc += toLen; + + if (loc >= sNew.length()) + break; + } + } + + return sNew; + } + /****************************************************************** ** ** diff --git a/zypp/base/String.h b/zypp/base/String.h index 7a6864c..2f914ec 100644 --- a/zypp/base/String.h +++ b/zypp/base/String.h @@ -322,6 +322,14 @@ namespace zypp inline bool endsWith(const std::string& s, const char* str) { return s.find(str) == s.size() - strlen(str)-1; } inline bool contains(const std::string& s, const char* str) { return s.find(str) != std::string::npos; } + /** + * \short Looks for text in a string and replaces it. + * + * \note It only perform substtution in one pass + */ + std::string gsub( const std::string& sData, const std::string& sFrom, const std::string& sTo); + + /////////////////////////////////////////////////////////////////// /** \name String prefix handling. diff --git a/zypp/repo/RepoVariables.cc b/zypp/repo/RepoVariables.cc index 4e8cb23..8bf617f 100644 --- a/zypp/repo/RepoVariables.cc +++ b/zypp/repo/RepoVariables.cc @@ -10,6 +10,7 @@ #include #include #include +#include "zypp/base/String.h" #include "zypp/repo/RepoException.h" #include "zypp/ZConfig.h" #include "RepoVariables.h" @@ -20,32 +21,6 @@ namespace zypp { namespace repo { - -static string -gsub(const string& sData, - const string& sFrom, - const string& sTo) -{ - string sNew = sData; - - if (! sNew.empty()) - { - string::size_type toLen = sTo.length(); - string::size_type frLen = sFrom.length(); - string::size_type loc = 0; - - while (string::npos != (loc = sNew.find(sFrom, loc))) - { - sNew.replace(loc, frLen, sTo); - loc += toLen; - - if (loc >= sNew.length()) - break; - } - } - - return sNew; -} RepoVariablesStringReplacer::RepoVariablesStringReplacer() {} @@ -58,9 +33,9 @@ std::string RepoVariablesStringReplacer::operator()( const std::string &value ) string newvalue(value); // $arch - newvalue = gsub( newvalue, - "$arch", - ZConfig::instance().systemArchitecture().asString() ); + newvalue = str::gsub( newvalue, + "$arch", + ZConfig::instance().systemArchitecture().asString() ); // $basearch Arch::CompatSet cset( Arch::compatSet( ZConfig::instance().systemArchitecture() ) ); @@ -75,9 +50,9 @@ std::string RepoVariablesStringReplacer::operator()( const std::string &value ) basearch = ZConfig::instance().systemArchitecture(); } - newvalue = gsub( newvalue, - "$basearch", - basearch.asString() ); + newvalue = str::gsub( newvalue, + "$basearch", + basearch.asString() ); return newvalue; }