+ add regex cache

This commit is contained in:
kelson42 2011-10-23 14:16:03 +00:00
parent a1dfb3c9fd
commit 72b8527654
3 changed files with 29 additions and 8 deletions

View File

@ -20,7 +20,6 @@
#ifndef KIWIX_MANAGER_H #ifndef KIWIX_MANAGER_H
#define KIWIX_MANAGER_H #define KIWIX_MANAGER_H
#include <map>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <time.h> #include <time.h>

View File

@ -19,12 +19,33 @@
#include "regexTools.h" #include "regexTools.h"
bool matchRegex(const std::string &content, const std::string regex) { std::map<std::string, regex_t> regexCache;
regex_t regexp;
bool result = false; regex_t buildRegex(const std::string &regex) {
regex_t regexStruct;
std::map<std::string, regex_t>::iterator itr = regexCache.find(regex);
regcomp(&regexp, regex.data(), REG_ICASE); /* Regex is in cache */
result = !regexec(&regexp, content.data(), 0, 0, 0); if (itr != regexCache.end()) {
regfree(&regexp); regexStruct = itr->second;
}
/* Regex needs to be parsed (and cached) */
else {
regcomp(&regexStruct, regex.data(), REG_ICASE);
regexCache[regex] = regexStruct;
}
return regexStruct;
}
/* todo */
void freeRegexCache() {
//regfree(&regexStructure);
}
bool matchRegex(const std::string &content, const std::string &regex) {
regex_t regexStructure = buildRegex(regex);
bool result = !regexec(&regexStructure, content.data(), 0, 0, 0);
return result; return result;
} }

View File

@ -23,7 +23,8 @@
#include <regex.h> #include <regex.h>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <map>
bool matchRegex(const std::string &content, const std::string regex); bool matchRegex(const std::string &content, const std::string &regex);
#endif #endif