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"
|
||||
|
||||
std::map<std::string, regex_t> regexCache;
|
||||
std::map<std::string, RegexMatcher*> regexCache;
|
||||
|
||||
regex_t buildRegex(const std::string ®ex) {
|
||||
regex_t regexStruct;
|
||||
std::map<std::string, regex_t>::iterator itr = regexCache.find(regex);
|
||||
RegexMatcher *buildRegex(const std::string ®ex) {
|
||||
RegexMatcher *matcher;
|
||||
std::map<std::string, RegexMatcher*>::iterator itr = regexCache.find(regex);
|
||||
|
||||
/* Regex is in cache */
|
||||
if (itr != regexCache.end()) {
|
||||
regexStruct = itr->second;
|
||||
matcher = itr->second;
|
||||
}
|
||||
|
||||
/* Regex needs to be parsed (and cached) */
|
||||
else {
|
||||
regcomp(®exStruct, regex.data(), REG_ICASE);
|
||||
regexCache[regex] = regexStruct;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
UnicodeString uregex = UnicodeString(regex.c_str());
|
||||
matcher = new RegexMatcher(uregex, UREGEX_CASE_INSENSITIVE, status);
|
||||
regexCache[regex] = matcher;
|
||||
}
|
||||
|
||||
return regexStruct;
|
||||
return matcher;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
ucnv_setDefaultName("UTF-8");
|
||||
UnicodeString ucontent = UnicodeString(content.c_str());
|
||||
RegexMatcher *matcher = buildRegex(regex);
|
||||
matcher->reset(ucontent);
|
||||
return matcher->find();
|
||||
}
|
||||
|
|
|
@ -20,9 +20,9 @@
|
|||
#ifndef KIWIX_REGEXTOOLS_H
|
||||
#define KIWIX_REGEXTOOLS_H
|
||||
|
||||
#include <regex.h>
|
||||
#include <unicode/regex.h>
|
||||
#include <unicode/ucnv.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
bool matchRegex(const std::string &content, const std::string ®ex);
|
||||
|
|
Loading…
Reference in New Issue