Debian Codegraph @

CodeLite/clWildMatch.cpp

raw
1 #include "clWildMatch.hpp"
2
3 #include <algorithm>
4 #include <wx/filefn.h>
5 #include <wx/tokenzr.h>
6
7 namespace
8 {
9 void split_mask(const wxString& maskString, std::vector<_Mask>& includeMask, std::vector<_Mask>& excludeMask)
10 {
11 wxArrayString masks = ::wxStringTokenize(maskString, ";,", wxTOKEN_STRTOK);
12 for(wxString& mask : masks) {
13 mask.Trim().Trim(false);
14 // exclude mask starts with "!" or "-"
15 if((mask[0] == '!') || (mask[0] == '-')) {
16 mask.Remove(0, 1);
17 excludeMask.push_back({ mask, ::wxIsWild(mask) });
18 } else {
19 includeMask.push_back({ mask, ::wxIsWild(mask) });
20 }
21 }
22 }
23
24 } // namespace
25 clFileExtensionMatcher::clFileExtensionMatcher(const wxString& mask)
26 : m_mask(mask)
27 {
28 std::vector<_Mask> dummy;
29 wxArrayString masks = ::wxStringTokenize(m_mask, ";,", wxTOKEN_STRTOK);
30 for(wxString& mask : masks) {
31 mask.Replace("*", wxEmptyString);
32 m_include_mask.push_back({ mask, false });
33 }
34 m_always_matches = false;
35 }
36
37 clFileExtensionMatcher::~clFileExtensionMatcher() {}
38
39 bool clFileExtensionMatcher::matches(const wxString& filename) const
40 {
41 if(m_always_matches) {
42 return true;
43 }
44
45 for(const _Mask& d : m_include_mask) {
46 if(!d.is_wild && filename.EndsWith(d.pattern)) {
47 return true;
48 }
49 }
50 return false;
51 }
52
53 // clPathExcluder
54
55 clPathExcluder::clPathExcluder(const wxArrayString& patterns)
56 {
57 m_exclude_mask.reserve(patterns.size());
58 for(const auto& pattern : patterns) {
59 m_exclude_mask.push_back({ pattern, ::wxIsWild(pattern) });
60 }
61 }
62
63 clPathExcluder::clPathExcluder(const wxString& mask)
64 : m_mask(mask)
65 {
66 std::vector<_Mask> includeMask;
67 split_mask(m_mask, includeMask, m_exclude_mask);
68 }
69
70 clPathExcluder::~clPathExcluder() {}
71
72 bool clPathExcluder::is_exclude_path(const wxString& str) const
73 {
74 if(m_exclude_mask.empty()) {
75 return false;
76 }
77
78 for(const auto& d : m_exclude_mask) {
79 if((!d.is_wild && str.Contains(d.pattern)) || // use Contains match
80 (d.is_wild && ::wxMatchWild(d.pattern, str))) // pattern matching
81 {
82 return true;
83 }
84 }
85 return false;
86 }