From 0e7f74a7c8e804a0223b4d88bf637649838f0412 Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Tue, 27 Aug 2013 07:29:13 +0100 Subject: Make all regex modules throw the same exception on error. --- include/modules/regex.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'include') diff --git a/include/modules/regex.h b/include/modules/regex.h index 875f942bc..e278df502 100644 --- a/include/modules/regex.h +++ b/include/modules/regex.h @@ -53,3 +53,13 @@ class RegexFactory : public DataProvider virtual Regex* Create(const std::string& expr) = 0; }; + +class RegexException : public ModuleException +{ + public: + RegexException(const std::string& regex, const std::string& error) + : ModuleException("Error in regex '" + regex + "': " + error) { } + + RegexException(const std::string& regex, const std::string& error, int offset) + : ModuleException("Error in regex '" + regex + "' at offset " + ConvToStr(offset) + ": " + error) { } +}; -- cgit v1.2.3 From eaf658de3d1ef984c9a0b4273a9cfbd3029f8b5b Mon Sep 17 00:00:00 2001 From: Peter Powell Date: Tue, 27 Aug 2013 07:54:16 +0100 Subject: Fix various small issues. - Add CXX11_OVERRIDE to *Regex::Matches and *RegexFactory::Create. - Fix documentation comment on regex_string. - Fix various code duplication/layout issues. --- include/modules/regex.h | 13 +++++-------- src/modules/extra/m_regex_pcre.cpp | 11 +++-------- src/modules/extra/m_regex_posix.cpp | 11 +++-------- src/modules/extra/m_regex_re2.cpp | 4 ++-- src/modules/extra/m_regex_stdlib.cpp | 4 ++-- src/modules/extra/m_regex_tre.cpp | 11 +++-------- src/modules/m_regex_glob.cpp | 4 ++-- 7 files changed, 20 insertions(+), 38 deletions(-) (limited to 'include') diff --git a/include/modules/regex.h b/include/modules/regex.h index e278df502..0bced4e2b 100644 --- a/include/modules/regex.h +++ b/include/modules/regex.h @@ -25,18 +25,15 @@ class Regex : public classbase { protected: - std::string regex_string; // The raw uncompiled regex string. + /** The uncompiled regex string. */ + std::string regex_string; // Constructor may as well be protected, as this class is abstract. - Regex(const std::string& rx) : regex_string(rx) - { - } + Regex(const std::string& rx) : regex_string(rx) { } public: - virtual ~Regex() - { - } + virtual ~Regex() { } virtual bool Matches(const std::string& text) = 0; @@ -49,7 +46,7 @@ public: class RegexFactory : public DataProvider { public: - RegexFactory(Module* Creator, const std::string& Name) : DataProvider(Creator, Name) {} + RegexFactory(Module* creator, const std::string& name) : DataProvider(creator, name) { } virtual Regex* Create(const std::string& expr) = 0; }; diff --git a/src/modules/extra/m_regex_pcre.cpp b/src/modules/extra/m_regex_pcre.cpp index 01cf47178..91c2d1404 100644 --- a/src/modules/extra/m_regex_pcre.cpp +++ b/src/modules/extra/m_regex_pcre.cpp @@ -51,14 +51,9 @@ class PCRERegex : public Regex pcre_free(regex); } - bool Matches(const std::string& text) + bool Matches(const std::string& text) CXX11_OVERRIDE { - if (pcre_exec(regex, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1) - { - // Bang. :D - return true; - } - return false; + return (pcre_exec(regex, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) >= 0); } }; @@ -66,7 +61,7 @@ class PCREFactory : public RegexFactory { public: PCREFactory(Module* m) : RegexFactory(m, "regex/pcre") {} - Regex* Create(const std::string& expr) + Regex* Create(const std::string& expr) CXX11_OVERRIDE { return new PCRERegex(expr); } diff --git a/src/modules/extra/m_regex_posix.cpp b/src/modules/extra/m_regex_posix.cpp index 6c52a935b..935cdbf92 100644 --- a/src/modules/extra/m_regex_posix.cpp +++ b/src/modules/extra/m_regex_posix.cpp @@ -54,14 +54,9 @@ class POSIXRegex : public Regex regfree(®buf); } - bool Matches(const std::string& text) + bool Matches(const std::string& text) CXX11_OVERRIDE { - if (regexec(®buf, text.c_str(), 0, NULL, 0) == 0) - { - // Bang. :D - return true; - } - return false; + return (regexec(®buf, text.c_str(), 0, NULL, 0) == 0); } }; @@ -70,7 +65,7 @@ class PosixFactory : public RegexFactory public: bool extended; PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {} - Regex* Create(const std::string& expr) + Regex* Create(const std::string& expr) CXX11_OVERRIDE { return new POSIXRegex(expr, extended); } diff --git a/src/modules/extra/m_regex_re2.cpp b/src/modules/extra/m_regex_re2.cpp index b97b7ec0f..2525b70ab 100644 --- a/src/modules/extra/m_regex_re2.cpp +++ b/src/modules/extra/m_regex_re2.cpp @@ -43,7 +43,7 @@ class RE2Regex : public Regex } } - bool Matches(const std::string& text) + bool Matches(const std::string& text) CXX11_OVERRIDE { return RE2::FullMatch(text, regexcl); } @@ -53,7 +53,7 @@ class RE2Factory : public RegexFactory { public: RE2Factory(Module* m) : RegexFactory(m, "regex/re2") { } - Regex* Create(const std::string& expr) + Regex* Create(const std::string& expr) CXX11_OVERRIDE { return new RE2Regex(expr); } diff --git a/src/modules/extra/m_regex_stdlib.cpp b/src/modules/extra/m_regex_stdlib.cpp index 2fa1ae090..5ec358d58 100644 --- a/src/modules/extra/m_regex_stdlib.cpp +++ b/src/modules/extra/m_regex_stdlib.cpp @@ -38,7 +38,7 @@ class StdRegex : public Regex } } - bool Matches(const std::string& text) + bool Matches(const std::string& text) CXX11_OVERRIDE { return std::regex_search(text, regexcl); } @@ -49,7 +49,7 @@ class StdRegexFactory : public RegexFactory public: std::regex::flag_type regextype; StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {} - Regex* Create(const std::string& expr) + Regex* Create(const std::string& expr) CXX11_OVERRIDE { return new StdRegex(expr, regextype); } diff --git a/src/modules/extra/m_regex_tre.cpp b/src/modules/extra/m_regex_tre.cpp index c845ab374..92f2ad990 100644 --- a/src/modules/extra/m_regex_tre.cpp +++ b/src/modules/extra/m_regex_tre.cpp @@ -57,14 +57,9 @@ public: regfree(®buf); } - bool Matches(const std::string& text) + bool Matches(const std::string& text) CXX11_OVERRIDE { - if (regexec(®buf, text.c_str(), 0, NULL, 0) == 0) - { - // Bang. :D - return true; - } - return false; + return (regexec(®buf, text.c_str(), 0, NULL, 0) == 0); } }; @@ -72,7 +67,7 @@ class TREFactory : public RegexFactory { public: TREFactory(Module* m) : RegexFactory(m, "regex/tre") {} - Regex* Create(const std::string& expr) + Regex* Create(const std::string& expr) CXX11_OVERRIDE { return new TRERegex(expr); } diff --git a/src/modules/m_regex_glob.cpp b/src/modules/m_regex_glob.cpp index 887b1cd79..eb7cf4ece 100644 --- a/src/modules/m_regex_glob.cpp +++ b/src/modules/m_regex_glob.cpp @@ -28,7 +28,7 @@ public: { } - bool Matches(const std::string& text) + bool Matches(const std::string& text) CXX11_OVERRIDE { return InspIRCd::Match(text, this->regex_string); } @@ -37,7 +37,7 @@ public: class GlobFactory : public RegexFactory { public: - Regex* Create(const std::string& expr) + Regex* Create(const std::string& expr) CXX11_OVERRIDE { return new GlobRegex(expr); } -- cgit v1.2.3