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=nullptr)
 
 REGEX_VALIDATOR (const wxString &aRegEx, int aFlags, wxString *aValue=nullptr)
 
 REGEX_VALIDATOR (const REGEX_VALIDATOR &aOther)
 
virtual wxObject * Clone () const override
 
bool Validate (wxWindow *aParent) override
 
const wxString & GetRegEx () const
 

Protected Member Functions

void compileRegEx (const wxString &aRegEx, int aFlags)
 < Compiles and stores a regular expression More...
 

Protected Attributes

wxString m_regExString
 Original compilation flags (for copy constructor) More...
 
int m_regExFlags
 Compiled regex. More...
 
wxRegEx m_regEx
 

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 = nullptr 
)
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:258

References compileRegEx().

Referenced by Clone().

◆ REGEX_VALIDATOR() [2/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const wxString &  aRegEx,
int  aFlags,
wxString *  aValue = nullptr 
)
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 }

References compileRegEx().

◆ REGEX_VALIDATOR() [3/3]

REGEX_VALIDATOR::REGEX_VALIDATOR ( const REGEX_VALIDATOR aOther)
inline

Definition at line 140 of file validators.h.

140 : wxTextValidator( aOther )
141 {
142 compileRegEx( aOther.m_regExString, aOther.m_regExFlags );
143 }
wxString m_regExString
Original compilation flags (for copy constructor)
Definition: validators.h:162
int m_regExFlags
Compiled regex.
Definition: validators.h:165

References compileRegEx(), m_regExFlags, and m_regExString.

Member Function Documentation

◆ Clone()

virtual wxObject * REGEX_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 145 of file validators.h.

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

References REGEX_VALIDATOR().

◆ compileRegEx()

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

< Compiles and stores a regular expression

Original regular expression (for copy constructor)

Definition at line 258 of file validators.cpp.

259{
260 if( !m_regEx.Compile( aRegEx, aFlags ) )
261 {
262 throw std::runtime_error( "REGEX_VALIDATOR: Invalid regular expression: "
263 + aRegEx.ToStdString() );
264 }
265
266 m_regExString = aRegEx;
267 m_regExFlags = aFlags;
268}
wxRegEx m_regEx
Definition: validators.h:168

References m_regEx, m_regExFlags, and m_regExString.

Referenced by REGEX_VALIDATOR().

◆ GetRegEx()

const wxString & REGEX_VALIDATOR::GetRegEx ( ) const
inline

Definition at line 152 of file validators.h.

153 {
154 return m_regExString;
155 }

References m_regExString.

◆ Validate()

bool REGEX_VALIDATOR::Validate ( wxWindow *  aParent)
override

Definition at line 220 of file validators.cpp.

221{
222 // If window is disabled, simply return
223 if( !m_validatorWindow->IsEnabled() )
224 return true;
225
226 wxTextEntry* const textEntry = GetTextEntry();
227
228 if( !textEntry )
229 return false;
230
231 bool valid = true;
232 const wxString& value = textEntry->GetValue();
233
234 if( m_regEx.Matches( value ) )
235 {
236 size_t start, len;
237 m_regEx.GetMatch( &start, &len );
238
239 if( start != 0 || len != value.Length() ) // whole string must match
240 valid = false;
241 }
242 else // no match at all
243 {
244 valid = false;
245 }
246
247 if( !valid )
248 {
249 m_validatorWindow->SetFocus();
250 DisplayError( aParent, wxString::Format( _( "Incorrect value: %s" ), value ) );
251 return false;
252 }
253
254 return true;
255}
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:300
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

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

Member Data Documentation

◆ m_regEx

wxRegEx REGEX_VALIDATOR::m_regEx
protected

Definition at line 168 of file validators.h.

Referenced by compileRegEx(), and Validate().

◆ m_regExFlags

int REGEX_VALIDATOR::m_regExFlags
protected

Compiled regex.

Definition at line 165 of file validators.h.

Referenced by compileRegEx(), and REGEX_VALIDATOR().

◆ m_regExString

wxString REGEX_VALIDATOR::m_regExString
protected

Original compilation flags (for copy constructor)

Definition at line 162 of file validators.h.

Referenced by compileRegEx(), GetRegEx(), and REGEX_VALIDATOR().


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