changeset 11388:db957a72fbd7

i18n: patch polib.unescape Patch submitted to upstream as rev b9174a342092 in http://bitbucket.org/mg/polib/
author Martin Geisler <mg@lazybytes.net>
date Sat, 19 Jun 2010 16:52:25 +0200
parents 181936ec9bfb
children 4fd49329a1b5
files i18n/polib.py
diffstat 1 files changed, 15 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/i18n/polib.py	Thu Jun 17 20:10:34 2010 -0300
+++ b/i18n/polib.py	Sat Jun 19 16:52:25 2010 +0200
@@ -42,6 +42,7 @@
 import struct
 import textwrap
 import types
+import re
 
 default_encoding = 'utf-8'
 
@@ -243,18 +244,21 @@
     '\\n'
     >>> unescape(r'\\\\n')
     '\\\\n'
+    >>> unescape(r'\\\\n\\n')
+    '\\\\n\\n'
     """
-    raw_strings = [
-        (r'\\n', r'\n', '\n'),
-        (r'\\r', r'\r', '\r'),
-        (r'\\t', r'\t', '\t'),
-    ]
-    for a, b, c in raw_strings:
-        if a in st:
-            st = st.replace(a, b)
-        else:
-            st = st.replace(b, c)
-    return st.replace(r'\"', '"').replace(r'\\', '\\')
+    def unescape_repl(m):
+        m = m.group(1)
+        if m == 'n':
+            return '\n'
+        if m == 't':
+            return '\t'
+        if m == 'r':
+            return '\r'
+        if m == '\\':
+            return '\\'
+        return m # handles escaped double quote
+    return re.sub(r'\\(\\|n|t|r|")', unescape_repl, st)
 
 # }}}
 # class _BaseFile {{{