## @file\r
# This file is used to define common string related functions used in parsing process\r
#\r
-# Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2018, 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
ValueList = []\r
Last = 0\r
Escaped = False\r
- InString = False\r
+ InSingleQuoteString = False\r
+ InDoubleQuoteString = False\r
InParenthesis = 0\r
for Index in range(0, len(String)):\r
Char = String[Index]\r
\r
if not Escaped:\r
# Found a splitter not in a string, split it\r
- if not InString and InParenthesis == 0 and Char == SplitTag:\r
+ if (not InSingleQuoteString or not InDoubleQuoteString) and InParenthesis == 0 and Char == SplitTag:\r
ValueList.append(String[Last:Index].strip())\r
Last = Index + 1\r
if MaxSplit > 0 and len(ValueList) >= MaxSplit:\r
break\r
\r
- if Char == '\\' and InString:\r
+ if Char == '\\' and (InSingleQuoteString or InDoubleQuoteString):\r
Escaped = True\r
- elif Char == '"':\r
- if not InString:\r
- InString = True\r
+ elif Char == '"' and not InSingleQuoteString:\r
+ if not InDoubleQuoteString:\r
+ InDoubleQuoteString = True\r
else:\r
- InString = False\r
+ InDoubleQuoteString = False\r
+ elif Char == "'" and not InDoubleQuoteString:\r
+ if not InSingleQuoteString:\r
+ InSingleQuoteString = True\r
+ else:\r
+ InSingleQuoteString = False\r
elif Char == '(':\r
InParenthesis = InParenthesis + 1\r
elif Char == ')':\r
# @param Arch: The Arch to be added or merged\r
#\r
def MergeArches(Dict, Key, Arch):\r
- if Key in Dict.keys():\r
+ if Key in Dict:\r
Dict[Key].append(Arch)\r
else:\r
Dict[Key] = Arch.split()\r
#\r
# remove comments, but we should escape comment character in string\r
#\r
- InString = False\r
+ InDoubleQuoteString = False\r
+ InSingleQuoteString = False\r
CommentInString = False\r
for Index in range(0, len(Line)):\r
- if Line[Index] == '"':\r
- InString = not InString\r
- elif Line[Index] == CommentCharacter and InString :\r
+ if Line[Index] == '"' and not InSingleQuoteString:\r
+ InDoubleQuoteString = not InDoubleQuoteString\r
+ elif Line[Index] == "'" and not InDoubleQuoteString:\r
+ InSingleQuoteString = not InSingleQuoteString\r
+ elif Line[Index] == CommentCharacter and (InSingleQuoteString or InDoubleQuoteString):\r
CommentInString = True\r
- elif Line[Index] == CommentCharacter and not InString :\r
+ elif Line[Index] == CommentCharacter and not (InSingleQuoteString or InDoubleQuoteString):\r
Line = Line[0: Index]\r
break\r
\r
#\r
# separate comments and statements, but we should escape comment character in string\r
#\r
- InString = False\r
+ InDoubleQuoteString = False\r
+ InSingleQuoteString = 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
+ if Line[Index] == '"' and not InSingleQuoteString:\r
+ InDoubleQuoteString = not InDoubleQuoteString\r
+ elif Line[Index] == "'" and not InDoubleQuoteString:\r
+ InSingleQuoteString = not InSingleQuoteString\r
+ elif Line[Index] == CommentCharacter and (InDoubleQuoteString or InSingleQuoteString):\r
CommentInString = True\r
- elif Line[Index] == CommentCharacter and not InString:\r
+ elif Line[Index] == CommentCharacter and not (InDoubleQuoteString or InSingleQuoteString):\r
Comment = Line[Index:].strip()\r
Line = Line[0:Index].strip()\r
break\r
# @retval True The file type is correct\r
#\r
def CheckFileType(CheckFilename, ExtName, ContainerFilename, SectionName, Line, LineNo= -1):\r
- if CheckFilename != '' and CheckFilename != None:\r
+ if CheckFilename != '' and CheckFilename is not None:\r
(Root, Ext) = os.path.splitext(CheckFilename)\r
if Ext.upper() != ExtName.upper():\r
ContainerFile = open(ContainerFilename, 'r').read()\r
#\r
def CheckFileExist(WorkspaceDir, CheckFilename, ContainerFilename, SectionName, Line, LineNo= -1):\r
CheckFile = ''\r
- if CheckFilename != '' and CheckFilename != None:\r
+ if CheckFilename != '' and CheckFilename is not None:\r
CheckFile = WorkspaceFile(WorkspaceDir, CheckFilename)\r
if not os.path.isfile(CheckFile):\r
ContainerFile = open(ContainerFilename, 'r').read()\r
def StringToArray(String):\r
if isinstance(String, unicode):\r
if len(unicode) == 0:\r
- return "{0x00, 0x00}"\r
- return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String])\r
+ return "{0x00,0x00}"\r
+ return "{%s,0x00,0x00}" % ",".join(["0x%02x,0x00" % ord(C) for C in String])\r
elif String.startswith('L"'):\r
if String == "L\"\"":\r
- return "{0x00, 0x00}"\r
+ return "{0x00,0x00}"\r
else:\r
- return "{%s, 0x00, 0x00}" % ", ".join(["0x%02x, 0x00" % ord(C) for C in String[2:-1]])\r
+ 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,0x00}"\r
else:\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
+ 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
+ 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
+ return "{%s,0x00}" % ",".join([ C.strip() for C in String[1:-1].split(',')])\r
else:\r
- return "{%s}" % ", ".join([ C for C in String[1:-1].split(',')])\r
+ return "{%s}" % ",".join([ C.strip() for C in String[1:-1].split(',')])\r
\r
else:\r
if len(String.split()) % 2:\r
- return '{%s, 0}' % ', '.join(String.split())\r
+ return '{%s,0}' % ','.join(String.split())\r
else:\r
- return '{%s, 0,0}' % ', '.join(String.split())\r
+ return '{%s,0,0}' % ','.join(String.split())\r
\r
def StringArrayLength(String):\r
if isinstance(String, unicode):\r