2 * @file SubstitutionList.cpp
4 * @brief Implementation file for SubstitutionList.
8 #include "SubstitutionList.h"
10 #include <Poco/RegularExpression.h>
13 SubstitutionItem::SubstitutionItem(const std::string& pattern,
14 const std::string& replacement, int regexpCompileOptions)
16 , replacement(replacement)
17 , regexpCompileOptions(regexpCompileOptions)
18 , regexp(pattern, regexpCompileOptions)
22 SubstitutionItem::SubstitutionItem(const SubstitutionItem& other)
23 : pattern(other.pattern)
24 , replacement(other.replacement)
25 , regexpCompileOptions(other.regexpCompileOptions)
26 , regexp(other.pattern, other.regexpCompileOptions)
30 void SubstitutionList::Add(const std::string& pattern, const std::string& replacement, int regexpCompileOptions)
32 m_list.emplace_back(pattern, replacement, regexpCompileOptions);
35 void SubstitutionList::Add(
36 const std::string& pattern, const std::string& replacement,
37 bool caseSensitive, bool matchWholeWordOnly)
39 int regexpCompileOptions =
40 caseSensitive ? 0 : Poco::RegularExpression::RE_CASELESS;
41 std::string rePattern;
46 case '\\': case '.': case '^': case '$': case '|':
47 case '[': case ']': case '(': case ')': case '!':
48 case '?': case '*': case '+': case '{': case '}':
49 rePattern.push_back('\\');
54 rePattern.push_back(c);
56 if (matchWholeWordOnly)
57 rePattern = "\b" + rePattern + "\b";
58 m_list.emplace_back(rePattern, replacement, regexpCompileOptions);
61 std::string SubstitutionList::Subst(const std::string& subject, int codepage/*=CP_UTF8*/) const
65 if (codepage != ucr::CP_UTF_8)
67 // convert string into UTF-8
68 ucr::buffer buf(subject.length() * 2);
70 ucr::convert(ucr::NONE, codepage, reinterpret_cast<const unsigned char*>(subject.c_str()),
71 subject.length(), ucr::UTF8, ucr::CP_UTF_8, &buf);
73 replaced.assign(reinterpret_cast<const char *>(buf.ptr), buf.size);
80 for (const auto& item : m_list)
84 item.regexp.subst(replaced, item.replacement, Poco::RegularExpression::RE_GLOBAL);
95 void SubstitutionList::RemoveAllFilters()