i18n: patch polib.unescape
authorMartin Geisler <mg@lazybytes.net>
Sat, 19 Jun 2010 16:52:25 +0200
changeset 11388 db957a72fbd7
parent 11387 181936ec9bfb
child 11389 4fd49329a1b5
i18n: patch polib.unescape Patch submitted to upstream as rev b9174a342092 in http://bitbucket.org/mg/polib/
i18n/polib.py
--- 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 {{{