diff --git a/src/common/kiwix/manager.h b/src/common/kiwix/manager.h index d3f738cd2..a38b7b0aa 100644 --- a/src/common/kiwix/manager.h +++ b/src/common/kiwix/manager.h @@ -20,7 +20,6 @@ #ifndef KIWIX_MANAGER_H #define KIWIX_MANAGER_H -#include #include #include #include diff --git a/src/common/regexTools.cpp b/src/common/regexTools.cpp index 6413a2190..202c55821 100644 --- a/src/common/regexTools.cpp +++ b/src/common/regexTools.cpp @@ -19,12 +19,33 @@ #include "regexTools.h" -bool matchRegex(const std::string &content, const std::string regex) { - regex_t regexp; - bool result = false; +std::map regexCache; + +regex_t buildRegex(const std::string ®ex) { + regex_t regexStruct; + std::map::iterator itr = regexCache.find(regex); - regcomp(®exp, regex.data(), REG_ICASE); - result = !regexec(®exp, content.data(), 0, 0, 0); - regfree(®exp); + /* Regex is in cache */ + if (itr != regexCache.end()) { + regexStruct = itr->second; + } + + /* Regex needs to be parsed (and cached) */ + else { + regcomp(®exStruct, regex.data(), REG_ICASE); + regexCache[regex] = regexStruct; + } + + return regexStruct; +} + +/* todo */ +void freeRegexCache() { + //regfree(®exStructure); +} + +bool matchRegex(const std::string &content, const std::string ®ex) { + regex_t regexStructure = buildRegex(regex); + bool result = !regexec(®exStructure, content.data(), 0, 0, 0); return result; } diff --git a/src/common/regexTools.h b/src/common/regexTools.h index e43b79b8d..3e4443d10 100644 --- a/src/common/regexTools.h +++ b/src/common/regexTools.h @@ -23,7 +23,8 @@ #include #include #include +#include -bool matchRegex(const std::string &content, const std::string regex); +bool matchRegex(const std::string &content, const std::string ®ex); #endif