mirror of https://github.com/kiwix/libkiwix.git
+ use libicu in regexTools
This commit is contained in:
parent
9c9b32f897
commit
9d371f295c
|
@ -19,33 +19,36 @@
|
||||||
|
|
||||||
#include "regexTools.h"
|
#include "regexTools.h"
|
||||||
|
|
||||||
std::map<std::string, regex_t> regexCache;
|
std::map<std::string, RegexMatcher*> regexCache;
|
||||||
|
|
||||||
regex_t buildRegex(const std::string ®ex) {
|
RegexMatcher *buildRegex(const std::string ®ex) {
|
||||||
regex_t regexStruct;
|
RegexMatcher *matcher;
|
||||||
std::map<std::string, regex_t>::iterator itr = regexCache.find(regex);
|
std::map<std::string, RegexMatcher*>::iterator itr = regexCache.find(regex);
|
||||||
|
|
||||||
/* Regex is in cache */
|
/* Regex is in cache */
|
||||||
if (itr != regexCache.end()) {
|
if (itr != regexCache.end()) {
|
||||||
regexStruct = itr->second;
|
matcher = itr->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Regex needs to be parsed (and cached) */
|
/* Regex needs to be parsed (and cached) */
|
||||||
else {
|
else {
|
||||||
regcomp(®exStruct, regex.data(), REG_ICASE);
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
regexCache[regex] = regexStruct;
|
UnicodeString uregex = UnicodeString(regex.c_str());
|
||||||
|
matcher = new RegexMatcher(uregex, UREGEX_CASE_INSENSITIVE, status);
|
||||||
|
regexCache[regex] = matcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
return regexStruct;
|
return matcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* todo */
|
/* todo */
|
||||||
void freeRegexCache() {
|
void freeRegexCache() {
|
||||||
//regfree(®exStructure);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool matchRegex(const std::string &content, const std::string ®ex) {
|
bool matchRegex(const std::string &content, const std::string ®ex) {
|
||||||
regex_t regexStructure = buildRegex(regex);
|
ucnv_setDefaultName("UTF-8");
|
||||||
bool result = !regexec(®exStructure, content.data(), 0, 0, 0);
|
UnicodeString ucontent = UnicodeString(content.c_str());
|
||||||
return result;
|
RegexMatcher *matcher = buildRegex(regex);
|
||||||
|
matcher->reset(ucontent);
|
||||||
|
return matcher->find();
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,9 +20,9 @@
|
||||||
#ifndef KIWIX_REGEXTOOLS_H
|
#ifndef KIWIX_REGEXTOOLS_H
|
||||||
#define KIWIX_REGEXTOOLS_H
|
#define KIWIX_REGEXTOOLS_H
|
||||||
|
|
||||||
#include <regex.h>
|
#include <unicode/regex.h>
|
||||||
|
#include <unicode/ucnv.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
bool matchRegex(const std::string &content, const std::string ®ex);
|
bool matchRegex(const std::string &content, const std::string ®ex);
|
||||||
|
|
Loading…
Reference in New Issue