KiCad PCB EDA Suite
eda_pattern_match.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2017 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
29 #ifndef EDA_PATTERN_MATCH_H
30 #define EDA_PATTERN_MATCH_H
31 
32 #include <vector>
33 #include <map>
34 #include <memory>
35 #include <wx/wx.h>
36 #include <wx/string.h>
37 #include <wx/regex.h>
38 
39 static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
40 
41 /*
42  * Interface for a pattern matcher, for which there are several implementations
43  */
45 {
46 public:
47  struct FIND_RESULT
48  {
50  int length = 0;
51 
52  bool valid() const
53  {
54  return start != EDA_PATTERN_NOT_FOUND;
55  }
56 
57  explicit operator bool() const
58  {
59  return valid();
60  }
61  };
62 
63  virtual ~EDA_PATTERN_MATCH() {}
64 
69  virtual bool SetPattern( const wxString& aPattern ) = 0;
70 
74  virtual wxString const& GetPattern() const = 0;
75 
81  virtual FIND_RESULT Find( const wxString& aCandidate ) const = 0;
82 };
83 
84 
85 /*
86  * Match simple substring
87  */
89 {
90 public:
91  virtual bool SetPattern( const wxString& aPattern ) override;
92  virtual wxString const& GetPattern() const override;
93  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
94 
95 protected:
96  wxString m_pattern;
97 };
98 
99 
100 /*
101  * Match regular expression
102  */
104 {
105 public:
106  virtual bool SetPattern( const wxString& aPattern ) override;
107  virtual wxString const& GetPattern() const override;
108  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
109 
110 protected:
111  wxString m_pattern;
112  wxRegEx m_regex;
113 };
114 
115 
117 {
118 public:
119  virtual bool SetPattern( const wxString& aPattern ) override;
120  virtual wxString const& GetPattern() const override;
121  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
122 
123 protected:
125 };
126 
127 
129 {
130 public:
131  virtual bool SetPattern( const wxString& aPattern ) override;
132 };
133 
134 
149 {
150 public:
151  virtual bool SetPattern( const wxString& aPattern ) override;
152  virtual wxString const& GetPattern() const override;
153  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
154  int FindOne( const wxString& aCandidate ) const;
155 
156 protected:
157 
158  enum RELATION { LT, LE, EQ, GE, GT, NONE };
159 
160  wxString m_pattern;
161  wxString m_key;
163  double m_value;
164 
165  static wxRegEx m_regex_description;
166  static wxRegEx m_regex_search;
167  static const std::map<wxString, double> m_units;
168 };
169 
170 
172 {
173 public:
174  EDA_COMBINED_MATCHER( const wxString &aPattern );
175 
176  /*
177  * Look in all existing matchers, return the earliest match of any of
178  * the existing.
179  *
180  * @param aTerm term to look for
181  * @param aMatchersTriggered out: number of matcher that found the term
182  * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
183  *
184  * @return true if any matchers found the term
185  */
186  bool Find( const wxString &aTerm, int& aMatchersTriggered, int& aPosition );
187 
188  wxString const& GetPattern() const;
189 
190 private:
191  // Add matcher if it can compile the pattern.
192  void AddMatcher( const wxString &aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
193 
194  std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
195  wxString m_pattern;
196 };
197 
198 #endif // EDA_PATTERN_MATCH_H
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual bool SetPattern(const wxString &aPattern)=0
Set the pattern against which candidates will be matched.
int FindOne(const wxString &aCandidate) const
wxString const & GetPattern() const
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
void AddMatcher(const wxString &aPattern, std::unique_ptr< EDA_PATTERN_MATCH > aMatcher)
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual ~EDA_PATTERN_MATCH()
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
static const int EDA_PATTERN_NOT_FOUND
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
virtual FIND_RESULT Find(const wxString &aCandidate) const =0
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual wxString const & GetPattern() const =0
Return the pattern passed to SetPattern().
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
virtual FIND_RESULT Find(const wxString &aCandidate) const override
Return the location and possibly length of a match iff a given candidate string matches the set patte...
EDA_COMBINED_MATCHER(const wxString &aPattern)
static const std::map< wxString, double > m_units