templater: replace regex complexity by simple str containment checks
authorDirkjan Ochtman <dirkjan@ochtman.nl>
Sun, 17 May 2009 16:06:48 +0200
changeset 8475 caaf1e779b37
parent 8474 bccc90b56d02
child 8476 d5dda64f5552
templater: replace regex complexity by simple str containment checks
mercurial/templater.py
--- a/mercurial/templater.py	Sun May 17 16:05:50 2009 +0200
+++ b/mercurial/templater.py	Sun May 17 16:06:48 2009 +0200
@@ -42,8 +42,7 @@
     filter uses function to transform value. syntax is
     {key|filter1|filter2|...}.'''
 
-    template_re = re.compile(r"(?:(?:#(?=[\w\|%]+#))|(?:{(?=[\w\|%]+})))"
-                             r"(\w+)(?:(?:%(\w+))|((?:\|\w+)*))[#}]")
+    template_re = re.compile(r'{([\w\|%]+)}|#([\w\|%]+)#')
 
     def __init__(self, loader, filters={}, defaults={}):
         self.loader = loader
@@ -70,7 +69,8 @@
             else:
                 yield str(item)
 
-    def _format(self, key, format, get, map):
+    def _format(self, expr, get, map):
+        key, format = expr.split('%')
         v = get(key)
         if not hasattr(v, '__iter__'):
             raise SyntaxError(_("Error expanding '%s%%%s'") % (key, format))
@@ -79,9 +79,10 @@
             lm.update(i)
             yield self.process(format, lm)
 
-    def _filter(self, key, filters, get, map):
-        v = get(key)
-        for f in filters.split('|')[1:]:
+    def _filter(self, expr, get, map):
+        parts = expr.split('|')
+        v = get(parts[0])
+        for f in parts[1:]:
             v = self.filters[f](v)
         return v
 
@@ -103,18 +104,19 @@
                 break
 
             start, end = m.span(0)
-            key, fmt, fl = m.groups()
+            variants = m.groups()
+            expr = variants[0] or variants[1]
 
             if start:
                 yield tmpl[:start]
             tmpl = tmpl[end:]
 
-            if fmt:
-                yield self._format(key, fmt, get, map)
-            elif fl:
-                yield self._filter(key, fl, get, map)
+            if '%' in expr:
+                yield self._format(expr, get, map)
+            elif '|' in expr:
+                yield self._filter(expr, get, map)
             else:
-                yield get(key)
+                yield get(expr)
 
 engines = {'default': engine}