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  virtual ~EDA_PATTERN_MATCH() {}
48 
53  virtual bool SetPattern( const wxString& aPattern ) = 0;
54 
58  virtual wxString const& GetPattern() const = 0;
59 
64  virtual int Find( const wxString& aCandidate ) const = 0;
65 };
66 
67 
68 /*
69  * Match simple substring
70  */
72 {
73 public:
74  virtual bool SetPattern( const wxString& aPattern ) override;
75  virtual wxString const& GetPattern() const override;
76  virtual int Find( const wxString& aCandidate ) const override;
77 
78 protected:
79  wxString m_pattern;
80 };
81 
82 
83 /*
84  * Match regular expression
85  */
87 {
88 public:
89  virtual bool SetPattern( const wxString& aPattern ) override;
90  virtual wxString const& GetPattern() const override;
91  virtual int Find( const wxString& aCandidate ) const override;
92 
93 protected:
94  wxString m_pattern;
95  wxRegEx m_regex;
96 };
97 
98 
100 {
101 public:
102  virtual bool SetPattern( const wxString& aPattern ) override;
103  virtual wxString const& GetPattern() const override;
104  virtual int Find( const wxString& aCandidate ) const override;
105 
106 protected:
108 };
109 
110 
125 {
126 public:
127  virtual bool SetPattern( const wxString& aPattern ) override;
128  virtual wxString const& GetPattern() const override;
129  virtual int Find( const wxString& aCandidate ) const override;
130  int FindOne( const wxString& aCandidate ) const;
131 
132 protected:
133 
134  enum RELATION { LT, LE, EQ, GE, GT, NONE };
135 
136  wxString m_pattern;
137  wxString m_key;
139  double m_value;
140 
141  static wxRegEx m_regex_description;
142  static wxRegEx m_regex_search;
143  static const std::map<wxString, double> m_units;
144 };
145 
146 
148 {
149 public:
150  EDA_COMBINED_MATCHER( const wxString &aPattern );
151 
152  /*
153  * Look in all existing matchers, return the earliest match of any of
154  * the existing.
155  *
156  * @param aTerm term to look for
157  * @param aMatchersTriggered out: number of matcher that found the term
158  * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
159  *
160  * @return true if any matchers found the term
161  */
162  bool Find( const wxString &aTerm, int& aMatchersTriggered, int& aPosition );
163 
164  wxString const& GetPattern() const;
165 
166 private:
167  // Add matcher if it can compile the pattern.
168  void AddMatcher( const wxString &aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
169 
170  std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
171  wxString m_pattern;
172 };
173 
174 #endif // EDA_PATTERN_MATCH_H
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
wxString const & GetPattern() const
virtual bool SetPattern(const wxString &aPattern)=0
Set the pattern against which candidates will be matched.
virtual int Find(const wxString &aCandidate) const override
Return the location of a match iff a given candidate string matches the set pattern.
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual int Find(const wxString &aCandidate) const override
Return the location of a match iff a given candidate string matches the set pattern.
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 wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual int Find(const wxString &aCandidate) const =0
Return the location of a match iff a given candidate string matches the set pattern.
virtual bool SetPattern(const wxString &aPattern) override
Set the pattern against which candidates will be matched.
int FindOne(const wxString &aCandidate) const
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual int Find(const wxString &aCandidate) const override
Return the location of a match iff a given candidate string matches the set pattern.
static const int EDA_PATTERN_NOT_FOUND
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
virtual int Find(const wxString &aCandidate) const override
Return the location of a match iff a given candidate string matches the set pattern.
virtual wxString const & GetPattern() const =0
Return the pattern passed to SetPattern().
EDA_COMBINED_MATCHER(const wxString &aPattern)
static const std::map< wxString, double > m_units