minor optimization

This commit is contained in:
Miroslav Stampar 2011-12-22 20:08:28 +00:00
parent 094129a656
commit 8a7b0406c8
2 changed files with 12 additions and 9 deletions

View File

@ -24,6 +24,9 @@ HEX_ENCODED_CHAR_REGEX = r"(?P<result>\\x[0-9A-Fa-f]{2})"
# Raw chars that will be safe encoded to their slash (\) representations (e.g. newline to \n) # Raw chars that will be safe encoded to their slash (\) representations (e.g. newline to \n)
SAFE_ENCODE_SLASH_REPLACEMENTS = "\t\n\r\x0b\x0c" SAFE_ENCODE_SLASH_REPLACEMENTS = "\t\n\r\x0b\x0c"
# Characters that don't need to be safe encoded
SAFE_CHARS = "".join(filter(lambda x: x not in SAFE_ENCODE_SLASH_REPLACEMENTS, string.printable.replace('\\', '')))
# String used for temporary marking of slash characters # String used for temporary marking of slash characters
SLASH_MARKER = "__SLASH__" SLASH_MARKER = "__SLASH__"
@ -40,15 +43,15 @@ def safecharencode(value):
retVal = value retVal = value
if isinstance(value, basestring): if isinstance(value, basestring):
retVal = retVal.replace('\\', SLASH_MARKER) if any(_ not in SAFE_CHARS for _ in value):
retVal = retVal.replace('\\', SLASH_MARKER)
for char in SAFE_ENCODE_SLASH_REPLACEMENTS: for char in SAFE_ENCODE_SLASH_REPLACEMENTS:
retVal = retVal.replace(char, repr(char).strip('\'')) retVal = retVal.replace(char, repr(char).strip('\''))
retVal = retVal.replace(SLASH_MARKER, '\\\\') retVal = retVal.replace(SLASH_MARKER, '\\\\')
retVal = reduce(lambda x, y: x + (y if (y in string.printable or ord(y) > 255) else '\\x%02x' % ord(y)), retVal, unicode())
retVal = reduce(lambda x, y: x + (y if (y in string.printable or ord(y) > 255) else '\\x%02x' % ord(y)), retVal, unicode())
elif isinstance(value, list): elif isinstance(value, list):
for i in xrange(len(value)): for i in xrange(len(value)):
retVal[i] = safecharencode(value[i]) retVal[i] = safecharencode(value[i])

View File

@ -1332,12 +1332,12 @@ def parseUnionPage(output, unique=True):
if output is None: if output is None:
return None return None
data = BigArray()
if output.startswith(kb.chars.start) and output.endswith(kb.chars.stop): if output.startswith(kb.chars.start) and output.endswith(kb.chars.stop):
data = BigArray()
_ = []
regExpr = '%s(.*?)%s' % (kb.chars.start, kb.chars.stop) regExpr = '%s(.*?)%s' % (kb.chars.start, kb.chars.stop)
output = re.finditer(regExpr, output, re.DOTALL | re.IGNORECASE) output = re.finditer(regExpr, output, re.DOTALL | re.IGNORECASE)
_ = []
for entry in output: for entry in output:
entry = entry.group(1) entry = entry.group(1)