#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"
using namespace std;
using namespace zypp;
-using namespace boost;
-using boost::regex;
+using namespace zypp::str;
namespace zypp
{
{
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;
}
// regex flags
unsigned int flags = regex::normal;
- flags |= regex_constants::icase;
+ flags |= regex::icase;
regex reg;
// create regex object
}
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;;
}
}
-#endif
\ No newline at end of file
+#endif
+
#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()
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();
}
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++;
#include <regex.h>
+#include "zypp/base/Exception.h"
+
///////////////////////////////////////////////////////////////////
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:
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();
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
#include "zypp/base/String.h"
+using std::string;
+
///////////////////////////////////////////////////////////////////
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;
+ }
+
/******************************************************************
**
**
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.
#include <iostream>
#include <map>
#include <algorithm>
+#include "zypp/base/String.h"
#include "zypp/repo/RepoException.h"
#include "zypp/ZConfig.h"
#include "RepoVariables.h"
{
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()
{}
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() ) );
basearch = ZConfig::instance().systemArchitecture();
}
- newvalue = gsub( newvalue,
- "$basearch",
- basearch.asString() );
+ newvalue = str::gsub( newvalue,
+ "$basearch",
+ basearch.asString() );
return newvalue;
}