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): ...@@ -100,15 +100,18 @@ class EncodedString(unicode):
# getattr(other, 'encoding', '') == self.encoding # getattr(other, 'encoding', '') == self.encoding
def _to_oct_sequence(s): 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)) _c_special_replacements = zip(_c_special, map(_to_oct_sequence, _c_special))
def _build_special_test(): def _build_special_test():
subexps = [] subexps = []
for special in _c_special + ('\n','\r','\t'): for special in _c_special:
regexp = ''.join(['[%s]' % c for c in special ]) regexp = ''.join(['[%s]' % c for c in special])
subexps.append(regexp) subexps.append(regexp)
return re.compile('(' + '|'.join(subexps) + ')').search return re.compile('(' + '|'.join(subexps) + ')').search
...@@ -116,7 +119,6 @@ _has_specials = _build_special_test() ...@@ -116,7 +119,6 @@ _has_specials = _build_special_test()
def escape_byte_string(s): def escape_byte_string(s):
if _has_specials(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: for special, replacement in _c_special_replacements:
s = s.replace(special, replacement) s = s.replace(special, replacement)
try: 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