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-2021 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/string.h>
36 #include <wx/regex.h>
37 
38 static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
39 
40 /*
41  * Interface for a pattern matcher, for which there are several implementations
42  */
44 {
45 public:
46  struct FIND_RESULT
47  {
49  int length = 0;
50 
51  bool valid() const
52  {
53  return start != EDA_PATTERN_NOT_FOUND;
54  }
55 
56  explicit operator bool() const
57  {
58  return valid();
59  }
60  };
61 
62  virtual ~EDA_PATTERN_MATCH() {}
63 
68  virtual bool SetPattern( const wxString& aPattern ) = 0;
69 
73  virtual wxString const& GetPattern() const = 0;
74 
80  virtual FIND_RESULT Find( const wxString& aCandidate ) const = 0;
81 };
82 
83 
84 /*
85  * Match simple substring
86  */
88 {
89 public:
90  virtual bool SetPattern( const wxString& aPattern ) override;
91  virtual wxString const& GetPattern() const override;
92  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
93 
94 protected:
95  wxString m_pattern;
96 };
97 
98 
99 /*
100  * Match regular expression
101  */
103 {
104 public:
105  virtual bool SetPattern( const wxString& aPattern ) override;
106  virtual wxString const& GetPattern() const override;
107  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
108 
109 protected:
110  wxString m_pattern;
111  wxRegEx m_regex;
112 };
113 
114 
116 {
117 public:
118  virtual bool SetPattern( const wxString& aPattern ) override;
119  virtual wxString const& GetPattern() const override;
120  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
121 
122 protected:
124 };
125 
126 
128 {
129 public:
130  virtual bool SetPattern( const wxString& aPattern ) override;
131 };
132 
133 
148 {
149 public:
150  virtual bool SetPattern( const wxString& aPattern ) override;
151  virtual wxString const& GetPattern() const override;
152  virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
153  int FindOne( const wxString& aCandidate ) const;
154 
155 protected:
156 
157  enum RELATION { LT, LE, EQ, GE, GT, NONE };
158 
159  wxString m_pattern;
160  wxString m_key;
162  double m_value;
163 
164  static wxRegEx m_regex_description;
165  static wxRegEx m_regex_search;
166  static const std::map<wxString, double> m_units;
167 };
168 
169 
171 {
172 public:
173  EDA_COMBINED_MATCHER( const wxString& aPattern );
174 
175  /*
176  * Look in all existing matchers, return the earliest match of any of
177  * the existing.
178  *
179  * @param aTerm term to look for
180  * @param aMatchersTriggered out: number of matcher that found the term
181  * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
182  *
183  * @return true if any matchers found the term
184  */
185  bool Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition );
186 
187  const wxString& GetPattern() const;
188 
189 private:
190  // Add matcher if it can compile the pattern.
191  void AddMatcher( const wxString& aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
192 
193  std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
194  wxString m_pattern;
195 };
196 
197 #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
const wxString & 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