Fix bad implementation of regex library for include/exclude

Add support for using \uhhhh to match characters with regex.
Optimize codes.
This commit is contained in:
Tindy X committed 2020-07-07 12:51:58 +08:00
1 parent 41b7663cd8
commit 6629f861da
3 files changed
+9 -8

No files matched your search

+2 -2
View File
@@ -223,7 +223,7 @@ public:
if(do_utf8_to_gbk && is_str_utf8(content)) if(do_utf8_to_gbk && is_str_utf8(content))
content = UTF8ToACP(content); //do conversion if flag is set content = UTF8ToACP(content); //do conversion if flag is set
if(store_isolated_line) if(store_isolated_line && isolated_items_section.size())
{ {
curSection = isolated_items_section; //items before any section define will be store in this section curSection = isolated_items_section; //items before any section define will be store in this section
//check this section first //check this section first
@@ -384,7 +384,7 @@ public:
if(section != cached_section) if(section != cached_section)
{ {
cached_section = section; cached_section = section;
cached_section_content= ini_content.find(section); cached_section_content = ini_content.find(section);
} }
auto &cache = cached_section_content->second; auto &cache = cached_section_content->second;
return cache.find(itemName) != cache.end(); return cache.find(itemName) != cache.end();
+5 -4
View File
@@ -677,7 +677,7 @@ bool regMatch(const std::string &src, const std::string &match)
bool regFind(const std::string &src, const std::string &match) bool regFind(const std::string &src, const std::string &match)
{ {
jp::Regex reg; jp::Regex reg;
reg.setPattern(match).addModifier("m").addPcre2Option(PCRE2_UTF).compile(); reg.setPattern(match).addModifier("m").addPcre2Option(PCRE2_UTF|PCRE2_ALT_BSUX).compile();
if(!reg) if(!reg)
return false; return false;
return reg.match(src, "g"); return reg.match(src, "g");
@@ -686,7 +686,7 @@ bool regFind(const std::string &src, const std::string &match)
std::string regReplace(const std::string &src, const std::string &match, const std::string &rep, bool global) std::string regReplace(const std::string &src, const std::string &match, const std::string &rep, bool global)
{ {
jp::Regex reg; jp::Regex reg;
reg.setPattern(match).addModifier("m").addPcre2Option(PCRE2_UTF|PCRE2_MULTILINE).compile(); reg.setPattern(match).addModifier("m").addPcre2Option(PCRE2_UTF|PCRE2_MULTILINE|PCRE2_ALT_BSUX).compile();
if(!reg) if(!reg)
return src; return src;
return reg.replace(src, rep, global ? "gx" : "x"); return reg.replace(src, rep, global ? "gx" : "x");
@@ -694,14 +694,15 @@ std::string regReplace(const std::string &src, const std::string &match, const s
bool regValid(const std::string &reg) bool regValid(const std::string &reg)
{ {
jp::Regex r(reg); jp::Regex r;
r.setPattern(reg).addPcre2Option(PCRE2_UTF|PCRE2_ALT_BSUX).compile();
return !!r; return !!r;
} }
int regGetMatch(const std::string &src, const std::string &match, size_t group_count, ...) int regGetMatch(const std::string &src, const std::string &match, size_t group_count, ...)
{ {
jp::Regex reg; jp::Regex reg;
reg.setPattern(match).addModifier("m").addPcre2Option(PCRE2_UTF).compile(); reg.setPattern(match).addModifier("m").addPcre2Option(PCRE2_UTF|PCRE2_ALT_BSUX).compile();
jp::VecNum vec_num; jp::VecNum vec_num;
jp::RegexMatch rm; jp::RegexMatch rm;
size_t count = rm.setRegexObject(&reg).setSubject(src).setNumberedSubstringVector(&vec_num).setModifier("g").match(); size_t count = rm.setRegexObject(&reg).setSubject(src).setNumberedSubstringVector(&vec_num).setModifier("g").match();
+2 -2
View File
@@ -2002,7 +2002,7 @@ void filterNodes(std::vector<nodeInfo> &nodes, string_array &exclude_remarks, st
for(i = 0; i < exclude_remarks.size(); i++) for(i = 0; i < exclude_remarks.size(); i++)
{ {
std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)> pattern(pcre2_compile(reinterpret_cast<const unsigned char*>(exclude_remarks[i].c_str()), PCRE2_ZERO_TERMINATED | PCRE2_MULTILINE, 0, &errornumber, &erroroffset, NULL), &pcre2_code_free); std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)> pattern(pcre2_compile(reinterpret_cast<const unsigned char*>(exclude_remarks[i].c_str()), exclude_remarks[i].size(), PCRE2_UTF | PCRE2_MULTILINE | PCRE2_ALT_BSUX, &errornumber, &erroroffset, NULL), &pcre2_code_free);
if(!pattern) if(!pattern)
return; return;
exclude_patterns.emplace_back(std::move(pattern)); exclude_patterns.emplace_back(std::move(pattern));
@@ -2012,7 +2012,7 @@ void filterNodes(std::vector<nodeInfo> &nodes, string_array &exclude_remarks, st
} }
for(i = 0; i < include_remarks.size(); i++) for(i = 0; i < include_remarks.size(); i++)
{ {
std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)> pattern(pcre2_compile(reinterpret_cast<const unsigned char*>(include_remarks[i].c_str()), PCRE2_ZERO_TERMINATED | PCRE2_MULTILINE, 0, &errornumber, &erroroffset, NULL), &pcre2_code_free); std::unique_ptr<pcre2_code, decltype(&pcre2_code_free)> pattern(pcre2_compile(reinterpret_cast<const unsigned char*>(include_remarks[i].c_str()), include_remarks[i].size(), PCRE2_UTF | PCRE2_MULTILINE | PCRE2_ALT_BSUX, &errornumber, &erroroffset, NULL), &pcre2_code_free);
if(!pattern) if(!pattern)
return; return;
include_patterns.emplace_back(std::move(pattern)); include_patterns.emplace_back(std::move(pattern));