diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2019-08-21 22:00:35 +0200 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2019-11-25 11:32:19 +0100 |
commit | 887b07aa284af75516fbf54e3db88acbf7e51b0c (patch) | |
tree | eeb6e67e58502281d850332ad620a59ff11effce | |
parent | 83f2732b10498c4a3abdfbc072da63803557ce9b (diff) |
patterns: Add base class for regular expression matching
-rw-r--r-- | apt-pkg/cachefilter-patterns.cc | 36 | ||||
-rw-r--r-- | apt-pkg/cachefilter-patterns.h | 15 |
2 files changed, 51 insertions, 0 deletions
diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index bf6166ee4..42bc2babb 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -6,8 +6,12 @@ * SPDX-License-Identifier: GPL-2.0+ */ +#include <config.h> + #include <apt-pkg/cachefilter-patterns.h> +#include <apti18n.h> + namespace APT { namespace Internal @@ -272,6 +276,38 @@ std::string PatternParser::aWord(std::unique_ptr<PatternTreeParser::Node> &nodeP return node->word.to_string(); } +namespace Patterns +{ + +BaseRegexMatcher::BaseRegexMatcher(std::string const &Pattern) +{ + pattern = new regex_t; + int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB); + if (Res == 0) + return; + + delete pattern; + pattern = NULL; + char Error[300]; + regerror(Res, pattern, Error, sizeof(Error)); + _error->Error(_("Regex compilation error - %s"), Error); +} +bool BaseRegexMatcher::operator()(const char *string) +{ + if (unlikely(pattern == NULL)) + return false; + else + return regexec(pattern, string, 0, 0, 0) == 0; +} +BaseRegexMatcher::~BaseRegexMatcher() +{ + if (pattern == NULL) + return; + regfree(pattern); + delete pattern; +} +} // namespace Patterns + } // namespace Internal // The bridge into the public world diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index d37da815f..f13edbbc2 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -117,6 +117,21 @@ namespace Patterns { using namespace APT::CacheFilter; +/** \brief Basic helper class for matching regex */ +class BaseRegexMatcher +{ + regex_t *pattern; + + public: + BaseRegexMatcher(std::string const &string); + ~BaseRegexMatcher(); + bool operator()(const char *cstring); + bool operator()(std::string const &string) + { + return (*this)(string.c_str()); + } +}; + struct PackageIsAutomatic : public PackageMatcher { pkgCacheFile *Cache; |