OSDN Git Service

Fix issue #784: Error on try to show differences between two different gif
[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()
19 : m_lastMatchExpression(nullptr)
20 {
21 }
22
23 /** 
24  * @brief Destructor.
25  */
26 FilterList::~FilterList()
27 {
28         RemoveAllFilters();
29 }
30
31 /** 
32  * @brief Add new regular expression to the list.
33  * This function adds new regular expression to the list of expressions.
34  * The regular expression is compiled and studied for better performance.
35  * @param [in] regularExpression Regular expression string.
36  * @param [in] encoding Expression encoding.
37  */
38 void FilterList::AddRegExp(const std::string& regularExpression)
39 {
40         try
41         {
42                 m_list.push_back(filter_item_ptr(new filter_item(regularExpression, RegularExpression::RE_UTF8)));
43         }
44         catch (...)
45         {
46                 // TODO:
47         }
48 }
49
50 /** 
51  * @brief Match string against list of expressions.
52  * This function matches given @p string against the list of regular
53  * expressions. The matching ends when first match is found, so all
54  * expressions may not be matched against.
55  * @param [in] string string to match.
56  * @param [in] codepage codepage of string.
57  * @return true if any of the expressions did match the string.
58  */
59 bool FilterList::Match(const std::string& string, int codepage/*=CP_UTF8*/)
60 {
61         bool retval = false;
62         const size_t count = m_list.size();
63
64         // convert string into UTF-8
65         ucr::buffer buf(string.length() * 2);
66
67         if (codepage != ucr::CP_UTF_8)
68                         ucr::convert(ucr::NONE, codepage, reinterpret_cast<const unsigned char *>(string.c_str()), 
69                                         string.length(), ucr::UTF8, ucr::CP_UTF_8, &buf);
70
71         unsigned i = 0;
72         while (i < count && !retval)
73         {
74                 const filter_item_ptr& item = m_list[i];
75                 int result = 0;
76                 RegularExpression::Match match;
77                 try
78                 {
79                         if (buf.size > 0)
80                                 result = item->regexp.match(std::string(reinterpret_cast<const char *>(buf.ptr), buf.size), 0, match);
81                         else
82                                 result = item->regexp.match(string, 0, match);
83                 }
84                 catch (...)
85                 {
86                         // TODO:
87                 }
88                 if (result > 0)
89                 {
90                         m_lastMatchExpression = &item->filterAsString;
91                         retval = true;
92                 }
93                 else
94                         ++i;
95         }
96
97         return retval;
98 }
99