From patchwork Tue Jul 15 23:15:30 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: [8,of,9] util.re: add an escape method From: Siddharth Agarwal X-Patchwork-Id: 5176 Message-Id: <8ec138de734383da9ab4.1405466130@dev1738.prn1.facebook.com> To: Date: Tue, 15 Jul 2014 16:15:30 -0700 # HG changeset patch # User Siddharth Agarwal # Date 1405462485 25200 # Tue Jul 15 15:14:45 2014 -0700 # Node ID 8ec138de734383da9ab4fd60e4a61054906f50ed # Parent a8ad4f53079750108dbfa35f6f57e0a1a1f5b8b3 util.re: add an escape method The escape method in at least one of the modules called 're2' is in C. This means it is significantly faster than the Python code written in 're'. An upcoming patch will have benchmarks. diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -744,6 +744,21 @@ pass return remod.compile(pat, flags) + @propertycache + def escape(self): + '''Return the version of escape corresponding to self.compile. + + This is imperfect because whether re2 or re is used for a particular + function depends on the flags, etc, but it's the best we can do. + ''' + global _re2 + if _re2 is None: + self._checkre2() + if _re2: + return re2.escape + else: + return remod.escape + re = _re() _fspathcache = {}