OSDN Git Service

Update TranslationsStatus
[winmerge-jp/winmerge-jp.git] / Src / FilterList.cpp
1 /** 
2  * @file  FilterList.h
3  *
4  * @brief Implementation file for FilterList.
5  */
6
7 #include "pch.h"
8 #include "FilterList.h"
9 #include <vector>
10 #include <Poco/RegularExpression.h>
11 #include "unicoder.h"
12
13 using Poco::RegularExpression;
14
15 /** 
16  * @brief Constructor.
17  */
18 FilterList::FilterList() = default;
19
20 /** 
21  * @brief Destructor.
22  */
23 FilterList::~FilterList()
24 {
25         RemoveAllFilters();
26 }
27
28 /** 
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 
35  */
36 void FilterList::AddRegExp(const std::string& regularExpression, bool exclude)
37 {
38         try
39         {
40                 auto& list = exclude ? m_listExclude : m_list;
41                 list.push_back(filter_item_ptr(new filter_item(regularExpression, RegularExpression::RE_UTF8)));
42         }
43         catch (...)
44         {
45                 // TODO:
46         }
47 }
48
49 /** 
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.
57  */
58 bool FilterList::Match(const std::string& string, int codepage/*=CP_UTF8*/)
59 {
60         const size_t count = m_list.size();
61         bool retval = m_list.size() == 0;
62
63         // convert string into UTF-8
64         ucr::buffer buf(string.length() * 2);
65
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);
69
70         unsigned i = 0;
71         while (i < count && !retval)
72         {
73                 const filter_item_ptr& item = m_list[i];
74                 int result = 0;
75                 RegularExpression::Match match;
76                 try
77                 {
78                         if (buf.size > 0)
79                                 result = item->regexp.match(std::string(reinterpret_cast<const char *>(buf.ptr), buf.size), 0, match);
80                         else
81                                 result = item->regexp.match(string, 0, match);
82                 }
83                 catch (...)
84                 {
85                         // TODO:
86                 }
87                 if (result > 0)
88                 {
89                         retval = true;
90                 }
91                 else
92                         ++i;
93         }
94
95         if (!retval)
96                 return retval;
97
98         i = 0;
99         const size_t countExclude = m_listExclude.size();
100         while (i < countExclude && retval)
101         {
102                 const filter_item_ptr& item = m_listExclude[i];
103                 int result = 0;
104                 RegularExpression::Match match;
105                 try
106                 {
107                         if (buf.size > 0)
108                                 result = item->regexp.match(std::string(reinterpret_cast<const char *>(buf.ptr), buf.size), 0, match);
109                         else
110                                 result = item->regexp.match(string, 0, match);
111                 }
112                 catch (...)
113                 {
114                         // TODO:
115                 }
116                 if (result > 0)
117                 {
118                         retval = false;
119                 }
120                 else
121                         ++i;
122         }
123
124         return retval;
125 }
126
127 /**
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.
132  */
133 void FilterList::CloneFrom(const FilterList* filterList)
134 {
135         if (!filterList)
136                 return;
137
138         m_list.clear();
139         m_listExclude.clear();
140
141         size_t count = filterList->m_list.size();
142         for (size_t i = 0; i < count; i++)
143         {
144                 m_list.emplace_back(std::make_shared<filter_item>(filterList->m_list[i].get()));
145         }
146         size_t countExclude = filterList->m_listExclude.size();
147         for (size_t i = 0; i < countExclude; i++)
148         {
149                 m_listExclude.emplace_back(std::make_shared<filter_item>(filterList->m_listExclude[i].get()));
150         }
151 }