KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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 <kicommon.h>
33#include <vector>
34#include <map>
35#include <memory>
36#include <wx/string.h>
37#include <wx/regex.h>
38
39static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
40
41/*
42 * A structure for storing weighted search terms.
43 *
44 * NOTE: an exact match is scored at 8 * Score while a match at the start of the text is scored
45 * at 2 * Score.
46 */
48{
49 SEARCH_TERM( const wxString& aText, int aScore ) :
50 Text( aText ),
51 Score( aScore ),
52 Normalized( false )
53 {}
54
55 wxString Text;
56 int Score;
58};
59
60
61/*
62 * Interface for a pattern matcher, for which there are several implementations
63 */
65{
66public:
68 {
70 int length = 0;
71
72 bool valid() const
73 {
74 return start != EDA_PATTERN_NOT_FOUND;
75 }
76
77 explicit operator bool() const
78 {
79 return valid();
80 }
81 };
82
83 virtual ~EDA_PATTERN_MATCH() {}
84
89 virtual bool SetPattern( const wxString& aPattern ) = 0;
90
94 virtual wxString const& GetPattern() const = 0;
95
101 virtual FIND_RESULT Find( const wxString& aCandidate ) const = 0;
102};
103
104
105/*
106 * Match simple substring
107 */
109{
110public:
111
112 virtual bool SetPattern( const wxString& aPattern ) override;
113 virtual wxString const& GetPattern() const override;
114 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
115
116protected:
117 wxString m_pattern;
118};
119
120
121/*
122 * Match regular expression
123 */
125{
126public:
127
128 virtual bool SetPattern( const wxString& aPattern ) override;
129 virtual wxString const& GetPattern() const override;
130 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
131
132protected:
133 wxString m_pattern;
134 wxRegEx m_regex;
135};
136
137
139{
140public:
141 virtual bool SetPattern( const wxString& aPattern ) override;
142};
143
144
146{
147public:
148
149 virtual bool SetPattern( const wxString& aPattern ) override;
150 virtual wxString const& GetPattern() const override;
151 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
152
153protected:
155};
156
157
159{
160public:
161 virtual bool SetPattern( const wxString& aPattern ) override;
162};
163
164
179{
180public:
181 virtual bool SetPattern( const wxString& aPattern ) override;
182 virtual wxString const& GetPattern() const override;
183 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
184 int FindOne( const wxString& aCandidate ) const;
185
186protected:
187
188 enum RELATION { LT, LE, EQ, GE, GT, ANY };
189
190 wxString m_pattern;
191 wxString m_key;
193 double m_value;
194
195 static wxRegEx m_regex_description;
196 static wxRegEx m_regex_search;
197 static const std::map<wxString, double> m_units;
198};
199
200
202{
208
209
211{
212public:
213 EDA_COMBINED_MATCHER( const wxString& aPattern, COMBINED_MATCHER_CONTEXT aContext );
214
215 /*
216 * Deleted copy or else we have to implement copy constructors for all EDA_PATTERN_MATCH classes
217 * due to this class' m_matchers member being copied
218 */
220
221 /*
222 * Deleted copy or else we have to implement copy constructors for all EDA_PATTERN_MATCH classes
223 * due to this class' m_matchers member being copied
224 */
226
227 /*
228 * Look in all existing matchers, return the earliest match of any of
229 * the existing.
230 *
231 * @param aTerm term to look for
232 * @param aMatchersTriggered out: number of matcher that found the term
233 * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
234 *
235 * @return true if any matchers found the term
236 */
237 bool Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition );
238
239 bool Find( const wxString& aTerm );
240
241 bool StartsWith( const wxString& aTerm );
242
243 const wxString& GetPattern() const;
244
245 int ScoreTerms( std::vector<SEARCH_TERM>& aWeightedTerms );
246
247private:
248 // Add matcher if it can compile the pattern.
249 void AddMatcher( const wxString& aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
250
251 std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
252 wxString m_pattern;
253};
254
255#endif // EDA_PATTERN_MATCH_H
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
EDA_COMBINED_MATCHER & operator=(EDA_COMBINED_MATCHER const &)=delete
EDA_COMBINED_MATCHER(EDA_COMBINED_MATCHER const &)=delete
static const std::map< wxString, double > m_units
virtual bool SetPattern(const wxString &aPattern)=0
Set the pattern against which candidates will be matched.
virtual wxString const & GetPattern() const =0
Return the pattern passed to SetPattern().
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 ~EDA_PATTERN_MATCH()
static const int EDA_PATTERN_NOT_FOUND
COMBINED_MATCHER_CONTEXT
@ CTX_SIGNAL
@ CTX_NETCLASS
@ CTX_SEARCH
@ CTX_LIBITEM
#define KICOMMON_API
Definition: kicommon.h:28
SEARCH_TERM(const wxString &aText, int aScore)