Commit 78576564 authored by Stefan Behnel's avatar Stefan Behnel

cleanup: removed special cases from string escaping code

parent 83a9dd2f
......@@ -100,15 +100,18 @@ class EncodedString(unicode):
# getattr(other, 'encoding', '') == self.encoding
def _to_oct_sequence(s):
return ''.join(['\\%03o' % ord(c) for c in s])
if s in '\n\r\t':
return repr(s)[1:-1]
else:
return ''.join(['\\%03o' % ord(c) for c in s])
_c_special = ('\0', '??', '<:', ':>', '<%', '%>', '%:', '%:')
_c_special = ('\0', '\n','\r','\t', '??', '<:', ':>', '<%', '%>', '%:', '%:')
_c_special_replacements = zip(_c_special, map(_to_oct_sequence, _c_special))
def _build_special_test():
subexps = []
for special in _c_special + ('\n','\r','\t'):
regexp = ''.join(['[%s]' % c for c in special ])
for special in _c_special:
regexp = ''.join(['[%s]' % c for c in special])
subexps.append(regexp)
return re.compile('(' + '|'.join(subexps) + ')').search
......@@ -116,7 +119,6 @@ _has_specials = _build_special_test()
def escape_byte_string(s):
if _has_specials(s):
s = s.replace('\n', r'\n').replace('\r', r'\r').replace('\t', r'\t')
for special, replacement in _c_special_replacements:
s = s.replace(special, replacement)
try:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment