]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_re2.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_re2.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Peter Powell <petpow@saberuk.com>
5  *   Copyright (C) 2012 ChrisTX <chris@rev-crew.info>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/regex.h"
23
24 // Fix warnings about the use of `long long` on C++03 and
25 // shadowing on GCC.
26 #if defined __clang__
27 # pragma clang diagnostic ignored "-Wc++11-long-long"
28 #elif defined __GNUC__
29 # pragma GCC diagnostic ignored "-Wlong-long"
30 # pragma GCC diagnostic ignored "-Wshadow"
31 #endif
32
33 #include <re2/re2.h>
34
35 /* $LinkerFlags: -lre2 */
36
37 class RE2Regex : public Regex
38 {
39         RE2 regexcl;
40
41  public:
42         RE2Regex(const std::string& rx) : Regex(rx), regexcl(rx, RE2::Quiet)
43         {
44                 if (!regexcl.ok())
45                 {
46                         throw RegexException(rx, regexcl.error());
47                 }
48         }
49
50         bool Matches(const std::string& text) CXX11_OVERRIDE
51         {
52                 return RE2::FullMatch(text, regexcl);
53         }
54 };
55
56 class RE2Factory : public RegexFactory
57 {
58  public:
59         RE2Factory(Module* m) : RegexFactory(m, "regex/re2") { }
60         Regex* Create(const std::string& expr) CXX11_OVERRIDE
61         {
62                 return new RE2Regex(expr);
63         }
64 };
65
66 class ModuleRegexRE2 : public Module
67 {
68         RE2Factory ref;
69
70  public:
71         ModuleRegexRE2() : ref(this)
72         {
73         }
74
75         Version GetVersion() CXX11_OVERRIDE
76         {
77                 return Version("Regex Provider Module for RE2", VF_VENDOR);
78         }
79 };
80
81 MODULE_INIT(ModuleRegexRE2)