check-code: introduce function for using re2 when available
authorSimon Heimberg <simohe@besonet.ch>
Sat, 08 Jun 2013 20:20:14 +0200
changeset 19310 30ea54660d14
parent 19309 7d77fa1cd537
child 19311 ad16e5c7a429
check-code: introduce function for using re2 when available Do it similar as in mercurial.util. For simplicity only support flag multiline which is the only one used.
contrib/check-code.py
--- a/contrib/check-code.py	Sat Jun 08 20:20:14 2013 +0200
+++ b/contrib/check-code.py	Sat Jun 08 20:20:14 2013 +0200
@@ -10,6 +10,20 @@
 import re, glob, os, sys
 import keyword
 import optparse
+try:
+    import re2
+except ImportError:
+    re2 = None
+
+def compilere(pat, multiline=False):
+    if multiline:
+        pat = '(?m)' + pat
+    if re2:
+        try:
+            return re2.compile(pat)
+        except re2.error:
+            pass
+    return re.compile(pat)
 
 def repquote(m):
     t = re.sub(r"\w", "x", m.group('text'))