+ use libicu in regexTools

This commit is contained in:
kelson42 2011-10-31 09:55:34 +00:00
parent 9c9b32f897
commit 9d371f295c
2 changed files with 17 additions and 14 deletions

View File

@ -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 &regex) { RegexMatcher *buildRegex(const std::string &regex) {
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(&regexStruct, 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(&regexStructure);
} }
bool matchRegex(const std::string &content, const std::string &regex) { bool matchRegex(const std::string &content, const std::string &regex) {
regex_t regexStructure = buildRegex(regex); ucnv_setDefaultName("UTF-8");
bool result = !regexec(&regexStructure, content.data(), 0, 0, 0); UnicodeString ucontent = UnicodeString(content.c_str());
return result; RegexMatcher *matcher = buildRegex(regex);
matcher->reset(ucontent);
return matcher->find();
} }

View File

@ -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 &regex); bool matchRegex(const std::string &content, const std::string &regex);