24#include <wx/tokenzr.h>
28#define CLAMPED_VAL_INT_MAX( x ) \
29 std::min( x, static_cast<size_t>( std::numeric_limits<int>::max() ) )
49 if( loc == wxNOT_FOUND )
52 return { loc,
static_cast<int>(
m_pattern.size() ) };
67 wxLog::SetLogLevel( level );
79 if( aPattern.StartsWith(
"^" ) && aPattern.EndsWith(
"$" ) )
83 else if( aPattern.StartsWith(
"/" ) )
107 wxString pattern( aPattern );
109 if( !pattern.StartsWith( wxT(
"^" ) ) )
110 pattern = wxT(
"^" ) + pattern;
112 if( !pattern.EndsWith( wxT(
"$" ) ) )
113 pattern += wxT(
"$" );
129 if(
m_regex.Matches( aCandidate ) )
132 m_regex.GetMatch( &start, &len, 0 );
146 if( loc == wxNOT_FOUND )
149 return { loc,
static_cast<int>(
m_pattern.size() ) };
160 regex.Alloc( 2 * aPattern.Length() );
162 const wxString to_replace = wxT(
".*+?^${}()|[]/\\" );
164 for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
174 regex += wxT(
".*" );
176 else if( to_replace.Find( c ) != wxNOT_FOUND )
209 regex.Alloc( 2 * aPattern.Length() );
211 const wxString to_replace = wxT(
".*+?^${}()|[]/\\" );
215 for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
225 regex += wxT(
".*" );
227 else if( to_replace.Find( c ) != wxNOT_FOUND )
229 regex += wxS(
"\\" );
246 wxRegEx regex_search( R
"(^(\w+)(<|<=|=|>=|>)([-+]?[\d.]*)(\w*)$)", wxRE_ADVANCED );
248 bool matches = regex_search.IsValid() && regex_search.Matches( aPattern );
250 if( !matches || regex_search.GetMatchCount() < 5 )
254 wxString key = regex_search.GetMatch( aPattern, 1 );
255 wxString rel = regex_search.GetMatch( aPattern, 2 );
256 wxString val = regex_search.GetMatch( aPattern, 3 );
257 wxString unit = regex_search.GetMatch( aPattern, 4 );
261 if( rel == wxS(
"<" ) )
263 else if( rel == wxS(
"<=" ) )
265 else if( rel == wxS(
"=" ) )
267 else if( rel == wxS(
">=" ) )
269 else if( rel == wxS(
">" ) )
280 else if( !val.ToCDouble( &
m_value ) )
285 auto unit_it =
m_units.find( unit.Lower() );
306 wxStringTokenizer tokenizer( aCandidate,
" \t\r\n", wxTOKEN_STRTOK );
309 while( tokenizer.HasMoreTokens() )
311 const wxString token = tokenizer.GetNextToken();
312 int found_delta =
FindOne( token );
316 size_t found = (size_t) found_delta + lastpos;
320 lastpos = tokenizer.GetPosition();
329 wxRegEx regex_description( R
"((\w+)[=:]([-+]?[\d.]+)(\w*))", wxRE_ADVANCED );
331 bool matches = regex_description.IsValid() && regex_description.Matches( aCandidate );
337 regex_description.GetMatch( &start, &len, 0 );
338 wxString key = regex_description.GetMatch( aCandidate, 1 );
339 wxString val = regex_description.GetMatch( aCandidate, 2 );
340 wxString unit = regex_description.GetMatch( aCandidate, 3 );
344 if( key.Lower() !=
m_key )
349 if( !val.ToCDouble( &val_parsed ) )
352 auto unit_it =
m_units.find( unit.Lower() );
355 val_parsed *= unit_it->second;
364 case ANY:
return istart;
371 { wxS(
"p" ), 1e-12 },
372 { wxS(
"n" ), 1e-9 },
373 { wxS(
"u" ), 1e-6 },
374 { wxS(
"m" ), 1e-3 },
377 { wxS(
"meg" ), 1e6 },
379 { wxS(
"t" ), 1e12 },
380 { wxS(
"ki" ), 1024. },
381 { wxS(
"mi" ), 1048576. },
382 { wxS(
"gi" ), 1073741824. },
383 { wxS(
"ti" ), 1099511627776. } };
394 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
395 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
396 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_RELATIONAL>() );
400 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
404 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
405 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
406 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
410 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_ANCHORED>() );
411 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
415 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
416 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
417 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
421 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
422 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
423 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
432 aMatchersTriggered = 0;
434 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
440 aMatchersTriggered += 1;
443 aPosition = local_find.
start;
453 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
455 if( matcher->Find( aTerm ).start >= 0 )
465 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
467 if( matcher->Find( aTerm ).start == 0 )
481 if( !term.Normalized )
483 term.Text = term.Text.MakeLower().Trim(
false ).Trim(
true );
487 if( term.Text.Length() > 1000 )
488 term.Text = term.Text.Left( 1000 );
490 term.Normalized =
true;
494 int matchers_fired = 0;
498 score += 8 * term.Score;
503 else if(
Find( term.Text, matchers_fired, found_pos ) )
506 score += 2 * term.Score;
523 std::unique_ptr<EDA_PATTERN_MATCH> aMatcher )
525 if ( aMatcher->SetPattern( aPattern ) )
526 m_matchers.push_back( std::move( aMatcher ) );
int ScoreTerms(std::vector< SEARCH_TERM > &aWeightedTerms, bool *aExactMatch=nullptr)
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_matchers
void AddMatcher(const wxString &aPattern, std::unique_ptr< EDA_PATTERN_MATCH > aMatcher)
Add matcher if it can compile the pattern.
EDA_COMBINED_MATCHER(const wxString &aPattern, COMBINED_MATCHER_CONTEXT aContext)
bool Find(const wxString &aTerm, int &aMatchersTriggered, int &aPosition)
Look in all existing matchers, return the earliest match of any of the existing.
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 if a given candidate string matches the set patter...
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 if a given candidate string matches the set patter...
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 if a given candidate string matches the set patter...
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 if a given candidate string matches the set patter...
wxString m_wildcard_pattern
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().
Context class to set wx loglevel for a block, and always restore it at the end.
WX_LOGLEVEL_CONTEXT(wxLogLevel level)
#define CLAMPED_VAL_INT_MAX(x)
Abstract pattern-matching tool and implementations.
static const int EDA_PATTERN_NOT_FOUND
A structure for storing weighted search terms.