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

269{
270 wxStringTokenizer tokenizer( aCandidate );
271 size_t lastpos = 0;
272
273 while( tokenizer.HasMoreTokens() )
274 {
275 const wxString token = tokenizer.GetNextToken();
276 int found_delta = FindOne( token );
277
278 if( found_delta != EDA_PATTERN_NOT_FOUND )
279 {
280 size_t found = (size_t) found_delta + lastpos;
281 return { static_cast<int>( std::min( found, static_cast<size_t>( INT_MAX ) ) ), 0 };
282 }
283
284 lastpos = tokenizer.GetPosition();
285 }
286
287 return {};
288}
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 291 of file eda_pattern_match.cpp.

292{
293 bool matches = m_regex_description.Matches( aCandidate );
294
295 if( !matches )
297
298 size_t start, len;
299 m_regex_description.GetMatch( &start, &len, 0 );
300 wxString key = m_regex_description.GetMatch( aCandidate, 1 );
301 wxString val = m_regex_description.GetMatch( aCandidate, 2 );
302 wxString unit = m_regex_description.GetMatch( aCandidate, 3 );
303
304 int istart = ( start > INT_MAX ) ? INT_MAX : start;
305
306 if( key.Lower() != m_key )
308
309 double val_parsed;
310
311 if( !val.ToCDouble( &val_parsed ) )
313
314 auto unit_it = m_units.find( unit.Lower() );
315
316 if( unit_it != m_units.end() )
317 val_parsed *= unit_it->second;
318
319 switch( m_relation )
320 {
321 case LT: return val_parsed < m_value ? istart : EDA_PATTERN_NOT_FOUND;
322 case LE: return val_parsed <= m_value ? istart : EDA_PATTERN_NOT_FOUND;
323 case EQ: return val_parsed == m_value ? istart : EDA_PATTERN_NOT_FOUND;
324 case GE: return val_parsed >= m_value ? istart : EDA_PATTERN_NOT_FOUND;
325 case GT: return val_parsed > m_value ? istart : EDA_PATTERN_NOT_FOUND;
326 case ANY: return istart;
327 default: return EDA_PATTERN_NOT_FOUND;
328 }
329}
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 261 of file eda_pattern_match.cpp.

262{
263 return m_pattern;
264}

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

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

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

Referenced by FindOne(), and SetPattern().

◆ m_pattern

wxString EDA_PATTERN_MATCH_RELATIONAL::m_pattern
protected

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

Referenced by FindOne().

◆ m_regex_search

wxRegEx EDA_PATTERN_MATCH_RELATIONAL::m_regex_search
staticprotected

Definition at line 165 of file eda_pattern_match.h.

Referenced by SetPattern().

◆ m_relation

RELATION EDA_PATTERN_MATCH_RELATIONAL::m_relation
protected

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

Referenced by FindOne(), and SetPattern().

◆ m_value

double EDA_PATTERN_MATCH_RELATIONAL::m_value
protected

Definition at line 162 of file eda_pattern_match.h.

Referenced by FindOne(), and SetPattern().


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