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 <vector>
33#include <map>
34#include <memory>
35#include <wx/string.h>
36#include <wx/regex.h>
37
38static const int EDA_PATTERN_NOT_FOUND = wxNOT_FOUND;
39
40/*
41 * A structure for storing weighted search terms.
42 *
43 * NOTE: an exact match is scored at 8 * Score while a match at the start of the text is scored
44 * at 2 * Score.
45 */
47{
48 SEARCH_TERM( const wxString& aText, int aScore ) :
49 Text( aText ),
50 Score( aScore ),
51 Normalized( false )
52 {}
53
54 wxString Text;
55 int Score;
57};
58
59
60/*
61 * Interface for a pattern matcher, for which there are several implementations
62 */
64{
65public:
67 {
69 int length = 0;
70
71 bool valid() const
72 {
74 }
75
76 explicit operator bool() const
77 {
78 return valid();
79 }
80 };
81
82 virtual ~EDA_PATTERN_MATCH() {}
83
88 virtual bool SetPattern( const wxString& aPattern ) = 0;
89
93 virtual wxString const& GetPattern() const = 0;
94
100 virtual FIND_RESULT Find( const wxString& aCandidate ) const = 0;
101};
102
103
104/*
105 * Match simple substring
106 */
108{
109public:
110 virtual bool SetPattern( const wxString& aPattern ) override;
111 virtual wxString const& GetPattern() const override;
112 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
113
114protected:
115 wxString m_pattern;
116};
117
118
119/*
120 * Match regular expression
121 */
123{
124public:
125 virtual bool SetPattern( const wxString& aPattern ) override;
126 virtual wxString const& GetPattern() const override;
127 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
128
129protected:
130 wxString m_pattern;
131 wxRegEx m_regex;
132};
133
134
136{
137public:
138 virtual bool SetPattern( const wxString& aPattern ) override;
139};
140
141
143{
144public:
145 virtual bool SetPattern( const wxString& aPattern ) override;
146 virtual wxString const& GetPattern() const override;
147 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
148
149protected:
151};
152
153
155{
156public:
157 virtual bool SetPattern( const wxString& aPattern ) override;
158};
159
160
175{
176public:
177 virtual bool SetPattern( const wxString& aPattern ) override;
178 virtual wxString const& GetPattern() const override;
179 virtual FIND_RESULT Find( const wxString& aCandidate ) const override;
180 int FindOne( const wxString& aCandidate ) const;
181
182protected:
183
184 enum RELATION { LT, LE, EQ, GE, GT, ANY };
185
186 wxString m_pattern;
187 wxString m_key;
189 double m_value;
190
191 static wxRegEx m_regex_description;
192 static wxRegEx m_regex_search;
193 static const std::map<wxString, double> m_units;
194};
195
196
198{
204
205
207{
208public:
209 EDA_COMBINED_MATCHER( const wxString& aPattern, COMBINED_MATCHER_CONTEXT aContext );
210
211 /*
212 * Look in all existing matchers, return the earliest match of any of
213 * the existing.
214 *
215 * @param aTerm term to look for
216 * @param aMatchersTriggered out: number of matcher that found the term
217 * @param aPostion out: where the term was found, or EDA_PATTERN_NOT_FOUND
218 *
219 * @return true if any matchers found the term
220 */
221 bool Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition );
222
223 bool Find( const wxString& aTerm );
224
225 bool StartsWith( const wxString& aTerm );
226
227 const wxString& GetPattern() const;
228
229 int ScoreTerms( std::vector<SEARCH_TERM>& aWeightedTerms );
230
231private:
232 // Add matcher if it can compile the pattern.
233 void AddMatcher( const wxString& aPattern, std::unique_ptr<EDA_PATTERN_MATCH> aMatcher );
234
235 std::vector<std::unique_ptr<EDA_PATTERN_MATCH>> m_matchers;
236 wxString m_pattern;
237};
238
239#endif // EDA_PATTERN_MATCH_H
int ScoreTerms(std::vector< SEARCH_TERM > &aWeightedTerms)
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
void AddMatcher(const wxString &aPattern, std::unique_ptr< EDA_PATTERN_MATCH > aMatcher)
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
const wxString & GetPattern() const
bool StartsWith(const wxString &aTerm)
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 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) 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.
int FindOne(const wxString &aCandidate) const
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 override
Return the pattern passed to SetPattern().
static const std::map< wxString, double > m_units
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.
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) override
Set the pattern against which candidates will be matched.
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) override
Set the pattern against which candidates will be matched.
virtual wxString const & GetPattern() const override
Return the pattern passed to SetPattern().
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
SEARCH_TERM(const wxString &aText, int aScore)