Simplified code by replacing misc direct regex/regex.h invocation with SimpleRegex wrapper
This commit is contained in:
+11
-61
@@ -16,14 +16,10 @@
|
||||
*/
|
||||
|
||||
#include "autoconfig.h"
|
||||
#include "strmatcher.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#ifdef _WIN32
|
||||
#include <regex>
|
||||
#else
|
||||
#include <regex.h>
|
||||
#endif
|
||||
#include <fnmatch.h>
|
||||
|
||||
#include <string>
|
||||
@@ -31,19 +27,19 @@
|
||||
#include "cstr.h"
|
||||
#include "log.h"
|
||||
#include "pathut.h"
|
||||
#include "strmatcher.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool StrWildMatcher::match(const string& val) const
|
||||
{
|
||||
LOGDEB2("StrWildMatcher::match: [" << (m_sexp) << "] against [" << (val) << "]\n" );
|
||||
LOGDEB2("StrWildMatcher::match ["<< m_sexp<< "] against [" << val << "]\n");
|
||||
int ret = fnmatch(m_sexp.c_str(), val.c_str(), FNM_NOESCAPE);
|
||||
switch (ret) {
|
||||
case 0: return true;
|
||||
case FNM_NOMATCH: return false;
|
||||
default:
|
||||
LOGINFO("StrWildMatcher::match:err: e [" << (m_sexp) << "] s [" << (val) << "] (" << (url_encode(val)) << ") ret " << (ret) << "\n" );
|
||||
LOGINFO("StrWildMatcher::match:err: e [" << m_sexp << "] s [" << val
|
||||
<< "] (" << url_encode(val) << ") ret " << ret << "\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -54,68 +50,22 @@ string::size_type StrWildMatcher::baseprefixlen() const
|
||||
}
|
||||
|
||||
StrRegexpMatcher::StrRegexpMatcher(const string& exp)
|
||||
: StrMatcher(exp), m_compiled(0), m_errcode(0)
|
||||
: StrMatcher(exp),
|
||||
m_re(exp, SimpleRegexp::SRE_NOSUB)
|
||||
{
|
||||
setExp(exp);
|
||||
}
|
||||
|
||||
bool StrRegexpMatcher::setExp(const string& exp)
|
||||
{
|
||||
if (m_compiled) {
|
||||
#ifdef _WIN32
|
||||
delete (regex*)m_compiled;
|
||||
#else
|
||||
regfree((regex_t*)m_compiled);
|
||||
delete (regex_t*)m_compiled;
|
||||
#endif
|
||||
}
|
||||
m_compiled = 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
try {
|
||||
m_compiled = new regex(exp, std::regex_constants::nosubs |
|
||||
std::regex_constants::extended);
|
||||
} catch (...) {
|
||||
m_reason = string("StrRegexpMatcher:regcomp failed for ")
|
||||
+ exp + string("syntax error ?");
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
m_compiled = new regex_t;
|
||||
if ((m_errcode =
|
||||
regcomp((regex_t*)m_compiled, exp.c_str(), REG_EXTENDED|REG_NOSUB))) {
|
||||
char errbuf[200];
|
||||
regerror(m_errcode, (regex_t*)m_compiled, errbuf, 199);
|
||||
m_reason = string("StrRegexpMatcher:regcomp failed for ")
|
||||
+ exp + string(errbuf);
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
m_sexp = exp;
|
||||
return true;
|
||||
}
|
||||
|
||||
StrRegexpMatcher::~StrRegexpMatcher()
|
||||
{
|
||||
if (m_compiled) {
|
||||
#ifdef _WIN32
|
||||
delete (regex *)m_compiled;
|
||||
#else
|
||||
regfree((regex_t*)m_compiled);
|
||||
delete (regex_t*)m_compiled;
|
||||
#endif
|
||||
}
|
||||
m_re = SimpleRegexp(exp, SimpleRegexp::SRE_NOSUB);
|
||||
return m_re.ok();
|
||||
}
|
||||
|
||||
bool StrRegexpMatcher::match(const string& val) const
|
||||
{
|
||||
if (m_errcode)
|
||||
if (!m_re.ok())
|
||||
return false;
|
||||
#ifdef _WIN32
|
||||
return regex_match(val, *((regex *)m_compiled));
|
||||
#else
|
||||
return regexec((regex_t*)m_compiled, val.c_str(), 0, 0, 0) != REG_NOMATCH;
|
||||
#endif
|
||||
return m_re(val);
|
||||
}
|
||||
|
||||
string::size_type StrRegexpMatcher::baseprefixlen() const
|
||||
@@ -125,6 +75,6 @@ string::size_type StrRegexpMatcher::baseprefixlen() const
|
||||
|
||||
bool StrRegexpMatcher::ok() const
|
||||
{
|
||||
return !m_errcode;
|
||||
return m_re.ok();
|
||||
}
|
||||
|
||||
|
||||
+15
-31
@@ -18,6 +18,7 @@
|
||||
#define _STRMATCHER_H_INCLUDED_
|
||||
|
||||
#include <string>
|
||||
#include "smallut.h"
|
||||
|
||||
// Encapsulating simple wildcard/regexp string matching.
|
||||
|
||||
@@ -25,28 +26,22 @@
|
||||
class StrMatcher {
|
||||
public:
|
||||
StrMatcher(const std::string& exp)
|
||||
: m_sexp(exp)
|
||||
{
|
||||
}
|
||||
: m_sexp(exp) {}
|
||||
virtual ~StrMatcher() {};
|
||||
virtual bool match(const std::string &val) const = 0;
|
||||
virtual std::string::size_type baseprefixlen() const = 0;
|
||||
virtual bool setExp(const std::string& newexp)
|
||||
{
|
||||
virtual bool setExp(const std::string& newexp) {
|
||||
m_sexp = newexp;
|
||||
return true;
|
||||
}
|
||||
virtual bool ok() const
|
||||
{
|
||||
virtual bool ok() const {
|
||||
return true;
|
||||
}
|
||||
virtual const std::string& exp()
|
||||
{
|
||||
virtual const std::string& exp() const {
|
||||
return m_sexp;
|
||||
}
|
||||
virtual StrMatcher *clone() = 0;
|
||||
const string& getreason()
|
||||
{
|
||||
virtual StrMatcher *clone() const = 0;
|
||||
const std::string& getreason() const {
|
||||
return m_reason;
|
||||
}
|
||||
protected:
|
||||
@@ -57,16 +52,11 @@ protected:
|
||||
class StrWildMatcher : public StrMatcher {
|
||||
public:
|
||||
StrWildMatcher(const std::string& exp)
|
||||
: StrMatcher(exp)
|
||||
{
|
||||
}
|
||||
virtual ~StrWildMatcher()
|
||||
{
|
||||
}
|
||||
: StrMatcher(exp) {}
|
||||
virtual ~StrWildMatcher() {}
|
||||
virtual bool match(const std::string& val) const;
|
||||
virtual std::string::size_type baseprefixlen() const;
|
||||
virtual StrWildMatcher *clone()
|
||||
{
|
||||
virtual StrWildMatcher *clone() const {
|
||||
return new StrWildMatcher(m_sexp);
|
||||
}
|
||||
};
|
||||
@@ -74,22 +64,16 @@ public:
|
||||
class StrRegexpMatcher : public StrMatcher {
|
||||
public:
|
||||
StrRegexpMatcher(const std::string& exp);
|
||||
virtual bool setExp(const std::string& newexp);
|
||||
virtual ~StrRegexpMatcher();
|
||||
virtual bool setExp(const std::string& newexp) override;
|
||||
virtual ~StrRegexpMatcher() {};
|
||||
virtual bool match(const std::string& val) const;
|
||||
virtual std::string::size_type baseprefixlen() const;
|
||||
virtual bool ok() const;
|
||||
virtual StrRegexpMatcher *clone()
|
||||
{
|
||||
virtual bool ok() const override;
|
||||
virtual StrRegexpMatcher *clone() const {
|
||||
return new StrRegexpMatcher(m_sexp);
|
||||
}
|
||||
const string& getreason()
|
||||
{
|
||||
return m_reason;
|
||||
}
|
||||
private:
|
||||
void *m_compiled;
|
||||
bool m_errcode;
|
||||
SimpleRegexp m_re;
|
||||
};
|
||||
|
||||
#endif /* _STRMATCHER_H_INCLUDED_ */
|
||||
|
||||
Reference in New Issue
Block a user