]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/String.py
There is a limitation on WINDOWS OS for the length of entire file path can’t be large...
[mirror_edk2.git] / BaseTools / Source / Python / Common / String.py
index 0f2a61b1b9793802e1679c2d2c6ab808eeed2810..4dc8e9b1168eb495bef4dfc4fca2faa97db9570a 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # This file is used to define common string related functions used in parsing process\r
 #\r
-# Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
 # This program and the accompanying materials\r
 # are licensed and made available under the terms and conditions of the BSD License\r
 # which accompanies this distribution.  The full text of the license may be found at\r
 #\r
 import re\r
 import DataType\r
-import os.path\r
+import Common.LongFilePathOs as os\r
 import string\r
 import EdkLogger as EdkLogger\r
 \r
 import GlobalData\r
 from BuildToolError import *\r
 from CommonDataClass.Exceptions import *\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\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
@@ -319,7 +320,7 @@ def NormPath(Path, Defines={}):
 #\r
 # @retval Path Formatted path\r
 #\r
-def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False):\r
+def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyleComment=False, BuildOption=False):\r
     #\r
     # remove whitespace\r
     #\r
@@ -343,7 +344,7 @@ def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyle
             Line = Line[0: Index]\r
             break\r
 \r
-    if CommentInString:\r
+    if CommentInString and BuildOption:\r
         Line = Line.replace('"', '')\r
         ChIndex = Line.find('#')\r
         while ChIndex >= 0:\r
@@ -368,7 +369,7 @@ def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyle
 \r
 ## CleanString2\r
 #\r
-# Split comments in a string\r
+# Split statement with comments in a string\r
 # Remove spaces\r
 #\r
 # @param Line:              The string to be cleaned\r
@@ -387,26 +388,20 @@ def CleanString2(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyl
     if AllowCppStyleComment:\r
         Line = Line.replace(DataType.TAB_COMMENT_EDK_SPLIT, CommentCharacter)\r
     #\r
-    # separate comments and statements\r
+    # separate comments and statements, but we should escape comment character in string\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
+    InString = False\r
+    CommentInString = False\r
+    Comment = ''\r
+    for Index in range(0, len(Line)):\r
+        if Line[Index] == '"':\r
+            InString = not InString\r
+        elif Line[Index] == CommentCharacter and InString:\r
+            CommentInString = True\r
+        elif Line[Index] == CommentCharacter and not InString:\r
+            Comment = Line[Index:].strip()\r
+            Line = Line[0:Index].strip()\r
+            break\r
 \r
     return Line, Comment\r
 \r
@@ -807,11 +802,25 @@ def StringToArray(String):
             return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String[2:-1]])\r
     elif String.startswith('"'):\r
         if String == "\"\"":\r
-            return "{0x00}";\r
+            return "{0x00,0x00}"\r
         else:\r
-            return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])\r
+            StringLen = len(String[1:-1])\r
+            if StringLen % 2:\r
+                return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])\r
+            else:\r
+                return "{%s, 0x00,0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]])\r
+    elif String.startswith('{'):\r
+        StringLen = len(String.split(","))\r
+        if StringLen % 2:\r
+            return "{%s, 0x00}" % ", ".join([ C for C in String[1:-1].split(',')])\r
+        else:\r
+            return "{%s}" % ", ".join([ C for C in String[1:-1].split(',')])\r
+        \r
     else:\r
-        return '{%s, 0}' % ', '.join(String.split())\r
+        if len(String.split()) % 2:\r
+            return '{%s, 0}' % ', '.join(String.split())\r
+        else:\r
+            return '{%s, 0,0}' % ', '.join(String.split())\r
 \r
 def StringArrayLength(String):\r
     if isinstance(String, unicode):\r