mercurial/util.py
changeset 51623 59f846fbc11d
parent 51549 a452807df09b
parent 51610 6c39edd1d348
equal deleted inserted replaced
51607:a4b3b8dee0a8 51623:59f846fbc11d
  2207     except OSError:
  2207     except OSError:
  2208         return True
  2208         return True
  2209 
  2209 
  2210 
  2210 
  2211 _re2_input = lambda x: x
  2211 _re2_input = lambda x: x
       
  2212 # google-re2 will need to be tell to not output error on its own
       
  2213 _re2_options = None
  2212 try:
  2214 try:
  2213     import re2  # pytype: disable=import-error
  2215     import re2  # pytype: disable=import-error
  2214 
  2216 
  2215     _re2 = None
  2217     _re2 = None
  2216 except ImportError:
  2218 except ImportError:
  2227 class _re:
  2229 class _re:
  2228     @staticmethod
  2230     @staticmethod
  2229     def _checkre2():
  2231     def _checkre2():
  2230         global _re2
  2232         global _re2
  2231         global _re2_input
  2233         global _re2_input
       
  2234         global _re2_options
  2232         if _re2 is not None:
  2235         if _re2 is not None:
  2233             # we already have the answer
  2236             # we already have the answer
  2234             return
  2237             return
  2235 
  2238 
  2236         check_pattern = br'\[([^\[]+)\]'
  2239         check_pattern = br'\[([^\[]+)\]'
  2245             # the `fb-re2` project provides a re2 module that acccept sysstr
  2248             # the `fb-re2` project provides a re2 module that acccept sysstr
  2246             check_pattern = pycompat.sysstr(check_pattern)
  2249             check_pattern = pycompat.sysstr(check_pattern)
  2247             check_input = pycompat.sysstr(check_input)
  2250             check_input = pycompat.sysstr(check_input)
  2248             _re2 = bool(re2.match(check_pattern, check_input))
  2251             _re2 = bool(re2.match(check_pattern, check_input))
  2249             _re2_input = pycompat.sysstr
  2252             _re2_input = pycompat.sysstr
       
  2253         try:
       
  2254             quiet = re2.Options()
       
  2255             quiet.log_errors = False
       
  2256             _re2_options = quiet
       
  2257         except AttributeError:
       
  2258             pass
  2250 
  2259 
  2251     def compile(self, pat, flags=0):
  2260     def compile(self, pat, flags=0):
  2252         """Compile a regular expression, using re2 if possible
  2261         """Compile a regular expression, using re2 if possible
  2253 
  2262 
  2254         For best performance, use only re2-compatible regexp features. The
  2263         For best performance, use only re2-compatible regexp features. The
  2260             if flags & remod.IGNORECASE:
  2269             if flags & remod.IGNORECASE:
  2261                 pat = b'(?i)' + pat
  2270                 pat = b'(?i)' + pat
  2262             if flags & remod.MULTILINE:
  2271             if flags & remod.MULTILINE:
  2263                 pat = b'(?m)' + pat
  2272                 pat = b'(?m)' + pat
  2264             try:
  2273             try:
  2265                 return re2.compile(_re2_input(pat))
  2274                 input_regex = _re2_input(pat)
       
  2275                 if _re2_options is not None:
       
  2276                     compiled = re2.compile(input_regex, options=_re2_options)
       
  2277                 else:
       
  2278                     compiled = re2.compile(input_regex)
       
  2279                 return compiled
  2266             except re2.error:
  2280             except re2.error:
  2267                 pass
  2281                 pass
  2268         return remod.compile(pat, flags)
  2282         return remod.compile(pat, flags)
  2269 
  2283 
  2270     @propertycache
  2284     @propertycache