-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
eda_pattern_match.cpp
525 lines (413 loc) · 14.2 KB
/
eda_pattern_match.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015-2017 Chris Pavlina <[email protected]>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <eda_pattern_match.h>
#include <limits>
#include <wx/log.h>
#include <wx/tokenzr.h>
#include <algorithm>
// Helper to make the code cleaner when we want this operation
#define CLAMPED_VAL_INT_MAX( x ) std::min( x, static_cast<size_t>( std::numeric_limits<int>::max() ) )
bool EDA_PATTERN_MATCH_SUBSTR::SetPattern( const wxString& aPattern )
{
m_pattern = aPattern;
return true;
}
wxString const& EDA_PATTERN_MATCH_SUBSTR::GetPattern() const
{
return m_pattern;
}
EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_SUBSTR::Find( const wxString& aCandidate ) const
{
int loc = aCandidate.Find( m_pattern );
if( loc == wxNOT_FOUND )
return {};
else
return { loc, static_cast<int>( m_pattern.size() ) };
}
/**
* Context class to set wx loglevel for a block, and always restore it at the end.
*/
class WX_LOGLEVEL_CONTEXT
{
wxLogLevel m_old_level;
public:
WX_LOGLEVEL_CONTEXT( wxLogLevel level )
{
m_old_level = wxLog::GetLogLevel();
wxLog::SetLogLevel( level );
}
~WX_LOGLEVEL_CONTEXT()
{
wxLog::SetLogLevel( m_old_level );
}
};
bool EDA_PATTERN_MATCH_REGEX::SetPattern( const wxString& aPattern )
{
if( aPattern.StartsWith( "^" ) && aPattern.EndsWith( "$" ) )
{
m_pattern = aPattern;
}
else if( aPattern.StartsWith( "/" ) )
{
// Requiring a '/' on the end means they get no feedback while they type
m_pattern = aPattern.Mid( 1 );
if( m_pattern.EndsWith( "/" ) )
m_pattern = m_pattern.Left( m_pattern.length() - 1 );
}
else
{
// For now regular expressions must be explicit
return false;
}
// Evil and undocumented: wxRegEx::Compile calls wxLogError on error, even
// though it promises to just return false. Silence the error.
WX_LOGLEVEL_CONTEXT ctx( wxLOG_FatalError );
return m_regex.Compile( m_pattern, wxRE_ADVANCED );
}
bool EDA_PATTERN_MATCH_REGEX_ANCHORED::SetPattern( const wxString& aPattern )
{
wxString pattern( aPattern );
if( !pattern.StartsWith( wxT( "^" ) ) )
pattern = wxT( "^" ) + pattern;
if( !pattern.EndsWith( wxT( "$" ) ) )
pattern += wxT( "$" );
return EDA_PATTERN_MATCH_REGEX::SetPattern( pattern );
}
wxString const& EDA_PATTERN_MATCH_REGEX::GetPattern() const
{
return m_pattern;
}
EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_REGEX::Find( const wxString& aCandidate ) const
{
if( m_regex.IsValid() )
{
if( m_regex.Matches( aCandidate ) )
{
size_t start, len;
m_regex.GetMatch( &start, &len, 0 );
return { static_cast<int>( CLAMPED_VAL_INT_MAX( start ) ),
static_cast<int>( CLAMPED_VAL_INT_MAX( len ) ) };
}
else
{
return {};
}
}
else
{
int loc = aCandidate.Find( m_pattern );
if( loc == wxNOT_FOUND )
return {};
else
return { loc, static_cast<int>( m_pattern.size() ) };
}
}
bool EDA_PATTERN_MATCH_WILDCARD::SetPattern( const wxString& aPattern )
{
m_wildcard_pattern = aPattern;
// Compile the wildcard string to a regular expression
wxString regex;
regex.Alloc( 2 * aPattern.Length() ); // no need to keep resizing, we know the size roughly
const wxString to_replace = wxT( ".*+?^${}()|[]/\\" );
for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
{
wxUniChar c = *it;
if( c == '?' )
{
regex += wxT( "." );
}
else if( c == '*' )
{
regex += wxT( ".*" );
}
else if( to_replace.Find( c ) != wxNOT_FOUND )
{
regex += "\\";
regex += c;
}
else
{
regex += c;
}
}
return EDA_PATTERN_MATCH_REGEX::SetPattern( wxS( "/" ) + regex + wxS( "/" ) );
}
wxString const& EDA_PATTERN_MATCH_WILDCARD::GetPattern() const
{
return m_wildcard_pattern;
}
EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_WILDCARD::Find( const wxString& aCandidate ) const
{
return EDA_PATTERN_MATCH_REGEX::Find( aCandidate );
}
bool EDA_PATTERN_MATCH_WILDCARD_ANCHORED::SetPattern( const wxString& aPattern )
{
m_wildcard_pattern = aPattern;
// Compile the wildcard string to a regular expression
wxString regex;
regex.Alloc( 2 * aPattern.Length() ); // no need to keep resizing, we know the size roughly
const wxString to_replace = wxT( ".*+?^${}()|[]/\\" );
regex += wxT( "^" );
for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
{
wxUniChar c = *it;
if( c == '?' )
{
regex += wxT( "." );
}
else if( c == '*' )
{
regex += wxT( ".*" );
}
else if( to_replace.Find( c ) != wxNOT_FOUND )
{
regex += wxS( "\\" );
regex += c;
}
else
{
regex += c;
}
}
regex += wxT( "$" );
return EDA_PATTERN_MATCH_REGEX::SetPattern( regex );
}
bool EDA_PATTERN_MATCH_RELATIONAL::SetPattern( const wxString& aPattern )
{
wxRegEx regex_search( R"(^(\w+)(<|<=|=|>=|>)([-+]?[\d.]*)(\w*)$)", wxRE_ADVANCED );
bool matches = regex_search.IsValid() && regex_search.Matches( aPattern );
if( !matches || regex_search.GetMatchCount() < 5 )
return false;
m_pattern = aPattern;
wxString key = regex_search.GetMatch( aPattern, 1 );
wxString rel = regex_search.GetMatch( aPattern, 2 );
wxString val = regex_search.GetMatch( aPattern, 3 );
wxString unit = regex_search.GetMatch( aPattern, 4 );
m_key = key.Lower();
if( rel == wxS( "<" ) )
m_relation = LT;
else if( rel == wxS( "<=" ) )
m_relation = LE;
else if( rel == wxS( "=" ) )
m_relation = EQ;
else if( rel == wxS( ">=" ) )
m_relation = GE;
else if( rel == wxS( ">" ) )
m_relation = GT;
else
return false;
if( val == "" )
{
// Matching on empty values keeps the match list from going empty when the user
// types the relational operator character, which helps prevent confusion.
m_relation = ANY;
}
else if( !val.ToCDouble( &m_value ) )
{
return false;
}
auto unit_it = m_units.find( unit.Lower() );
if( unit_it != m_units.end() )
m_value *= unit_it->second;
else
return false;
m_pattern = aPattern;
return true;
}
wxString const& EDA_PATTERN_MATCH_RELATIONAL::GetPattern() const
{
return m_pattern;
}
EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_RELATIONAL::Find( const wxString& aCandidate ) const
{
wxStringTokenizer tokenizer( aCandidate );
size_t lastpos = 0;
while( tokenizer.HasMoreTokens() )
{
const wxString token = tokenizer.GetNextToken();
int found_delta = FindOne( token );
if( found_delta != EDA_PATTERN_NOT_FOUND )
{
size_t found = (size_t) found_delta + lastpos;
return { static_cast<int>( CLAMPED_VAL_INT_MAX( found ) ), 0 };
}
lastpos = tokenizer.GetPosition();
}
return {};
}
int EDA_PATTERN_MATCH_RELATIONAL::FindOne( const wxString& aCandidate ) const
{
wxRegEx regex_description( R"((\w+)[=:]([-+]?[\d.]+)(\w*))", wxRE_ADVANCED );
bool matches = regex_description.IsValid() && regex_description.Matches( aCandidate );
if( !matches )
return EDA_PATTERN_NOT_FOUND;
size_t start, len;
regex_description.GetMatch( &start, &len, 0 );
wxString key = regex_description.GetMatch( aCandidate, 1 );
wxString val = regex_description.GetMatch( aCandidate, 2 );
wxString unit = regex_description.GetMatch( aCandidate, 3 );
int istart = static_cast<int>( CLAMPED_VAL_INT_MAX( start ) );
if( key.Lower() != m_key )
return EDA_PATTERN_NOT_FOUND;
double val_parsed;
if( !val.ToCDouble( &val_parsed ) )
return EDA_PATTERN_NOT_FOUND;
auto unit_it = m_units.find( unit.Lower() );
if( unit_it != m_units.end() )
val_parsed *= unit_it->second;
switch( m_relation )
{
case LT: return val_parsed < m_value ? istart : EDA_PATTERN_NOT_FOUND;
case LE: return val_parsed <= m_value ? istart : EDA_PATTERN_NOT_FOUND;
case EQ: return val_parsed == m_value ? istart : EDA_PATTERN_NOT_FOUND;
case GE: return val_parsed >= m_value ? istart : EDA_PATTERN_NOT_FOUND;
case GT: return val_parsed > m_value ? istart : EDA_PATTERN_NOT_FOUND;
case ANY: return istart;
default: return EDA_PATTERN_NOT_FOUND;
}
}
const std::map<wxString, double> EDA_PATTERN_MATCH_RELATIONAL::m_units = {
{ wxS( "p" ), 1e-12 },
{ wxS( "n" ), 1e-9 },
{ wxS( "u" ), 1e-6 },
{ wxS( "m" ), 1e-3 },
{ wxS( "" ), 1. },
{ wxS( "k" ), 1e3 },
{ wxS( "meg" ), 1e6 },
{ wxS( "g" ), 1e9 },
{ wxS( "t" ), 1e12 },
{ wxS( "ki" ), 1024. },
{ wxS( "mi" ), 1048576. },
{ wxS( "gi" ), 1073741824. },
{ wxS( "ti" ), 1099511627776. } };
EDA_COMBINED_MATCHER::EDA_COMBINED_MATCHER( const wxString& aPattern,
COMBINED_MATCHER_CONTEXT aContext ) :
m_pattern( aPattern )
{
switch( aContext )
{
case CTX_LIBITEM:
// Whatever syntax users prefer, it shall be matched.
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_RELATIONAL>() );
// If any of the above matchers couldn't be created because the pattern
// syntax does not match, the substring will try its best.
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
break;
case CTX_NET:
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
break;
case CTX_NETCLASS:
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_ANCHORED>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
break;
case CTX_SIGNAL:
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
break;
case CTX_SEARCH:
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
break;
}
}
bool EDA_COMBINED_MATCHER::Find( const wxString& aTerm, int& aMatchersTriggered, int& aPosition )
{
aPosition = EDA_PATTERN_NOT_FOUND;
aMatchersTriggered = 0;
for( const std::unique_ptr<EDA_PATTERN_MATCH>& matcher : m_matchers )
{
EDA_PATTERN_MATCH::FIND_RESULT local_find = matcher->Find( aTerm );
if( local_find )
{
aMatchersTriggered += 1;
if( local_find.start < aPosition || aPosition == EDA_PATTERN_NOT_FOUND )
aPosition = local_find.start;
}
}
return aPosition != EDA_PATTERN_NOT_FOUND;
}
bool EDA_COMBINED_MATCHER::Find( const wxString& aTerm )
{
for( const std::unique_ptr<EDA_PATTERN_MATCH>& matcher : m_matchers )
{
if( matcher->Find( aTerm ).start >= 0 )
return true;
}
return false;
}
bool EDA_COMBINED_MATCHER::StartsWith( const wxString& aTerm )
{
for( const std::unique_ptr<EDA_PATTERN_MATCH>& matcher : m_matchers )
{
if( matcher->Find( aTerm ).start == 0 )
return true;
}
return false;
}
int EDA_COMBINED_MATCHER::ScoreTerms( std::vector<SEARCH_TERM>& aWeightedTerms )
{
int score = 0;
for( SEARCH_TERM& term : aWeightedTerms )
{
if( !term.Normalized )
{
term.Text = term.Text.MakeLower().Trim( false ).Trim( true );
// Don't cause KiCad to hang if someone accidentally pastes the PCB or schematic
// into the search box.
if( term.Text.Length() > 5000 )
term.Text = term.Text.Left( 5000 );
term.Normalized = true;
}
int found_pos = EDA_PATTERN_NOT_FOUND;
int matchers_fired = 0;
if( GetPattern() == term.Text )
{
score += 8 * term.Score;
}
else if( Find( term.Text, matchers_fired, found_pos ) )
{
if( found_pos == 0 )
score += 2 * term.Score;
else
score += term.Score;
}
}
return score;
}
wxString const& EDA_COMBINED_MATCHER::GetPattern() const
{
return m_pattern;
}
void EDA_COMBINED_MATCHER::AddMatcher( const wxString &aPattern,
std::unique_ptr<EDA_PATTERN_MATCH> aMatcher )
{
if ( aMatcher->SetPattern( aPattern ) )
m_matchers.push_back( std::move( aMatcher ) );
}