- adapting regex code
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 10 Aug 2007 14:08:41 +0000 (14:08 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 10 Aug 2007 14:08:41 +0000 (14:08 +0000)
zypp/Locks.cc
zypp/Locks.h
zypp/base/Regex.cc
zypp/base/Regex.h
zypp/base/String.cc
zypp/base/String.h
zypp/repo/RepoVariables.cc

index 2656352..3846762 100644 (file)
@@ -9,9 +9,10 @@
 
 #include <set>
 #include <fstream>
-#include <boost/regex.hpp>
 #include <boost/function.hpp>
 
+#include <zypp/base/Regex.h>
+#include <zypp/base/String.h>
 #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;;
index f3789d3..98212a2 100644 (file)
@@ -14,4 +14,5 @@ namespace zypp
   }
 }
     
-#endif
\ No newline at end of file
+#endif
+
index 33d2af2..b6eb990 100644 (file)
 
 #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++;
index 6189fd5..4d4cdad 100644 (file)
@@ -17,6 +17,8 @@
 
 #include <regex.h>
 
+#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
index 7bb7948..cac28ad 100644 (file)
@@ -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;
+    }
+    
     /******************************************************************
     **
     **
index 7a6864c..2f914ec 100644 (file)
@@ -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.
index 4e8cb23..8bf617f 100644 (file)
@@ -10,6 +10,7 @@
 #include <iostream>
 #include <map>
 #include <algorithm>
+#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;
 }