[edk2] [PATCH v1 2/5] BaseTools: Expression refactor function

Jaben Carsey posted 5 patches 6 years, 9 months ago
[edk2] [PATCH v1 2/5] BaseTools: Expression refactor function
Posted by Jaben Carsey 6 years, 9 months ago
The function is about C Names, not C Strings.
Move the re.compile outside the function call

Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jaben Carsey <jaben.carsey@intel.com>
---
 BaseTools/Source/Python/Common/Expression.py | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py
index bcb27ec11fd5..c7037dd0d00b 100644
--- a/BaseTools/Source/Python/Common/Expression.py
+++ b/BaseTools/Source/Python/Common/Expression.py
@@ -40,6 +40,8 @@ ERR_ARRAY_ELE           = 'This must be HEX value for NList or Array: [%s].'
 ERR_EMPTY_EXPR          = 'Empty expression is not allowed.'
 ERR_IN_OPERAND          = 'Macro after IN operator can only be: $(FAMILY), $(ARCH), $(TOOL_CHAIN_TAG) and $(TARGET).'
 
+__ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')
+
 ## SplitString
 #  Split string to list according double quote
 #  For example: abc"de\"f"ghi"jkl"mn will be: ['abc', '"de\"f"', 'ghi', '"jkl"', 'mn']
@@ -117,11 +119,8 @@ def SplitPcdValueString(String):
         RetList.append(Item)
     return RetList
 
-def IsValidCString(Str):
-    ValidString = re.compile(r'[_a-zA-Z][_0-9a-zA-Z]*$')
-    if not ValidString.match(Str):
-        return False
-    return True
+def IsValidCName(Str):
+    return True if __ValidString.match(Str) else False
 
 def BuildOptionValue(PcdValue, GuidDict):
     IsArray = False
@@ -912,7 +911,7 @@ class ValueExpressionEx(ValueExpression):
                             Item = Item.strip()
                             if LabelList:
                                 for Label in LabelList:
-                                    if not IsValidCString(Label):
+                                    if not IsValidCName(Label):
                                         raise BadExpression('%s is not a valid c variable name' % Label)
                                     if Label not in LabelDict.keys():
                                         LabelDict[Label] = str(LabelOffset)
-- 
2.16.2.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel