4 * @brief Implementation file for FilterList.
8 #include "FilterList.h"
10 #include <Poco/RegularExpression.h>
13 using Poco::RegularExpression;
18 FilterList::FilterList() = default;
23 FilterList::~FilterList()
29 * @brief Add new regular expression to the list.
30 * This function adds new regular expression to the list of expressions.
31 * The regular expression is compiled and studied for better performance.
32 * @param [in] regularExpression Regular expression string.
33 * @param [in] encoding Expression encoding.
34 * @param [in] excluded
36 void FilterList::AddRegExp(const std::string& regularExpression, bool exclude)
40 auto& list = exclude ? m_listExclude : m_list;
41 list.push_back(filter_item_ptr(new filter_item(regularExpression, RegularExpression::RE_UTF8)));
50 * @brief Match string against list of expressions.
51 * This function matches given @p string against the list of regular
52 * expressions. The matching ends when first match is found, so all
53 * expressions may not be matched against.
54 * @param [in] string string to match.
55 * @param [in] codepage codepage of string.
56 * @return true if any of the expressions did match the string.
58 bool FilterList::Match(const std::string& string, int codepage/*=CP_UTF8*/)
60 const size_t count = m_list.size();
61 bool retval = m_list.size() == 0;
63 // convert string into UTF-8
64 ucr::buffer buf(string.length() * 2);
66 if (codepage != ucr::CP_UTF_8)
67 ucr::convert(ucr::NONE, codepage, reinterpret_cast<const unsigned char *>(string.c_str()),
68 string.length(), ucr::UTF8, ucr::CP_UTF_8, &buf);
71 while (i < count && !retval)
73 const filter_item_ptr& item = m_list[i];
75 RegularExpression::Match match;
79 result = item->regexp.match(std::string(reinterpret_cast<const char *>(buf.ptr), buf.size), 0, match);
81 result = item->regexp.match(string, 0, match);
99 const size_t countExclude = m_listExclude.size();
100 while (i < countExclude && retval)
102 const filter_item_ptr& item = m_listExclude[i];
104 RegularExpression::Match match;
108 result = item->regexp.match(std::string(reinterpret_cast<const char *>(buf.ptr), buf.size), 0, match);
110 result = item->regexp.match(string, 0, match);
128 * @brief Clone filter list from another list.
129 * This function clones filter list from another list. Current items in the
130 * list are removed and new items added from the given list.
131 * @param [in] filterList File list to clone.
133 void FilterList::CloneFrom(const FilterList* filterList)
139 m_listExclude.clear();
141 size_t count = filterList->m_list.size();
142 for (size_t i = 0; i < count; i++)
144 m_list.emplace_back(std::make_shared<filter_item>(filterList->m_list[i].get()));
146 size_t countExclude = filterList->m_listExclude.size();
147 for (size_t i = 0; i < countExclude; i++)
149 m_listExclude.emplace_back(std::make_shared<filter_item>(filterList->m_listExclude[i].get()));