28#include <wx/tokenzr.h>
32#define CLAMPED_VAL_INT_MAX( x ) \
33 std::min( x, static_cast<size_t>( std::numeric_limits<int>::max() ) )
53 if( loc == wxNOT_FOUND )
56 return { loc,
static_cast<int>(
m_pattern.size() ) };
71 wxLog::SetLogLevel( level );
83 if( aPattern.StartsWith(
"^" ) && aPattern.EndsWith(
"$" ) )
87 else if( aPattern.StartsWith(
"/" ) )
111 wxString pattern( aPattern );
113 if( !pattern.StartsWith( wxT(
"^" ) ) )
114 pattern = wxT(
"^" ) + pattern;
116 if( !pattern.EndsWith( wxT(
"$" ) ) )
117 pattern += wxT(
"$" );
133 if(
m_regex.Matches( aCandidate ) )
136 m_regex.GetMatch( &start, &len, 0 );
150 if( loc == wxNOT_FOUND )
153 return { loc,
static_cast<int>(
m_pattern.size() ) };
164 regex.Alloc( 2 * aPattern.Length() );
166 const wxString to_replace = wxT(
".*+?^${}()|[]/\\" );
168 for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
178 regex += wxT(
".*" );
180 else if( to_replace.Find( c ) != wxNOT_FOUND )
213 regex.Alloc( 2 * aPattern.Length() );
215 const wxString to_replace = wxT(
".*+?^${}()|[]/\\" );
219 for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
229 regex += wxT(
".*" );
231 else if( to_replace.Find( c ) != wxNOT_FOUND )
233 regex += wxS(
"\\" );
250 wxRegEx regex_search( R
"(^(\w+)(<|<=|=|>=|>)([-+]?[\d.]*)(\w*)$)", wxRE_ADVANCED );
252 bool matches = regex_search.IsValid() && regex_search.Matches( aPattern );
254 if( !matches || regex_search.GetMatchCount() < 5 )
258 wxString key = regex_search.GetMatch( aPattern, 1 );
259 wxString rel = regex_search.GetMatch( aPattern, 2 );
260 wxString val = regex_search.GetMatch( aPattern, 3 );
261 wxString unit = regex_search.GetMatch( aPattern, 4 );
265 if( rel == wxS(
"<" ) )
267 else if( rel == wxS(
"<=" ) )
269 else if( rel == wxS(
"=" ) )
271 else if( rel == wxS(
">=" ) )
273 else if( rel == wxS(
">" ) )
284 else if( !val.ToCDouble( &
m_value ) )
289 auto unit_it =
m_units.find( unit.Lower() );
310 wxStringTokenizer tokenizer( aCandidate );
313 while( tokenizer.HasMoreTokens() )
315 const wxString token = tokenizer.GetNextToken();
316 int found_delta =
FindOne( token );
320 size_t found = (size_t) found_delta + lastpos;
324 lastpos = tokenizer.GetPosition();
333 wxRegEx regex_description( R
"((\w+)[=:]([-+]?[\d.]+)(\w*))", wxRE_ADVANCED );
335 bool matches = regex_description.IsValid() && regex_description.Matches( aCandidate );
341 regex_description.GetMatch( &start, &len, 0 );
342 wxString key = regex_description.GetMatch( aCandidate, 1 );
343 wxString val = regex_description.GetMatch( aCandidate, 2 );
344 wxString unit = regex_description.GetMatch( aCandidate, 3 );
348 if( key.Lower() !=
m_key )
353 if( !val.ToCDouble( &val_parsed ) )
356 auto unit_it =
m_units.find( unit.Lower() );
359 val_parsed *= unit_it->second;
368 case ANY:
return istart;
375 { wxS(
"p" ), 1e-12 },
376 { wxS(
"n" ), 1e-9 },
377 { wxS(
"u" ), 1e-6 },
378 { wxS(
"m" ), 1e-3 },
381 { wxS(
"meg" ), 1e6 },
383 { wxS(
"t" ), 1e12 },
384 { wxS(
"ki" ), 1024. },
385 { wxS(
"mi" ), 1048576. },
386 { wxS(
"gi" ), 1073741824. },
387 { wxS(
"ti" ), 1099511627776. } };
392 m_pattern( aPattern )
398 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
399 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
400 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_RELATIONAL>() );
404 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
408 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
409 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
410 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
414 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_ANCHORED>() );
415 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
419 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
420 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
421 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
425 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
426 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
427 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
436 aMatchersTriggered = 0;
438 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
444 aMatchersTriggered += 1;
447 aPosition = local_find.
start;
457 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
459 if( matcher->Find( aTerm ).start >= 0 )
469 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
471 if( matcher->Find( aTerm ).start == 0 )
485 if( !term.Normalized )
487 term.Text = term.Text.MakeLower().Trim(
false ).Trim(
true );
491 if( term.Text.Length() > 1000 )
492 term.Text = term.Text.Left( 1000 );
494 term.Normalized =
true;
498 int matchers_fired = 0;
502 score += 8 * term.Score;
504 else if(
Find( term.Text, matchers_fired, found_pos ) )
507 score += 2 * term.Score;
524 std::unique_ptr<EDA_PATTERN_MATCH> aMatcher )
526 if ( aMatcher->SetPattern( aPattern ) )
527 m_matchers.push_back( std::move( aMatcher ) );
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)
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.