KiCad PCB EDA Suite
EDA_PATTERN_MATCH_RELATIONAL Class Reference

Relational match. More...

#include <eda_pattern_match.h>

Inheritance diagram for EDA_PATTERN_MATCH_RELATIONAL:
EDA_PATTERN_MATCH

Public Member Functions

virtual bool SetPattern (const wxString &aPattern) override
 Set the pattern against which candidates will be matched. More...
 
virtual wxString const & GetPattern () const override
 Return the pattern passed to SetPattern(). More...
 
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 pattern. More...
 
int FindOne (const wxString &aCandidate) const
 

Protected Types

enum  RELATION {
  LT , LE , EQ , GE ,
  GT , ANY
}
 

Protected Attributes

wxString m_pattern
 
wxString m_key
 
RELATION m_relation
 
double m_value
 

Static Protected Attributes

static wxRegEx m_regex_description
 
static wxRegEx m_regex_search
 
static const std::map< wxString, double > m_units
 

Detailed Description

Relational match.

Matches tokens of the format:

 key:value       or      key=value

with search patterns of the format:

 key<value, key<=value, key=value, key>=value, key>value

by parsing the value numerically and comparing.

Definition at line 154 of file eda_pattern_match.h.

Member Enumeration Documentation

◆ RELATION

Member Function Documentation

◆ Find()

EDA_PATTERN_MATCH::FIND_RESULT EDA_PATTERN_MATCH_RELATIONAL::Find ( const wxString &  aCandidate) const
overridevirtual

Return the location and possibly length of a match iff a given candidate string matches the set pattern.

Otherwise, return an invalid FIND_RESULT.

Implements EDA_PATTERN_MATCH.

Definition at line 285 of file eda_pattern_match.cpp.

286{
287 wxStringTokenizer tokenizer( aCandidate );
288 size_t lastpos = 0;
289
290 while( tokenizer.HasMoreTokens() )
291 {
292 const wxString token = tokenizer.GetNextToken();
293 int found_delta = FindOne( token );
294
295 if( found_delta != EDA_PATTERN_NOT_FOUND )
296 {
297 size_t found = (size_t) found_delta + lastpos;
298 return { static_cast<int>( std::min( found, static_cast<size_t>( INT_MAX ) ) ), 0 };
299 }
300
301 lastpos = tokenizer.GetPosition();
302 }
303
304 return {};
305}
int FindOne(const wxString &aCandidate) const
static const int EDA_PATTERN_NOT_FOUND

References EDA_PATTERN_NOT_FOUND, and FindOne().

◆ FindOne()

int EDA_PATTERN_MATCH_RELATIONAL::FindOne ( const wxString &  aCandidate) const

Definition at line 308 of file eda_pattern_match.cpp.

309{
310 bool matches = m_regex_description.Matches( aCandidate );
311
312 if( !matches )
314
315 size_t start, len;
316 m_regex_description.GetMatch( &start, &len, 0 );
317 wxString key = m_regex_description.GetMatch( aCandidate, 1 );
318 wxString val = m_regex_description.GetMatch( aCandidate, 2 );
319 wxString unit = m_regex_description.GetMatch( aCandidate, 3 );
320
321 int istart = ( start > INT_MAX ) ? INT_MAX : start;
322
323 if( key.Lower() != m_key )
325
326 double val_parsed;
327
328 if( !val.ToCDouble( &val_parsed ) )
330
331 auto unit_it = m_units.find( unit.Lower() );
332
333 if( unit_it != m_units.end() )
334 val_parsed *= unit_it->second;
335
336 switch( m_relation )
337 {
338 case LT: return val_parsed < m_value ? istart : EDA_PATTERN_NOT_FOUND;
339 case LE: return val_parsed <= m_value ? istart : EDA_PATTERN_NOT_FOUND;
340 case EQ: return val_parsed == m_value ? istart : EDA_PATTERN_NOT_FOUND;
341 case GE: return val_parsed >= m_value ? istart : EDA_PATTERN_NOT_FOUND;
342 case GT: return val_parsed > m_value ? istart : EDA_PATTERN_NOT_FOUND;
343 case ANY: return istart;
344 default: return EDA_PATTERN_NOT_FOUND;
345 }
346}
static const std::map< wxString, double > m_units

