From 7857656439f240e6cc5f8e5d91a2aa4f11b2d0ea Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 11 Aug 2008 13:41:38 +0200 Subject: [PATCH] cleanup: removed special cases from string escaping code --- Cython/Utils.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Cython/Utils.py b/Cython/Utils.py index 0a85c7fc..f17ab613 100644 --- a/Cython/Utils.py +++ b/Cython/Utils.py @@ -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: -- 2.26.2