]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Optimize unescape()
authorAarni Koskela <akx@iki.fi>
Mon, 17 Mar 2025 10:51:31 +0000 (12:51 +0200)
committerAarni Koskela <akx@iki.fi>
Fri, 21 Mar 2025 06:23:57 +0000 (08:23 +0200)
babel/messages/pofile.py

index 4e732637c034fa6d410a159a5be8ff6ac841d65d..b0a017258229bd953879f515a6978bc3c16898e6 100644 (file)
@@ -25,6 +25,9 @@ if TYPE_CHECKING:
     from _typeshed import SupportsWrite
 
 
+_unescape_re = re.compile(r'\\([\\trn"])')
+
+
 def unescape(string: str) -> str:
     r"""Reverse `escape` the given string.
 
@@ -45,7 +48,10 @@ def unescape(string: str) -> str:
             return '\r'
         # m is \ or "
         return m
-    return re.compile(r'\\([\\trn"])').sub(replace_escapes, string[1:-1])
+
+    if "\\" not in string:  # Fast path: there's nothing to unescape
+        return string[1:-1]
+    return _unescape_re.sub(replace_escapes, string[1:-1])
 
 
 def denormalize(string: str) -> str: