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)
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
SLASH_MARKER = "__SLASH__"
@@ -40,15 +43,15 @@ def safecharencode(value):
retVal = value
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:
retVal = retVal.replace(char, repr(char).strip('\''))
for char in SAFE_ENCODE_SLASH_REPLACEMENTS:
retVal = retVal.replace(char, repr(char).strip('\''))
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 = 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())
elif isinstance(value, list):
for i in xrange(len(value)):
retVal[i] = safecharencode(value[i])