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

References EDA_PATTERN_NOT_FOUND, and FindOne().

220 {
221  wxStringTokenizer tokenizer( aCandidate );
222  size_t lastpos = 0;
223 
224  while( tokenizer.HasMoreTokens() )
225  {
226  const wxString token = tokenizer.GetNextToken();
227  int found_delta = FindOne( token );
228 
229  if( found_delta != EDA_PATTERN_NOT_FOUND )
230  {
231  size_t found = (size_t) found_delta + lastpos;
232  return ( found > INT_MAX ) ? INT_MAX : (int) found;
233  }
234 
235  lastpos = tokenizer.GetPosition();
236  }
237 
238  return EDA_PATTERN_NOT_FOUND;
239 }
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 242 of file eda_pattern_match.cpp.

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

243 {
244  bool matches = m_regex_description.Matches( aCandidate );
245 
246  if( !matches )
247  return EDA_PATTERN_NOT_FOUND;
248 
249  size_t start, len;
250  m_regex_description.GetMatch( &start, &len, 0 );
251  wxString key = m_regex_description.GetMatch( aCandidate, 1 );
252  wxString val = m_regex_description.GetMatch( aCandidate, 2 );
253  wxString unit = m_regex_description.GetMatch( aCandidate, 3 );
254 
255  int istart = ( start > INT_MAX ) ? INT_MAX : start;
256 
257  if( key.Lower() != m_key )
258  return EDA_PATTERN_NOT_FOUND;
259 
260  double val_parsed;
261 
262  if( !val.ToCDouble( &val_parsed ) )
263  return EDA_PATTERN_NOT_FOUND;
264 
265  auto unit_it = m_units.find( unit.Lower() );
266 
267  if( unit_it != m_units.end() )
268  val_parsed *= unit_it->second;
269 
270  switch( m_relation )
271  {
272  case LT: return val_parsed < m_value ? istart : EDA_PATTERN_NOT_FOUND;
273  case LE: return val_parsed <= m_value ? istart : EDA_PATTERN_NOT_FOUND;
274  case EQ: return val_parsed == m_value ? istart : EDA_PATTERN_NOT_FOUND;
275  case GE: return val_parsed >= m_value ? istart : EDA_PATTERN_NOT_FOUND;
276  case GT: return val_parsed > m_value ? istart : EDA_PATTERN_NOT_FOUND;
277  case NONE: return istart;
278  default: return EDA_PATTERN_NOT_FOUND;
279  }
280 }
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 213 of file eda_pattern_match.cpp.

References m_pattern.

214 {
215  return m_pattern;
216 }
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 162 of file eda_pattern_match.cpp.

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

163 {
164  bool matches = m_regex_search.Matches( aPattern );
165 
166  if( !matches || m_regex_search.GetMatchCount() < 5 )
167  return false;
168 
169  m_pattern = aPattern;
170  wxString key = m_regex_search.GetMatch( aPattern, 1 );
171  wxString rel = m_regex_search.GetMatch( aPattern, 2 );
172  wxString val = m_regex_search.GetMatch( aPattern, 3 );
173  wxString unit = m_regex_search.GetMatch( aPattern, 4 );
174 
175  m_key = key.Lower();
176 
177  if( rel == "<" )
178  m_relation = LT;
179  else if( rel == "<=" )
180  m_relation = LE;
181  else if( rel == "=" )
182  m_relation = EQ;
183  else if( rel == ">=" )
184  m_relation = GE;
185  else if( rel == ">" )
186  m_relation = GT;
187  else
188  return false;
189 
190  if( val == "" )
191  {
192  // Matching on empty values keeps the match list from going empty when
193  // the user types the relational operator character, which helps prevent
194  // confusion.
195  m_relation = NONE;
196  }
197  else if( !val.ToCDouble( &m_value ) )
198  return false;
199 
200  auto unit_it = m_units.find( unit.Lower() );
201 
202  if( unit_it != m_units.end() )
203  m_value *= unit_it->second;
204  else
205  return false;
206 
207  m_pattern = aPattern;
208 
209  return true;
210 }
static const std::map< wxString, double > m_units

Member Data Documentation

wxString EDA_PATTERN_MATCH_RELATIONAL::m_key
protected

Definition at line 137 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

wxString EDA_PATTERN_MATCH_RELATIONAL::m_pattern
protected

Definition at line 136 of file eda_pattern_match.h.

Referenced by GetPattern(), and SetPattern().

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_description
staticprotected

Definition at line 141 of file eda_pattern_match.h.

Referenced by FindOne().

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search
staticprotected

Definition at line 142 of file eda_pattern_match.h.

Referenced by SetPattern().

RELATION EDA_PATTERN_MATCH_RELATIONAL::m_relation
protected

Definition at line 138 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().

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

Referenced by FindOne(), and SetPattern().

double EDA_PATTERN_MATCH_RELATIONAL::m_value
protected

Definition at line 139 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().


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