References ANY, EDA_PATTERN_NOT_FOUND, EQ, GE, GT, LE, LT, m_key, m_regex_description, m_relation, m_units, and m_value.

Referenced by Find().

◆ GetPattern()

wxString const & EDA_PATTERN_MATCH_RELATIONAL::GetPattern ( ) const
overridevirtual

Return the pattern passed to SetPattern().

Implements EDA_PATTERN_MATCH.

Definition at line 279 of file eda_pattern_match.cpp.

280{
281 return m_pattern;
282}

References m_pattern.

◆ SetPattern()

bool EDA_PATTERN_MATCH_RELATIONAL::SetPattern ( const wxString &  aPattern)
overridevirtual

Set the pattern against which candidates will be matched.

If the pattern can not be processed, returns false.

Implements EDA_PATTERN_MATCH.

Definition at line 227 of file eda_pattern_match.cpp.

228{
229 bool matches = m_regex_search.Matches( aPattern );
230
231 if( !matches || m_regex_search.GetMatchCount() < 5 )
232 return false;
233
234 m_pattern = aPattern;
235 wxString key = m_regex_search.GetMatch( aPattern, 1 );
236 wxString rel = m_regex_search.GetMatch( aPattern, 2 );
237 wxString val = m_regex_search.GetMatch( aPattern, 3 );
238 wxString unit = m_regex_search.GetMatch( aPattern, 4 );
239
240 m_key = key.Lower();
241
242 if( rel == wxS( "<" ) )
243 m_relation = LT;
244 else if( rel == wxS( "<=" ) )
245 m_relation = LE;
246 else if( rel == wxS( "=" ) )
247 m_relation = EQ;
248 else if( rel == wxS( ">=" ) )
249 m_relation = GE;
250 else if( rel == wxS( ">" ) )
251 m_relation = GT;
252 else
253 return false;
254
255 if( val == "" )
256 {
257 // Matching on empty values keeps the match list from going empty when the user
258 // types the relational operator character, which helps prevent confusion.
259 m_relation = ANY;
260 }
261 else if( !val.ToCDouble( &m_value ) )
262 {
263 return false;
264 }
265
266 auto unit_it = m_units.find( unit.Lower() );
267
268 if( unit_it != m_units.end() )
269 m_value *= unit_it->second;
270 else
271 return false;
272
273 m_pattern = aPattern;
274
275 return true;
276}

References ANY, EQ, GE, GT, LE, LT, m_key, m_pattern, m_regex_search, m_relation, m_units, and m_value.

Member Data Documentation

◆ m_key

wxString EDA_PATTERN_MATCH_RELATIONAL::m_key
protected

Definition at line 167 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

◆ m_pattern

wxString EDA_PATTERN_MATCH_RELATIONAL::m_pattern
protected

Definition at line 166 of file eda_pattern_match.h.

Referenced by GetPattern(), and SetPattern().

◆ m_regex_description

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_description
staticprotected

Definition at line 171 of file eda_pattern_match.h.

Referenced by FindOne().

◆ m_regex_search

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search
staticprotected

Definition at line 172 of file eda_pattern_match.h.

Referenced by SetPattern().

◆ m_relation

RELATION EDA_PATTERN_MATCH_RELATIONAL::m_relation
protected

Definition at line 168 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

◆ m_units

const std::map< wxString, double > EDA_PATTERN_MATCH_RELATIONAL::m_units
staticprotected
Initial value:
= {
{ 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. } }

Definition at line 173 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

◆ m_value

double EDA_PATTERN_MATCH_RELATIONAL::m_value
protected

Definition at line 169 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().


The documentation for this class was generated from the following files: