OSDN Git Service

compiler-calculated maximum value for `m_SourceDefs` (#966)
[winmerge-jp/winmerge-jp.git] / Src / FilterList.h
1 /**
2  * @file  FilterList.h
3  *
4  * @brief Declaration file for FilterList.
5  */
6 #pragma once
7
8 #include <vector>
9 #include <memory>
10 #include <Poco/RegularExpression.h>
11 #include "unicoder.h"
12
13 /**
14  * @brief Container for one filtering rule / compiled expression.
15  * This structure holds compiled regular expression and a original expression
16  * as a string. We need the original expression string in case we want to
17  * know which regular expression did match.
18  */
19 struct filter_item
20 {
21         std::string filterAsString; /** Original regular expression string */
22         Poco::RegularExpression regexp; /**< Compiled regular expression */
23         filter_item(const std::string &filter, int reOpts) : filterAsString(filter), regexp(filter, reOpts) {}
24 };
25
26 typedef std::shared_ptr<filter_item> filter_item_ptr;
27
28 /**
29  * @brief Regular expression list.
30  * This class holds a list of regular expressions for matching strings.
31  * The class also provides simple function for matching and remembers the
32  * last matched expression.
33  */
34 class FilterList
35 {
36 public:
37         FilterList();
38         ~FilterList();
39         
40         void AddRegExp(const std::string& regularExpression);
41         void RemoveAllFilters();
42         bool HasRegExps() const;
43         bool Match(const std::string& string, int codepage = ucr::CP_UTF_8);
44         const char * GetLastMatchExpression() const;
45
46 private:
47         std::vector <filter_item_ptr> m_list;
48         const std::string *m_lastMatchExpression;
49
50 };
51
52 /** 
53  * @brief Removes all expressions from the list.
54  */
55 inline void FilterList::RemoveAllFilters()
56 {
57         m_list.clear();
58 }
59
60 /** 
61  * @brief Returns if list has any expressions.
62  * @return true if list contains one or more expressions.
63  */
64 inline bool FilterList::HasRegExps() const
65 {
66         return !m_list.empty();
67 }
68
69 /** 
70  * @brief Returns the last matched expression (if any).
71  * This function returns the regular expression string that matched last.
72  * @return Last matched expression, or `nullptr` in case no matches yet.
73  */
74 inline const char * FilterList::GetLastMatchExpression() const
75 {
76         return m_lastMatchExpression->c_str();
77 }