KiCad PCB EDA Suite
REGEX_VALIDATOR Class Reference

Custom validator that checks verifies that a string exactly matches a regular expression. More...

#include <validators.h>

Inheritance diagram for REGEX_VALIDATOR:

Public Member Functions

 REGEX_VALIDATOR (const wxString &aRegEx, wxString *aValue=NULL)
 
 REGEX_VALIDATOR (const wxString &aRegEx, int aFlags, wxString *aValue=NULL)
 
 REGEX_VALIDATOR (const REGEX_VALIDATOR &aOther)
 
virtual wxObject * Clone () const override
 
bool Validate (wxWindow *aParent) override
 

Protected Member Functions

void compileRegEx (const wxString &aRegEx, int aFlags)
 

Compiles and stores a regular expression

More...
 

Protected Attributes

wxString m_regExString
 

Original regular expression (for copy constructor)

More...
 
int m_regExFlags
 

Original compilation flags (for copy constructor)

More...
 
wxRegEx m_regEx
 

Compiled regex

More...
 

Detailed Description

Custom validator that checks verifies that a string exactly matches a regular expression.

Definition at line 116 of file validators.h.

Constructor & Destructor Documentation

◆ REGEX_VALIDATOR() [1/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const wxString &  aRegEx,
wxString *  aValue = NULL 
)
inline
Parameters
aRegExis a regular expression to validate strings.
aValueis a pointer to a wxString containing the value to validate.

Definition at line 123 of file validators.h.

124  : wxTextValidator( wxFILTER_NONE, aValue )
125  {
126  compileRegEx( aRegEx, wxRE_DEFAULT );
127  }
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:264

References compileRegEx().

Referenced by Clone().

◆ REGEX_VALIDATOR() [2/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const wxString &  aRegEx,
int  aFlags,
wxString *  aValue = NULL 
)
inline
Parameters
aRegExis a regular expression to validate strings.
aFlagsare compilation flags (normally wxRE_DEFAULT).
aValueis a pointer to a wxString containing the value to validate.

Definition at line 134 of file validators.h.

135  : wxTextValidator( wxFILTER_NONE, aValue )
136  {
137  compileRegEx( aRegEx, aFlags );
138  }
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:264

References compileRegEx().

◆ REGEX_VALIDATOR() [3/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const REGEX_VALIDATOR aOther)
inline

Definition at line 140 of file validators.h.

141  {
142  wxValidator::Copy( aOther );
143  compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
144  }
wxString m_regExString
Original regular expression (for copy constructor)
Definition: validators.h:158
void compileRegEx(const wxString &aRegEx, int aFlags)
Compiles and stores a regular expression
Definition: validators.cpp:264
int m_regExFlags
Original compilation flags (for copy constructor)
Definition: validators.h:161

References compileRegEx(), m_regExFlags, and m_regExString.

Member Function Documentation

◆ Clone()

virtual wxObject* REGEX_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 146 of file validators.h.

147  {
148  return new REGEX_VALIDATOR( *this );
149  }
REGEX_VALIDATOR(const wxString &aRegEx, wxString *aValue=NULL)
Definition: validators.h:123

References REGEX_VALIDATOR().

◆ compileRegEx()

void REGEX_VALIDATOR::compileRegEx ( const wxString &  aRegEx,
int  aFlags 
)
protected

Compiles and stores a regular expression

Definition at line 264 of file validators.cpp.

265 {
266  if( !m_regEx.Compile( aRegEx, aFlags ) )
267  {
268  throw std::runtime_error( "REGEX_VALIDATOR: Invalid regular expression: "
269  + aRegEx.ToStdString() );
270  }
271 
272  m_regExString = aRegEx;
273  m_regExFlags = aFlags;
274 }
wxString m_regExString
Original regular expression (for copy constructor)
Definition: validators.h:158
wxRegEx m_regEx
Compiled regex
Definition: validators.h:164
int m_regExFlags
Original compilation flags (for copy constructor)
Definition: validators.h:161

References m_regEx, m_regExFlags, and m_regExString.

Referenced by REGEX_VALIDATOR().

◆ Validate()

bool REGEX_VALIDATOR::Validate ( wxWindow *  aParent)
override

Definition at line 226 of file validators.cpp.

227 {
228  // If window is disabled, simply return
229  if( !m_validatorWindow->IsEnabled() )
230  return true;
231 
232  wxTextEntry* const textEntry = GetTextEntry();
233 
234  if( !textEntry )
235  return false;
236 
237  bool valid = true;
238  const wxString& value = textEntry->GetValue();
239 
240  if( m_regEx.Matches( value ) )
241  {
242  size_t start, len;
243  m_regEx.GetMatch( &start, &len );
244 
245  if( start != 0 || len != value.Length() ) // whole string must match
246  valid = false;
247  }
248  else // no match at all
249  {
250  valid = false;
251  }
252 
253  if( !valid )
254  {
255  m_validatorWindow->SetFocus();
256  DisplayError( aParent, wxString::Format( _( "Incorrect value: %s" ), value ) );
257  return false;
258  }
259 
260  return true;
261 }
wxRegEx m_regEx
Compiled regex
Definition: validators.h:164
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:244

References DisplayError(), Format(), and m_regEx.

Member Data Documentation

◆ m_regEx

wxRegEx REGEX_VALIDATOR::m_regEx
protected

Compiled regex

Definition at line 164 of file validators.h.

Referenced by compileRegEx(), and Validate().

◆ m_regExFlags

int REGEX_VALIDATOR::m_regExFlags
protected

Original compilation flags (for copy constructor)

Definition at line 161 of file validators.h.

Referenced by compileRegEx(), and REGEX_VALIDATOR().

◆ m_regExString

wxString REGEX_VALIDATOR::m_regExString
protected

Original regular expression (for copy constructor)

Definition at line 158 of file validators.h.

Referenced by compileRegEx(), and REGEX_VALIDATOR().


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