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 int Find (const wxString &aCandidate) const override
 Return the location 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, NONE
}
 

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 131 of file eda_pattern_match.h.

Member Enumeration Documentation

◆ RELATION

Member Function Documentation

◆ Find()

int EDA_PATTERN_MATCH_RELATIONAL::Find ( const wxString &  aCandidate) const
overridevirtual

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

Otherwise, return EDA_PATTERN_NOT_FOUND.

Implements EDA_PATTERN_MATCH.

Definition at line 256 of file eda_pattern_match.cpp.

257 {
258  wxStringTokenizer tokenizer( aCandidate );
259  size_t lastpos = 0;
260 
261  while( tokenizer.HasMoreTokens() )
262  {
263  const wxString token = tokenizer.GetNextToken();
264  int found_delta = FindOne( token );
265 
266  if( found_delta != EDA_PATTERN_NOT_FOUND )
267  {
268  size_t found = (size_t) found_delta + lastpos;
269  return ( found > INT_MAX ) ? INT_MAX : (int) found;
270  }
271 
272  lastpos = tokenizer.GetPosition();
273  }
274 
275  return EDA_PATTERN_NOT_FOUND;
276 }
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 279 of file eda_pattern_match.cpp.

280 {
281  bool matches = m_regex_description.Matches( aCandidate );
282 
283  if( !matches )
284  return EDA_PATTERN_NOT_FOUND;
285 
286  size_t start, len;
287  m_regex_description.GetMatch( &start, &len, 0 );
288  wxString key = m_regex_description.GetMatch( aCandidate, 1 );
289  wxString val = m_regex_description.GetMatch( aCandidate, 2 );
290  wxString unit = m_regex_description.GetMatch( aCandidate, 3 );
291 
292  int istart = ( start > INT_MAX ) ? INT_MAX : start;
293 
294  if( key.Lower() != m_key )
295  return EDA_PATTERN_NOT_FOUND;
296 
297  double val_parsed;
298 
299  if( !val.ToCDouble( &val_parsed ) )
300  return EDA_PATTERN_NOT_FOUND;
301 
302  auto unit_it = m_units.find( unit.Lower() );
303 
304  if( unit_it != m_units.end() )
305  val_parsed *= unit_it->second;
306 
307  switch( m_relation )
308  {
309  case LT: return val_parsed < m_value ? istart : EDA_PATTERN_NOT_FOUND;
310  case LE: return val_parsed <= m_value ? istart : EDA_PATTERN_NOT_FOUND;
311  case EQ: return val_parsed == m_value ? istart : EDA_PATTERN_NOT_FOUND;
312  case GE: return val_parsed >= m_value ? istart : EDA_PATTERN_NOT_FOUND;
313  case GT: return val_parsed > m_value ? istart : EDA_PATTERN_NOT_FOUND;
314  case NONE: return istart;
315  default: return EDA_PATTERN_NOT_FOUND;
316  }
317 }
static const int EDA_PATTERN_NOT_FOUND
static const std::map< wxString, double > m_units

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

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 250 of file eda_pattern_match.cpp.

251 {
252  return m_pattern;
253 }

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 199 of file eda_pattern_match.cpp.

200 {
201  bool matches = m_regex_search.Matches( aPattern );
202 
203  if( !matches || m_regex_search.GetMatchCount() < 5 )
204  return false;
205 
206  m_pattern = aPattern;
207  wxString key = m_regex_search.GetMatch( aPattern, 1 );
208  wxString rel = m_regex_search.GetMatch( aPattern, 2 );
209  wxString val = m_regex_search.GetMatch( aPattern, 3 );
210  wxString unit = m_regex_search.GetMatch( aPattern, 4 );
211 
212  m_key = key.Lower();
213 
214  if( rel == "<" )
215  m_relation = LT;
216  else if( rel == "<=" )
217  m_relation = LE;
218  else if( rel == "=" )
219  m_relation = EQ;
220  else if( rel == ">=" )
221  m_relation = GE;
222  else if( rel == ">" )
223  m_relation = GT;
224  else
225  return false;
226 
227  if( val == "" )
228  {
229  // Matching on empty values keeps the match list from going empty when
230  // the user types the relational operator character, which helps prevent
231  // confusion.
232  m_relation = NONE;
233  }
234  else if( !val.ToCDouble( &m_value ) )
235  return false;
236 
237  auto unit_it = m_units.find( unit.Lower() );
238 
239  if( unit_it != m_units.end() )
240  m_value *= unit_it->second;
241  else
242  return false;
243 
244  m_pattern = aPattern;
245 
246  return true;
247 }
static const std::map< wxString, double > m_units

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

Member Data Documentation

◆ m_key

wxString EDA_PATTERN_MATCH_RELATIONAL::m_key
protected

Definition at line 144 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

◆ m_pattern

wxString EDA_PATTERN_MATCH_RELATIONAL::m_pattern
protected

Definition at line 143 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 148 of file eda_pattern_match.h.

Referenced by FindOne().

◆ m_regex_search

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search
staticprotected

Definition at line 149 of file eda_pattern_match.h.

Referenced by SetPattern().

◆ m_relation

RELATION EDA_PATTERN_MATCH_RELATIONAL::m_relation
protected

Definition at line 145 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:
= {
{ "p", 1e-12 },
{ "n", 1e-9 },
{ "u", 1e-6 },
{ "m", 1e-3 },
{ "", 1. },
{ "k", 1e3 },
{ "meg",1e6 },
{ "g", 1e9 },
{ "t", 1e12 },
{ "ki", 1024. },
{ "mi", 1048576. },
{ "gi", 1073741824. },
{ "ti", 1099511627776. } }

Definition at line 150 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

◆ m_value

double EDA_PATTERN_MATCH_RELATIONAL::m_value
protected

Definition at line 146 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().


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