Module: check_mk
Branch: master
Commit: 5bad2b1ce9cb410f584fb12e4fd55572c9b26a91
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=5bad2b1ce9cb41…
Author: Sven Panne <sp(a)mathias-kettner.de>
Date: Tue Feb 13 10:04:33 2018 +0100
Really use RE2 if available.
Change-Id: I973f340069529aec740c02fe2c158592c841e514
---
livestatus/src/RegExp.cc | 65 ++++++++++++++++++++++++++++++++----------------
livestatus/src/RegExp.h | 8 +++---
2 files changed, 48 insertions(+), 25 deletions(-)
diff --git a/livestatus/src/RegExp.cc b/livestatus/src/RegExp.cc
index 02cf401..bac70a7 100644
--- a/livestatus/src/RegExp.cc
+++ b/livestatus/src/RegExp.cc
@@ -24,19 +24,52 @@
#include "RegExp.h"
-// Currently just for testing purposes, the real stuff will follow soon...
#ifdef HAVE_RE2
+// -----------------------------------------------------------------------------
+// RE2 implementation
+// -----------------------------------------------------------------------------
#include <re2/re2.h>
-#endif
+#include "re2/stringpiece.h"
+#include <stdexcept>
-#include <regex>
+class RegExp::Impl {
+public:
+ Impl(const std::string &str, Case c) : _regex(str, opts(c)) {
+ if (!_regex.ok()) {
+ throw std::runtime_error(_regex.error());
+ }
+ }
+
+ std::string replace(std::string str, const std::string &replacement) {
+ RE2::GlobalReplace(&str, _regex, replacement);
+ return str;
+ }
+ bool search(const std::string &str) const {
+ return RE2::PartialMatch(str, _regex);
+ }
+
+private:
+ RE2 _regex;
+
+ static RE2::Options opts(Case c) {
+ RE2::Options options{RE2::Quiet};
+ options.set_case_sensitive(c == Case::respect);
+ return options;
+ }
+};
+
+#else
+// -----------------------------------------------------------------------------
+// standard <regex> implementation
+// -----------------------------------------------------------------------------
+#include <regex>
class RegExp::Impl {
public:
Impl(const std::string &str, Case c)
- : _regex(str, c == Case::ignore
- ? std::regex::extended | std::regex::icase
- : std::regex::extended) {}
+ : _regex(str, c == Case::respect
+ ? std::regex::extended
+ : std::regex::extended | std::regex::icase) {}
std::string replace(const std::string &str,
const std::string &replacement) {
@@ -51,29 +84,19 @@ public:
private:
std::regex _regex;
};
+#endif
+
+// -----------------------------------------------------------------------------
+// boilerplate pimpl code
+// -----------------------------------------------------------------------------
RegExp::RegExp(const std::string &str, Case c)
: _impl(std::make_unique<Impl>(str, c)) {}
RegExp::~RegExp() = default;
-RegExp::RegExp(const RegExp &rhs) noexcept
- : _impl(std::make_unique<Impl>(*rhs._impl)) {}
-
-RegExp &RegExp::operator=(const RegExp &rhs) noexcept {
- *_impl = *rhs._impl;
- return *this;
-}
-
-RegExp::RegExp(RegExp &&rhs) noexcept = default;
-
-RegExp &RegExp::operator=(RegExp &&rhs) noexcept = default;
-
std::string RegExp::replace(const std::string &str,
const std::string &replacement) const {
-#ifdef HAVE_RE2
- RE2 dummyRegExpJustForTesting{".*"};
-#endif
return _impl->replace(str, replacement);
}
diff --git a/livestatus/src/RegExp.h b/livestatus/src/RegExp.h
index d9d2aa0..216e642 100644
--- a/livestatus/src/RegExp.h
+++ b/livestatus/src/RegExp.h
@@ -38,10 +38,10 @@ public:
// the implementation file."
RegExp(const std::string &str, Case c);
~RegExp();
- RegExp(const RegExp &rhs) noexcept;
- RegExp &operator=(const RegExp &rhs) noexcept;
- RegExp(RegExp &&rhs) noexcept;
- RegExp &operator=(RegExp &&rhs) noexcept;
+ RegExp(const RegExp &rhs) = delete;
+ RegExp &operator=(const RegExp &rhs) = delete;
+ RegExp(RegExp &&rhs) = delete;
+ RegExp &operator=(RegExp &&rhs) = delete;
std::string replace(const std::string &str,
const std::string &replacement) const;