Patchwork util: add flag support to compilere

login
register
mail settings
Submitter Bryan O'Sullivan
Date March 11, 2013, 7:14 p.m.
Message ID <c6945de2450811e4d724.1363029272@australite.local>
Download mbox | patch
Permalink /patch/1109/
State Accepted, archived
Headers show

Comments

Bryan O'Sullivan - March 11, 2013, 7:14 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bryano@fb.com>
# Date 1363028773 25200
# Node ID c6945de2450811e4d7240f1e083eaea26d81bd2b
# Parent  ed3d3376fcd98cb249676183803ad745e657b704
util: add flag support to compilere
Augie Fackler - March 12, 2013, 12:26 a.m.
LGTM

On Mar 11, 2013, at 2:14 PM, Bryan O'Sullivan <bos@serpentine.com> wrote:

> # HG changeset patch
> # User Bryan O'Sullivan <bryano@fb.com>
> # Date 1363028773 25200
> # Node ID c6945de2450811e4d7240f1e083eaea26d81bd2b
> # Parent  ed3d3376fcd98cb249676183803ad745e657b704
> util: add flag support to compilere
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -662,10 +662,12 @@ try:
> except ImportError:
>     _re2 = False
> 
> -def compilere(pat):
> +def compilere(pat, flags=0):
>     '''Compile a regular expression, using re2 if possible
> 
> -    For best performance, use only re2-compatible regexp features.'''
> +    For best performance, use only re2-compatible regexp features. The
> +    only flags from the re module that are re2-compatible are
> +    IGNORECASE and MULTILINE.'''
>     global _re2
>     if _re2 is None:
>         try:
> @@ -673,12 +675,16 @@ def compilere(pat):
>             _re2 = True
>         except ImportError:
>             _re2 = False
> -    if _re2:
> +    if _re2 and (flags & ~(re.IGNORECASE | re.MULTILINE)) == 0:
> +        if flags & re.IGNORECASE:
> +            pat = '(?i)' + pat
> +        if flags & re.MULTILINE:
> +            pat = '(?m)' + pat
>         try:
>             return re2.compile(pat)
>         except re2.error:
>             pass
> -    return re.compile(pat)
> +    return re.compile(pat, flags)
> 
> _fspathcache = {}
> def fspath(name, root):
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
Durham Goode - March 12, 2013, 12:34 a.m.
Lgtm.  Have you verified that prepending (?i) and (?m) flags worked as
expected with re2?  Do the 'hg grep' tests cover them?

Durham

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -662,10 +662,12 @@  try:
 except ImportError:
     _re2 = False
 
-def compilere(pat):
+def compilere(pat, flags=0):
     '''Compile a regular expression, using re2 if possible
 
-    For best performance, use only re2-compatible regexp features.'''
+    For best performance, use only re2-compatible regexp features. The
+    only flags from the re module that are re2-compatible are
+    IGNORECASE and MULTILINE.'''
     global _re2
     if _re2 is None:
         try:
@@ -673,12 +675,16 @@  def compilere(pat):
             _re2 = True
         except ImportError:
             _re2 = False
-    if _re2:
+    if _re2 and (flags & ~(re.IGNORECASE | re.MULTILINE)) == 0:
+        if flags & re.IGNORECASE:
+            pat = '(?i)' + pat
+        if flags & re.MULTILINE:
+            pat = '(?m)' + pat
         try:
             return re2.compile(pat)
         except re2.error:
             pass
-    return re.compile(pat)
+    return re.compile(pat, flags)
 
 _fspathcache = {}
 def fspath(name, root):