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

Member Function Documentation

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

References EDA_PATTERN_NOT_FOUND.

258 {
259  wxStringTokenizer tokenizer( aCandidate );
260  size_t lastpos = 0;
261 
262  while( tokenizer.HasMoreTokens() )
263  {
264  const wxString token = tokenizer.GetNextToken();
265  int found_delta = FindOne( token );
266 
267  if( found_delta != EDA_PATTERN_NOT_FOUND )
268  {
269  size_t found = (size_t) found_delta + lastpos;
270  return ( found > INT_MAX ) ? INT_MAX : (int) found;
271  }
272 
273  lastpos = tokenizer.GetPosition();
274  }
275 
276  return EDA_PATTERN_NOT_FOUND;
277 }
int FindOne(const wxString &aCandidate) const
static const int EDA_PATTERN_NOT_FOUND
int EDA_PATTERN_MATCH_RELATIONAL::FindOne ( const wxString &  aCandidate) const

Definition at line 280 of file eda_pattern_match.cpp.

References EDA_PATTERN_NOT_FOUND, m_regex_description, m_regex_search, m_units, and KIGFX::NONE.

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

Return the pattern passed to SetPattern().

Implements EDA_PATTERN_MATCH.

Definition at line 251 of file eda_pattern_match.cpp.

References EDA_PATTERN_MATCH_SUBSTR::m_pattern.

252 {
253  return m_pattern;
254 }
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 200 of file eda_pattern_match.cpp.

References EDA_PATTERN_MATCH_SUBSTR::m_pattern, and KIGFX::NONE.

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

Member Data Documentation

wxString EDA_PATTERN_MATCH_RELATIONAL::m_key
protected

Definition at line 144 of file eda_pattern_match.h.

wxString EDA_PATTERN_MATCH_RELATIONAL::m_pattern
protected

Definition at line 143 of file eda_pattern_match.h.

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_description
staticprotected

Definition at line 148 of file eda_pattern_match.h.

Referenced by FindOne().

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search
staticprotected

Definition at line 149 of file eda_pattern_match.h.

Referenced by FindOne().

RELATION EDA_PATTERN_MATCH_RELATIONAL::m_relation
protected

Definition at line 145 of file eda_pattern_match.h.

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().

double EDA_PATTERN_MATCH_RELATIONAL::m_value
protected

Definition at line 146 of file eda_pattern_match.h.


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