28#include <wx/tokenzr.h>
32#define CLAMPED_VAL_INT_MAX( x ) std::min( x, static_cast<size_t>( std::numeric_limits<int>::max() ) )
51 if( loc == wxNOT_FOUND )
54 return { loc,
static_cast<int>(
m_pattern.size() ) };
69 wxLog::SetLogLevel( level );
81 if( aPattern.StartsWith(
"^" ) && aPattern.EndsWith(
"$" ) )
85 else if( aPattern.StartsWith(
"/" ) )
109 wxString pattern( aPattern );
111 if( !pattern.StartsWith( wxT(
"^" ) ) )
112 pattern = wxT(
"^" ) + pattern;
114 if( !pattern.EndsWith( wxT(
"$" ) ) )
115 pattern += wxT(
"$" );
131 if(
m_regex.Matches( aCandidate ) )
134 m_regex.GetMatch( &start, &len, 0 );
148 if( loc == wxNOT_FOUND )
151 return { loc,
static_cast<int>(
m_pattern.size() ) };
162 regex.Alloc( 2 * aPattern.Length() );
164 const wxString to_replace = wxT(
".*+?^${}()|[]/\\" );
166 for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
176 regex += wxT(
".*" );
178 else if( to_replace.Find( c ) != wxNOT_FOUND )
211 regex.Alloc( 2 * aPattern.Length() );
213 const wxString to_replace = wxT(
".*+?^${}()|[]/\\" );
217 for( wxString::const_iterator it = aPattern.begin(); it < aPattern.end(); ++it )
227 regex += wxT(
".*" );
229 else if( to_replace.Find( c ) != wxNOT_FOUND )
231 regex += wxS(
"\\" );
248 wxRegEx regex_search( R
"(^(\w+)(<|<=|=|>=|>)([-+]?[\d.]*)(\w*)$)", wxRE_ADVANCED );
250 bool matches = regex_search.IsValid() && regex_search.Matches( aPattern );
252 if( !matches || regex_search.GetMatchCount() < 5 )
256 wxString key = regex_search.GetMatch( aPattern, 1 );
257 wxString rel = regex_search.GetMatch( aPattern, 2 );
258 wxString val = regex_search.GetMatch( aPattern, 3 );
259 wxString unit = regex_search.GetMatch( aPattern, 4 );
263 if( rel == wxS(
"<" ) )
265 else if( rel == wxS(
"<=" ) )
267 else if( rel == wxS(
"=" ) )
269 else if( rel == wxS(
">=" ) )
271 else if( rel == wxS(
">" ) )
282 else if( !val.ToCDouble( &
m_value ) )
287 auto unit_it =
m_units.find( unit.Lower() );
308 wxStringTokenizer tokenizer( aCandidate );
311 while( tokenizer.HasMoreTokens() )
313 const wxString token = tokenizer.GetNextToken();
314 int found_delta =
FindOne( token );
318 size_t found = (size_t) found_delta + lastpos;
322 lastpos = tokenizer.GetPosition();
331 wxRegEx regex_description( R
"((\w+)[=:]([-+]?[\d.]+)(\w*))", wxRE_ADVANCED );
333 bool matches = regex_description.IsValid() && regex_description.Matches( aCandidate );
339 regex_description.GetMatch( &start, &len, 0 );
340 wxString key = regex_description.GetMatch( aCandidate, 1 );
341 wxString val = regex_description.GetMatch( aCandidate, 2 );
342 wxString unit = regex_description.GetMatch( aCandidate, 3 );
346 if( key.Lower() !=
m_key )
351 if( !val.ToCDouble( &val_parsed ) )
354 auto unit_it =
m_units.find( unit.Lower() );
357 val_parsed *= unit_it->second;
366 case ANY:
return istart;
373 { wxS(
"p" ), 1e-12 },
374 { wxS(
"n" ), 1e-9 },
375 { wxS(
"u" ), 1e-6 },
376 { wxS(
"m" ), 1e-3 },
379 { wxS(
"meg" ), 1e6 },
381 { wxS(
"t" ), 1e12 },
382 { wxS(
"ki" ), 1024. },
383 { wxS(
"mi" ), 1048576. },
384 { wxS(
"gi" ), 1073741824. },
385 { wxS(
"ti" ), 1099511627776. } };
390 m_pattern( aPattern )
396 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
397 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
398 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_RELATIONAL>() );
401 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
405 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
406 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
407 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
411 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX_ANCHORED>() );
412 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
416 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
417 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
418 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
422 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_REGEX>() );
423 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_WILDCARD>() );
424 AddMatcher( aPattern, std::make_unique<EDA_PATTERN_MATCH_SUBSTR>() );
433 aMatchersTriggered = 0;
435 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
441 aMatchersTriggered += 1;
444 aPosition = local_find.
start;
454 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
456 if( matcher->Find( aTerm ).start >= 0 )
466 for(
const std::unique_ptr<EDA_PATTERN_MATCH>& matcher :
m_matchers )
468 if( matcher->Find( aTerm ).start == 0 )
482 if( !term.Normalized )
484 term.Text = term.Text.MakeLower().Trim(
false ).Trim(
true );
485 term.Normalized =
true;
489 int matchers_fired = 0;
493 score += 8 * term.Score;
495 else if(
Find( term.Text, matchers_fired, found_pos ) )
498 score += 2 * term.Score;
515 std::unique_ptr<EDA_PATTERN_MATCH> aMatcher )
517 if ( aMatcher->SetPattern( aPattern ) )
518 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)
EDA_COMBINED_MATCHER(const wxString &aPattern, COMBINED_MATCHER_CONTEXT aContext)
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...
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