From 2d5e6330e9901d4bb7c1abbf5e5a78f4e8b30004 Mon Sep 17 00:00:00 2001 From: sbh Date: Mon, 13 Apr 2015 18:37:38 +0300 Subject: [PATCH] Variable validation without regexp --- .../ParametersPlugin_Validators.cpp | 17 +++++++++++++++-- .../ParametersPlugin_Validators.h | 6 +++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.cpp b/src/ParametersPlugin/ParametersPlugin_Validators.cpp index dc7eb066c..788e16398 100644 --- a/src/ParametersPlugin/ParametersPlugin_Validators.cpp +++ b/src/ParametersPlugin/ParametersPlugin_Validators.cpp @@ -13,7 +13,6 @@ ParametersPlugin_VariableValidator::ParametersPlugin_VariableValidator() { - myPyVariableRegex = std::regex("[_a-zA-Z][a-zA-Z0-9_]*"); } ParametersPlugin_VariableValidator::~ParametersPlugin_VariableValidator() @@ -24,10 +23,24 @@ bool ParametersPlugin_VariableValidator::isValid(const AttributePtr& theAttribut const std::list& theArguments) const { AttributeStringPtr aStrAttr = std::dynamic_pointer_cast(theAttribute); - bool result = std::regex_match(aStrAttr->value(), myPyVariableRegex); + bool result = isVariable(aStrAttr->value()); return result; } +bool ParametersPlugin_VariableValidator::isVariable(const std::string& theString) const +{ + std::string::const_iterator it = theString.begin(); + if (!(isalpha(*it) || (*it) == '_') || it == theString.end()) + return false; + it++; + for ( ; it != theString.end(); ++it ) { + if(!(isalnum(*it) || (*it) == '_')) { + return false; + } + } + return true; +} + ParametersPlugin_ExpressionValidator::ParametersPlugin_ExpressionValidator() { diff --git a/src/ParametersPlugin/ParametersPlugin_Validators.h b/src/ParametersPlugin/ParametersPlugin_Validators.h index 3a4db55c8..be8f285b5 100644 --- a/src/ParametersPlugin/ParametersPlugin_Validators.h +++ b/src/ParametersPlugin/ParametersPlugin_Validators.h @@ -13,7 +13,6 @@ #include -#include #include class ParametersPlugin_VariableValidator : public ModelAPI_AttributeValidator @@ -28,8 +27,9 @@ class ParametersPlugin_VariableValidator : public ModelAPI_AttributeValidator PARAMETERSPLUGIN_EXPORT virtual bool isValid(const AttributePtr& theAttribute, const std::list& theArguments) const; - private: - std::regex myPyVariableRegex; + protected: + PARAMETERSPLUGIN_EXPORT bool isVariable(const std::string& theString) const; + }; class ParametersPlugin_ExpressionValidator: public ModelAPI_AttributeValidator -- 2.39.2