]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/String.py
Sync EDKII BaseTools to BaseTools project r2065.
[mirror_edk2.git] / BaseTools / Source / Python / Common / String.py
index 1f199fe2ca58ff832185dfea8b4efb342b4a16c7..195fa5c6cacc0c9d82f483ace03316d04ae67ac2 100644 (file)
@@ -23,6 +23,9 @@ import EdkLogger as EdkLogger
 from GlobalData import *\r
 from BuildToolError import *\r
 \r
+gHexVerPatt = re.compile('0x[a-f0-9]{4}[a-f0-9]{4}$',re.IGNORECASE)\r
+gHumanReadableVerPatt = re.compile(r'([1-9][0-9]*|0)\.[0-9]{1,2}$')\r
+\r
 ## GetSplitValueList\r
 #\r
 # Get a value list from a string with multiple values splited with SplitTag\r
@@ -279,9 +282,16 @@ def CleanString(Line, CommentCharacter = DataType.TAB_COMMENT_SPLIT, AllowCppSty
     if AllowCppStyleComment:\r
         Line = Line.replace(DataType.TAB_COMMENT_R8_SPLIT, CommentCharacter)\r
     #\r
-    # remove comments\r
+    # remove comments, but we should escape comment character in string\r
     #\r
-    Line = Line.split(CommentCharacter, 1)[0];\r
+    InString = False\r
+    for Index in range(0, len(Line)):\r
+        if Line[Index] == '"':\r
+            InString = not InString\r
+        elif Line[Index] == CommentCharacter and not InString:\r
+            Line = Line[0: Index]\r
+            break\r
+    \r
     #\r
     # remove whitespace again\r
     #\r
@@ -289,6 +299,50 @@ def CleanString(Line, CommentCharacter = DataType.TAB_COMMENT_SPLIT, AllowCppSty
 \r
     return Line\r
 \r
+## CleanString2\r
+#\r
+# Split comments in a string\r
+# Remove spaces\r
+#\r
+# @param Line:              The string to be cleaned\r
+# @param CommentCharacter:  Comment char, used to ignore comment content, default is DataType.TAB_COMMENT_SPLIT\r
+#\r
+# @retval Path Formatted path\r
+#\r
+def CleanString2(Line, CommentCharacter = DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):\r
+    #\r
+    # remove whitespace\r
+    #\r
+    Line = Line.strip();\r
+    #\r
+    # Replace R8's comment character\r
+    #\r
+    if AllowCppStyleComment:\r
+        Line = Line.replace(DataType.TAB_COMMENT_R8_SPLIT, CommentCharacter)\r
+    #\r
+    # separate comments and statements\r
+    #\r
+    LineParts = Line.split(CommentCharacter, 1);\r
+    #\r
+    # remove whitespace again\r
+    #\r
+    Line = LineParts[0].strip();\r
+    if len(LineParts) > 1:\r
+        Comment = LineParts[1].strip()\r
+        # Remove prefixed and trailing comment characters\r
+        Start = 0\r
+        End = len(Comment)\r
+        while Start < End and Comment.startswith(CommentCharacter, Start, End):\r
+            Start += 1\r
+        while End >= 0 and Comment.endswith(CommentCharacter, Start, End):\r
+            End -= 1\r
+        Comment = Comment[Start:End]\r
+        Comment = Comment.strip()\r
+    else:\r
+        Comment = ''\r
+\r
+    return Line, Comment\r
+\r
 ## GetMultipleValuesOfKeyFromLines\r
 #\r
 # Parse multiple strings to clean comment and spaces\r
@@ -326,6 +380,34 @@ def GetDefineValue(String, Key, CommentCharacter):
     String = CleanString(String)\r
     return String[String.find(Key + ' ') + len(Key + ' ') : ]\r
 \r
+## GetHexVerValue\r
+#\r
+# Get a Hex Version Value\r
+#\r
+# @param VerString:         The version string to be parsed\r
+#\r
+#\r
+# @retval:      If VerString is incorrectly formatted, return "None" which will break the build.\r
+#               If VerString is correctly formatted, return a Hex value of the Version Number (0xmmmmnnnn)\r
+#                   where mmmm is the major number and nnnn is the adjusted minor number.\r
+#\r
+def GetHexVerValue(VerString):\r
+    VerString = CleanString(VerString)\r
+\r
+    if gHumanReadableVerPatt.match(VerString):\r
+        ValueList = VerString.split('.')\r
+        Major = ValueList[0]\r
+        Minor = ValueList[1]\r
+        if len(Minor) == 1:\r
+            Minor += '0'\r
+        DeciValue = (int(Major) << 16) + int(Minor);\r
+        return "0x%08x"%DeciValue\r
+    elif gHexVerPatt.match(VerString):\r
+        return VerString\r
+    else:\r
+        return None\r
+\r
+\r
 ## GetSingleValueOfKeyFromLines\r
 #\r
 # Parse multiple strings as below to get value of each definition line\r