mirror of
https://github.com/inspircd/inspircd.git
synced 2025-03-09 18:49:03 -04:00
m_regex_stdlib: A regex provider for the C++11 container std::regex
For it to work you need a standards compliant implementation as for instance Visual C++ 2010 and 2012 deliver. GCC's libstdc++ does not implement this class yet.
This commit is contained in:
parent
3f72ca011c
commit
3b63bc840f
@ -1388,6 +1388,17 @@
|
||||
# command: 'man 7 regex'.
|
||||
#<module name="m_regex_posix.so">
|
||||
|
||||
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
|
||||
# Regular Expression Provider for C++11 std::regex Regular Expressions.
|
||||
# This module works on any fully compliant implementation of the C++11
|
||||
# std::regex container. Examples for such are Visual C++ 2010 and newer
|
||||
# but not libstdc++ (which GCC uses)
|
||||
#<module name="m_regex_stdlib.so">
|
||||
|
||||
# Specify the Regular Expression engine to use here. Valid settings are
|
||||
# bre, ere, awk, grep, egrep, ecmascript (default if not specified)
|
||||
#<stdregex type="ecmascript">
|
||||
|
||||
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
|
||||
# Regular Expression Provider for TRE Regular Expressions.
|
||||
# This is the same regular expression engine used by UnrealIRCd, so
|
||||
|
112
src/modules/extra/m_regex_stdlib.cpp
Normal file
112
src/modules/extra/m_regex_stdlib.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* InspIRCd -- Internet Relay Chat Daemon
|
||||
*
|
||||
* Copyright (C) 2012 ChrisTX <chris@rev-crew.info>
|
||||
*
|
||||
* This file is part of InspIRCd. InspIRCd is free software: you can
|
||||
* redistribute it and/or modify it under the terms of the GNU General Public
|
||||
* License as published by the Free Software Foundation, version 2.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "inspircd.h"
|
||||
#include "m_regex.h"
|
||||
#include <regex>
|
||||
|
||||
/* $ModDesc: Regex Provider Module for std::regex Regular Expressions */
|
||||
/* $ModConfig: <stdregex type="ecmascript">
|
||||
* Specify the Regular Expression engine to use here. Valid settings are
|
||||
* bre, ere, awk, grep, egrep, ecmascript (default if not specified)*/
|
||||
/* $CompileFlags: -std=c++11 */
|
||||
/* $ModDep: m_regex.h */
|
||||
|
||||
class StdRegexException : public ModuleException
|
||||
{
|
||||
public:
|
||||
StdRegexException(const std::string& rx, const std::string& error)
|
||||
: ModuleException(std::string("Error in regex ") + rx + ": " + error)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class StdRegex : public Regex
|
||||
{
|
||||
private:
|
||||
std::regex regexcl;
|
||||
public:
|
||||
StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
|
||||
{
|
||||
try{
|
||||
regexcl.assign(rx, fltype | std::regex::optimize);
|
||||
}
|
||||
catch(std::regex_error rxerr)
|
||||
{
|
||||
throw StdRegexException(rx, rxerr.what());
|
||||
}
|
||||
}
|
||||
|
||||
virtual bool Matches(const std::string& text)
|
||||
{
|
||||
return std::regex_search(text, regexcl);
|
||||
}
|
||||
};
|
||||
|
||||
class StdRegexFactory : public RegexFactory
|
||||
{
|
||||
public:
|
||||
std::regex::flag_type regextype;
|
||||
StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
|
||||
Regex* Create(const std::string& expr)
|
||||
{
|
||||
return new StdRegex(expr, regextype);
|
||||
}
|
||||
};
|
||||
|
||||
class ModuleRegexStd : public Module
|
||||
{
|
||||
public:
|
||||
StdRegexFactory ref;
|
||||
ModuleRegexStd() : ref(this) {
|
||||
ServerInstance->Modules->AddService(ref);
|
||||
Implementation eventlist[] = { I_OnRehash };
|
||||
ServerInstance->Modules->Attach(eventlist, this, 1);
|
||||
OnRehash(NULL);
|
||||
}
|
||||
|
||||
Version GetVersion()
|
||||
{
|
||||
return Version("Regex Provider Module for std::regex", VF_VENDOR);
|
||||
}
|
||||
|
||||
void OnRehash(User* u)
|
||||
{
|
||||
ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
|
||||
std::string regextype = Conf->getString("type", "ecmascript");
|
||||
|
||||
if(regextype == "bre")
|
||||
ref.regextype = std::regex::basic;
|
||||
else if(regextype == "ere")
|
||||
ref.regextype = std::regex::extended;
|
||||
else if(regextype == "awk")
|
||||
ref.regextype = std::regex::awk;
|
||||
else if(regextype == "grep")
|
||||
ref.regextype = std::regex::grep;
|
||||
else if(regextype == "egrep")
|
||||
ref.regextype = std::regex::egrep;
|
||||
else
|
||||
{
|
||||
if(regextype != "ecmascript")
|
||||
ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Non-existent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
|
||||
ref.regextype = std::regex::ECMAScript;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MODULE_INIT(ModuleRegexStd)
|
Loading…
x
Reference in New Issue
Block a user