1 // +------------------------------------------------------------------+
2 // | ____ _ _ __ __ _ __ |
3 // | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 // | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 // | | |___| | | | __/ (__| < | | | | . \ |
6 // | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
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.
28 // -----------------------------------------------------------------------------
30 // -----------------------------------------------------------------------------
32 #include <re2/stringpiece.h>
37 Impl(const std::string
&str
, Case c
, Syntax s
) : _regex(str
, opts(c
, s
)) {
39 throw std::runtime_error(_regex
.error());
43 std::string
replace(std::string str
, const std::string
&replacement
) {
44 RE2::GlobalReplace(&str
, _regex
, replacement
);
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"; }
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
);
70 // -----------------------------------------------------------------------------
71 // standard <regex> implementation
72 // -----------------------------------------------------------------------------
79 Impl(const std::string
&str
, Case c
, Syntax s
)
80 : _regex(s
== Syntax::literal
82 str
, std::regex(R
"([.^$|()\[\]{}*+?\\])"), R
"(\\&)",
83 std::regex_constants::format_sed
)
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"; }
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
); }
133 std::string
RegExp::engine() { return Impl::engine(); }