X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FCommon%2FString.py;h=5dc5b85dc5a459fb03358fd9ff07571cdc1de3b1;hp=c282326677f6f49d6b58fc5fb0adc424d8dc6901;hb=066c71544ed1c0e1a703b26982f9da60d21bcc5a;hpb=4afd3d042215afe68d00b9ab8c32f063a3a1c03f diff --git a/BaseTools/Source/Python/Common/String.py b/BaseTools/Source/Python/Common/String.py index c282326677..5dc5b85dc5 100644 --- a/BaseTools/Source/Python/Common/String.py +++ b/BaseTools/Source/Python/Common/String.py @@ -1,7 +1,7 @@ ## @file # This file is used to define common string related functions used in parsing process # -# Copyright (c) 2007 - 2008, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BSD License # which accompanies this distribution. The full text of the license may be found at @@ -16,13 +16,15 @@ # import re import DataType -import os.path +import Common.LongFilePathOs as os import string import EdkLogger as EdkLogger import GlobalData from BuildToolError import * from CommonDataClass.Exceptions import * +from Common.LongFilePathSupport import OpenLongFilePath as open +from Common.MultipleWorkspace import MultipleWorkspace as mws gHexVerPatt = re.compile('0x[a-f0-9]{4}[a-f0-9]{4}$', re.IGNORECASE) gHumanReadableVerPatt = re.compile(r'([1-9][0-9]*|0)\.[0-9]{1,2}$') @@ -43,25 +45,36 @@ def GetSplitValueList(String, SplitTag=DataType.TAB_VALUE_SPLIT, MaxSplit= -1): ValueList = [] Last = 0 Escaped = False - InString = False + InSingleQuoteString = False + InDoubleQuoteString = False + InParenthesis = 0 for Index in range(0, len(String)): Char = String[Index] if not Escaped: # Found a splitter not in a string, split it - if not InString and Char == SplitTag: + if (not InSingleQuoteString or not InDoubleQuoteString) and InParenthesis == 0 and Char == SplitTag: ValueList.append(String[Last:Index].strip()) Last = Index + 1 if MaxSplit > 0 and len(ValueList) >= MaxSplit: break - if Char == '\\' and InString: + if Char == '\\' and (InSingleQuoteString or InDoubleQuoteString): Escaped = True - elif Char == '"': - if not InString: - InString = True + elif Char == '"' and not InSingleQuoteString: + if not InDoubleQuoteString: + InDoubleQuoteString = True else: - InString = False + InDoubleQuoteString = False + elif Char == "'" and not InDoubleQuoteString: + if not InSingleQuoteString: + InSingleQuoteString = True + else: + InSingleQuoteString = False + elif Char == '(': + InParenthesis = InParenthesis + 1 + elif Char == ')': + InParenthesis = InParenthesis - 1 else: Escaped = False @@ -271,7 +284,8 @@ def ReplaceMacro(String, MacroDefinitions={}, SelfReplacement=False, RaiseError= if SelfReplacement: String = String.replace("$(%s)" % Macro, '') continue - String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro]) + if "$(%s)" % Macro not in MacroDefinitions[Macro]: + String = String.replace("$(%s)" % Macro, MacroDefinitions[Macro]) # in case there's macro not defined if String == LastString: break @@ -303,6 +317,11 @@ def NormPath(Path, Defines={}): # To local path format # Path = os.path.normpath(Path) + if Path.startswith(GlobalData.gWorkspace) and not Path.startswith(GlobalData.gBuildDirectory) and not os.path.exists(Path): + Path = Path[len (GlobalData.gWorkspace):] + if Path[0] == os.path.sep: + Path = Path[1:] + Path = mws.join(GlobalData.gWorkspace, Path) if IsRelativePath and Path[0] != '.': Path = os.path.join('.', Path) @@ -332,14 +351,17 @@ def CleanString(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyle # # remove comments, but we should escape comment character in string # - InString = False + InDoubleQuoteString = False + InSingleQuoteString = False CommentInString = False for Index in range(0, len(Line)): - if Line[Index] == '"': - InString = not InString - elif Line[Index] == CommentCharacter and InString : + if Line[Index] == '"' and not InSingleQuoteString: + InDoubleQuoteString = not InDoubleQuoteString + elif Line[Index] == "'" and not InDoubleQuoteString: + InSingleQuoteString = not InSingleQuoteString + elif Line[Index] == CommentCharacter and (InSingleQuoteString or InDoubleQuoteString): CommentInString = True - elif Line[Index] == CommentCharacter and not InString : + elif Line[Index] == CommentCharacter and not (InSingleQuoteString or InDoubleQuoteString): Line = Line[0: Index] break @@ -389,28 +411,21 @@ def CleanString2(Line, CommentCharacter=DataType.TAB_COMMENT_SPLIT, AllowCppStyl # # separate comments and statements, but we should escape comment character in string # - InString = False + InDoubleQuoteString = False + InSingleQuoteString = False CommentInString = False Comment = '' for Index in range(0, len(Line)): - if Line[Index] == '"': - InString = not InString - elif Line[Index] == CommentCharacter and InString: + if Line[Index] == '"' and not InSingleQuoteString: + InDoubleQuoteString = not InDoubleQuoteString + elif Line[Index] == "'" and not InDoubleQuoteString: + InSingleQuoteString = not InSingleQuoteString + elif Line[Index] == CommentCharacter and (InDoubleQuoteString or InSingleQuoteString): CommentInString = True - elif Line[Index] == CommentCharacter and not InString: + elif Line[Index] == CommentCharacter and not (InDoubleQuoteString or InSingleQuoteString): Comment = Line[Index:].strip() Line = Line[0:Index].strip() break - if Comment: - # Remove prefixed and trailing comment characters - Start = 0 - End = len(Comment) - while Start < End and Comment.startswith(CommentCharacter, Start, End): - Start += 1 - while End >= 0 and Comment.endswith(CommentCharacter, Start, End): - End -= 1 - Comment = Comment[Start:End] - Comment = Comment.strip() return Line, Comment @@ -619,7 +634,7 @@ def PreCheck(FileName, FileContent, SupSectionTag): # @retval True The file type is correct # def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, LineNo= -1): - if CheckFilename != '' and CheckFilename != None: + if CheckFilename != '' and CheckFilename is not None: (Root, Ext) = os.path.splitext(CheckFilename) if Ext.upper() != ExtName.upper(): ContainerFile = open(ContainerFilename, 'r').read() @@ -647,7 +662,7 @@ def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, # def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo= -1): CheckFile = '' - if CheckFilename != '' and CheckFilename != None: + if CheckFilename != '' and CheckFilename is not None: CheckFile = WorkspaceFile(WorkspaceDir, CheckFilename) if not os.path.isfile(CheckFile): ContainerFile = open(ContainerFilename, 'r').read() @@ -710,7 +725,7 @@ def RaiseParserError(Line, Section, File, Format='', LineNo= -1): # @retval string A full path # def WorkspaceFile(WorkspaceDir, Filename): - return os.path.join(NormPath(WorkspaceDir), NormPath(Filename)) + return mws.join(NormPath(WorkspaceDir), NormPath(Filename)) ## Split string # @@ -802,20 +817,34 @@ def GetHelpTextList(HelpTextClassList): def StringToArray(String): if isinstance(String, unicode): if len(unicode) == 0: - return "{0x00, 0x00}" - return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String]) + return "{0x00,0x00}" + return "{%s,0x00,0x00}" % ",".join(["0x%02x,0x00" % ord(C) for C in String]) elif String.startswith('L"'): if String == "L\"\"": - return "{0x00, 0x00}" + return "{0x00,0x00}" else: - return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String[2:-1]]) + return "{%s,0x00,0x00}" % ",".join(["0x%02x,0x00" % ord(C) for C in String[2:-1]]) elif String.startswith('"'): if String == "\"\"": - return "{0x00}"; + return "{0x00,0x00}" + else: + StringLen = len(String[1:-1]) + if StringLen % 2: + return "{%s,0x00}" % ",".join(["0x%02x" % ord(C) for C in String[1:-1]]) + else: + return "{%s,0x00,0x00}" % ",".join(["0x%02x" % ord(C) for C in String[1:-1]]) + elif String.startswith('{'): + StringLen = len(String.split(",")) + if StringLen % 2: + return "{%s,0x00}" % ",".join([ C.strip() for C in String[1:-1].split(',')]) else: - return "{%s, 0x00}" % ", ".join(["0x%02x" % ord(C) for C in String[1:-1]]) + return "{%s}" % ",".join([ C.strip() for C in String[1:-1].split(',')]) + else: - return '{%s, 0}' % ', '.join(String.split()) + if len(String.split()) % 2: + return '{%s,0}' % ','.join(String.split()) + else: + return '{%s,0,0}' % ','.join(String.split()) def StringArrayLength(String): if isinstance(String, unicode):