mirror of https://github.com/kiwix/libkiwix.git
+ add regex cache
This commit is contained in:
parent
a1dfb3c9fd
commit
72b8527654
|
@ -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>
|
||||||
|
|
|
@ -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 ®ex) {
|
||||||
|
regex_t regexStruct;
|
||||||
|
std::map<std::string, regex_t>::iterator itr = regexCache.find(regex);
|
||||||
|
|
||||||
regcomp(®exp, regex.data(), REG_ICASE);
|
/* Regex is in cache */
|
||||||
result = !regexec(®exp, content.data(), 0, 0, 0);
|
if (itr != regexCache.end()) {
|
||||||
regfree(®exp);
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 ®ex);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue