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 <memory>
34 #include <wx/wx.h>
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  virtual ~EDA_PATTERN_MATCH() {}
47 
52  virtual bool SetPattern( const wxString& aPattern ) = 0;
53 
57  virtual wxString const& GetPattern() const = 0;
58 
63  virtual int Find( const wxString& aCandidate ) const = 0;
64 };
65 
66 
67 /*
68  * Match simple substring
69  */
71 {
72 public:
73  virtual bool SetPattern( const wxString& aPattern ) override;
74  virtual wxString const& GetPattern() const override;
75  virtual int Find( const wxString& aCandidate ) const override;
76 
77 protected:
78  wxString m_pattern;
79 };
80 
81 
82 /*
83  * Match regular expression
84  */
86 {
87 public:
88  virtual bool SetPattern( const wxString& aPattern ) override;
89  virtual wxString const& GetPattern() const override;
90  virtual int Find( const wxString& aCandidate ) const override;
91 
92 protected:
93  wxString m_pattern;
94  wxRegEx m_regex;
95 };
96 
97 
99 {
100 public:
101  virtual bool SetPattern( const wxString& aPattern ) override;
102  virtual wxString const& GetPattern() const override;
103  virtual int Find( const wxString& aCandidate ) const override;
104 
105 protected:
107 };
108 
109 
111 {
112 public:
113  EDA_COMBINED_MATCHER( const wxString &aPattern );
114 
115  /*
116  * Look in all existing matchers, return the earliest match of any of
117  * the existing.
118  *
119  * @param aTerm term to look for
120  * @param aMatchersTriggered out: number of matcher that found the term
121  * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
122  *
123  * @return true if any matchers found the term
124  */
125  bool Find( const wxString &aTerm, int& aMatchersTriggered, int& aPosition );
126 
127  wxString const& GetPattern() const;
128 
129 private:
130  // Add matcher if it can compile the pattern.
131  void AddMatcher( const wxString &aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
132 
133  std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
134  wxString m_pattern;
135 };
136 
137 #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 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 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.
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
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)