Refactoring: Changed all check parameters starting with a 'p' to the new rulespec...
[check_mk.git] / livestatus / src / RegExp.cc
blob8e60074e8a057e4e1367d8c129686ad582113806
1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
7 // | |
8 // | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
9 // +------------------------------------------------------------------+
11 // This file is part of Check_MK.
12 // The official homepage is at http://mathias-kettner.de/check_mk.
14 // check_mk is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by
16 // the Free Software Foundation in version 2. check_mk is distributed
17 // in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 // out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 // PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 // tails. You should have received a copy of the GNU General Public
21 // License along with GNU Make; see the file COPYING. If not, write
22 // to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 // Boston, MA 02110-1301 USA.
25 #include "RegExp.h"
27 #ifdef HAVE_RE2
28 // -----------------------------------------------------------------------------
29 // RE2 implementation
30 // -----------------------------------------------------------------------------
31 #include <re2/re2.h>
32 #include <re2/stringpiece.h>
33 #include <stdexcept>
35 class RegExp::Impl {
36 public:
37 Impl(const std::string &str, Case c, Syntax s) : _regex(str, opts(c, s)) {
38 if (!_regex.ok()) {
39 throw std::runtime_error(_regex.error());
43 std::string replace(std::string str, const std::string &replacement) {
44 RE2::GlobalReplace(&str, _regex, replacement);
45 return str;
48 bool match(const std::string &str) const {
49 return RE2::FullMatch(str, _regex);
52 bool search(const std::string &str) const {
53 return RE2::PartialMatch(str, _regex);
56 static std::string engine() { return "RE2"; }
58 private:
59 RE2 _regex;
61 static RE2::Options opts(Case c, Syntax s) {
62 RE2::Options options{RE2::Quiet};
63 options.set_case_sensitive(c == Case::respect);
64 options.set_literal(s == Syntax::literal);
65 return options;
69 #else
70 // -----------------------------------------------------------------------------
71 // standard <regex> implementation
72 // -----------------------------------------------------------------------------
73 #include <map>
74 #include <regex>
75 #include <sstream>
76 #include <vector>
77 class RegExp::Impl {
78 public:
79 Impl(const std::string &str, Case c, Syntax s)
80 : _regex(s == Syntax::literal
81 ? std::regex_replace(
82 str, std::regex(R"([.^$|()\[\]{}*+?\\])"), R"(\\&)",
83 std::regex_constants::format_sed)
84 : str,
85 c == Case::respect
86 ? std::regex::extended
87 : std::regex::extended | std::regex::icase) {}
89 std::string replace(const std::string &str,
90 const std::string &replacement) {
91 return std::regex_replace(str, _regex, replacement,
92 std::regex_constants::format_sed);
95 bool match(const std::string &str) const {
96 return regex_match(str, _regex);
99 bool search(const std::string &str) const {
100 return regex_search(str, _regex);
103 static std::string engine() { return "C++11"; }
105 private:
106 std::regex _regex;
108 #endif
110 // -----------------------------------------------------------------------------
111 // boilerplate pimpl code
112 // -----------------------------------------------------------------------------
114 RegExp::RegExp(const std::string &str, Case c, Syntax s)
115 : _impl(std::make_unique<Impl>(str, c, s)) {}
117 RegExp::~RegExp() = default;
119 RegExp::RegExp(RegExp &&rhs) noexcept = default;
121 RegExp &RegExp::operator=(RegExp &&rhs) noexcept = default;
123 std::string RegExp::replace(const std::string &str,
124 const std::string &replacement) const {
125 return _impl->replace(str, replacement);
128 bool RegExp::match(const std::string &str) const { return _impl->match(str); }
130 bool RegExp::search(const std::string &str) const { return _impl->search(str); }
132 // static
133 std::string RegExp::engine() { return Impl::engine(); }