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 const std::map<wxString, double> m_units;
196};
197
198
200{
207
208
210{
211public:
212 EDA_COMBINED_MATCHER( const wxString& aPattern, COMBINED_MATCHER_CONTEXT aContext );
213
214 /*
215 * Deleted copy or else we have to implement copy constructors for all EDA_PATTERN_MATCH classes
216 * due to this class' m_matchers member being copied
217 */
219
220 /*
221 * Deleted copy or else we have to implement copy constructors for all EDA_PATTERN_MATCH classes
222 * due to this class' m_matchers member being copied
223 */
225
226 /*
227 * Look in all existing matchers, return the earliest match of any of
228 * the existing.
229 *
230 * @param aTerm term to look for
231 * @param aMatchersTriggered out: number of matcher that found the term
232 * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
233 *
234 * @return true if any matchers found the term
235 */
236 bool Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition );
237
238 bool Find( const wxString& aTerm );
239
240 bool StartsWith( const wxString& aTerm );
241
242 const wxString& GetPattern() const;
243
244 int ScoreTerms( std::vector<SEARCH_TERM>& aWeightedTerms );
245
246private:
247 // Add matcher if it can compile the pattern.
248 void AddMatcher( const wxString& aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
249
250 std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
251 wxString m_pattern;
252};
253
254#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_NET
@ CTX_SIGNAL
@ CTX_NETCLASS
@ CTX_SEARCH
@ CTX_LIBITEM
#define KICOMMON_API
Definition: kicommon.h:28
SEARCH_TERM(const wxString &aText, int aScore)